Skip to content

Commit

Permalink
Set up the distrubuted commands system
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaudov, Stanislav Tomov committed Jun 16, 2020
1 parent 675cc28 commit 1eb8f7c
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 25 deletions.
Empty file.
12 changes: 12 additions & 0 deletions code_manager/commands/commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CommitCommand:

name = 'commit'

def __init__(self):
pass

def execute(self, arguments, root):
pass


ExportedClass = CommitCommand
17 changes: 17 additions & 0 deletions code_manager/commands/grep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import subprocess


class GrepCommand:

name = 'grep'

def __init__(self):
pass

def execute(self, args, path):

ret = subprocess.run(['git', '-C', path, 'grep', *args], stdout=subprocess.PIPE)
return path, ret.returncode, ret.stdout


ExportedClass = GrepCommand
12 changes: 12 additions & 0 deletions code_manager/commands/pull.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class PullCommand:

name = 'pull'

def __init__(self):
pass

def execute(self, arguments, root):
pass


ExportedClass = PullCommand
12 changes: 12 additions & 0 deletions code_manager/commands/push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class PushCommand:

name = 'push'

def __init__(self):
pass

def execute(self, arguments, root):
pass


ExportedClass = PushCommand
12 changes: 12 additions & 0 deletions code_manager/commands/sed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class SedCommand:

name = 'sed'

def __init__(self):
pass

def execute(self, arguments, root):
pass


ExportedClass = SedCommand
53 changes: 53 additions & 0 deletions code_manager/core/command_container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import logging
import os

import code_manager.commands
from code_manager.core.configuration import ConfigurationAware
from code_manager.utils.importing import import_modules_from_folder
from code_manager.utils.logger import debug_cyan
from code_manager.utils.logger import debug_red


class CommandContainer(ConfigurationAware):

def __init__(self):
self.commands = {}
self.commands_dir = os.path.dirname(code_manager.commands.__file__)

def load_commands(self):
import_modules_from_folder(self.commands_dir, 'code_manager.commands', self._add_command)

def _add_command(self, command, file):
assert command is not None
assert file is not None

if not hasattr(command, 'ExportedClass'):
debug_red('No exported command class found in file %s', file)
return

if command.ExportedClass.name is None:
debug_red('The exported class does not have proper name.')
return

CommandClass = command.ExportedClass # pylint: disable=C0103

if CommandClass.name in self.commands.keys():
debug_red('Command with the name \'%s\' already exists', CommandClass.name)

debug_cyan('Loading command: \'%s\'', CommandClass.name)

self.commands[CommandClass.name] = CommandClass

def execute_command(self, command, args, pack, root):

if command not in self.commands.keys():
return

command_obj = self.commands[command]()

command_obj.root = root
command_obj.args = args
command_obj.pack = pack

logging.debug('Executing command: \'%s\' on package \'%s\'', command, pack)
command_obj.execute(args, root)
18 changes: 16 additions & 2 deletions code_manager/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

from code_manager.core.cache_container import CacheContainer
from code_manager.core.command_container import CommandContainer
from code_manager.core.configuration import CofigurationResolver
from code_manager.core.configuration import ConfigurationAware
from code_manager.core.deb_dependency import Depender
Expand All @@ -29,6 +30,7 @@ def __init__(self):
self.depender = DebGrapher()
self.dep_depender = Depender()
self.resolver = CofigurationResolver()
self.commands = CommandContainer()

self._setup_all()

Expand All @@ -52,7 +54,6 @@ def _get_root(self, pack):
return root

def _setup_all(self):
self.installation.load_installer()
self.cache.load_cache()
self.depender.verify_packages_tree()

Expand Down Expand Up @@ -114,6 +115,9 @@ def _invoke_fetch(self):
self._do_fetch(pack)

def _invoke_build(self):

self.installation.load_installer()

broken = []
for pack in self.install_queue:
if self.cache.is_installed(pack):
Expand All @@ -133,6 +137,9 @@ def _invoke_build(self):
cache.set_built(pack, True)

def _invoke_install(self):

self.installation.load_installer()

extended_queue = set(self.install_queue)
for pack in self.install_queue:
extended_queue.update(self.depender.get_deep_dependencies(pack))
Expand Down Expand Up @@ -300,4 +307,11 @@ def rebuild_cache(self, asume_installed=True):
self.cache.rebuild(pack, pack_root)

def run_command(self, command, args):
pass
self.commands.load_commands()
for pack, _ in self.packages.items():

if not self.cache.is_installed(pack):
continue
root = os.path.join(self.code_dir, self._get_root(pack))
self.commands.execute_command(command, args, pack, root)
return 0
89 changes: 66 additions & 23 deletions code_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
CODE_DIR = None
INSTALL_SCRIPTS_DIR = None
PACKAGES_FILE = None
commands = dict()


def command(name):
global commands

def inner(func):
commands[name] = func
return func
return inner


def get_arg_parser():
Expand Down Expand Up @@ -290,8 +300,7 @@ def get_arg_parser():
)

edit_parser = subparsers.add_parser(
'edit-cache', help='Edit the cache - the entire\
file of a section for specific package',
'edit-cache', help='Edit the cache file',
)

edit_parser.add_argument(
Expand Down Expand Up @@ -322,34 +331,54 @@ def get_arg_parser():
help='Groups to list. Will list every group if not given',
)

grep_parser = subparsers.add_parser('grep', help='Distributed grep over the fetched pakcages')
grep_parser.add_argument('rest', nargs=argparse.REMAINDER)

sed_parser = subparsers.add_parser('sed', help='Distributed sed over the fetched pakcages')
sed_parser.add_argument('rest', nargs=argparse.REMAINDER)

push_parser = subparsers.add_parser('push', help='Push every pacakge with git repo')
push_parser.add_argument('rest', nargs=argparse.REMAINDER)

pull_parser = subparsers.add_parser('pull', help='Pull every pacakge with git repo')
pull_parser.add_argument('rest', nargs=argparse.REMAINDER)

commit_parser = subparsers.add_parser('commit', help='Make commit for every pacakge with git repo')
commit_parser.add_argument('rest', nargs=argparse.REMAINDER)

return parser


@command('install')
def install(args, core):
if args.group is not None:
core.install_thing(args.group, install=True)
else:
core.install_thing(args.packages, install=True)


@command('fetch')
def fetch(args, core):
if args.group is not None:
core.install_thing(args.group, fetch=True, force=args.force)
else:
core.install_thing(args.packages, fetch=True, force=args.force)


@command('build')
def build(args, core):
if args.group is not None:
core.install_thing(args.group, build=True)
else:
core.install_thing(args.packages, build=True)


@command('remove')
def remove(args, core):
core.remove_package(args.packages)


@command('list-packages')
def list_packages(args, core):
logging.debug('Available packages:')
packs = flatten(CONFIG['packages_list'].values())
Expand All @@ -359,6 +388,7 @@ def list_packages(args, core):
print('\n'.join(packs))


@command('list-cache')
def list_cache(args, core):
logging.debug('Dumping cache file %s', CACHE)

Expand Down Expand Up @@ -387,6 +417,7 @@ def list_cache(args, core):
print(string)


@command('list-groups')
def list_groups(args, core):

if not args.groups:
Expand All @@ -400,6 +431,7 @@ def list_groups(args, core):
print(string)


@command('clear-cache')
def clear_cache(args, core):

if args.all:
Expand All @@ -414,6 +446,7 @@ def clear_cache(args, core):
cache.drop(pack)


@command('edit-cache')
def edit_cache(args, _):
if args.package is None:
logging.info('Editing %s', CACHE)
Expand All @@ -439,32 +472,41 @@ def edit_cache(args, _):
json.dump(cont, fp, indent=4)


@command('rebuild-cache')
def rebuild_cache(args, core):
core.rebuild_cache(args.assume_installed)


def list_fetchers(_, core):
print('Currently available fetchers:')
for fetcher in core.get_fetchers():
print(f'\t - {fetcher}')
@command('grep')
def grep(args, core):
core.run_command('grep', args.rest)


def get_commands_map():
commands = dict()
@command('sed')
def sed(args, core):
core.run_command('sed', args)

commands['install'] = install
commands['fetch'] = fetch
commands['build'] = build
commands['list-packages'] = list_packages
commands['list-cache'] = list_cache
commands['clear-cache'] = clear_cache
commands['rebuild-cache'] = rebuild_cache
commands['edit-cache'] = edit_cache
commands['list-groups'] = list_groups
commands['list-fetchers'] = list_fetchers
commands['remove'] = remove

return commands
@command('push')
def push(args, core):
core.run_command('push', args)


@command('pull')
def pull(args, core):
core.run_command('pull', args)


@command('commit')
def commit(args, core):
core.run_command('commit', args)


@command('list-fetchers')
def list_fetchers(_, core):
print('Currently available fetchers:')
for fetcher in core.get_fetchers():
print(f'\t - {fetcher}')


def copy_config():
Expand Down Expand Up @@ -606,9 +648,12 @@ def save_opt(opt):
def main():

parser = get_arg_parser()
args = parser.parse_args()
args, _ = parser.parse_known_args()
opt = configparser.ConfigParser()

if '--' in args.rest:
args.rest.remove('--')

copy_config()
opt.read(os.path.join(code_manager.CONFDIR, 'conf'))
setup_logging(args, opt)
Expand All @@ -627,8 +672,6 @@ def main():
parser.print_help()
raise SystemExit

commands = get_commands_map()

ConfigurationAware.set_configuration(CONFIG, INSTALL_SCRIPTS_DIR, CACHE, opt, args, extra_configs=args.packs)

logging.debug('Code dir: %s', CODE_DIR)
Expand Down

0 comments on commit 1eb8f7c

Please sign in to comment.