diff --git a/kas/build.py b/kas/build.py index 798288f..0b45005 100644 --- a/kas/build.py +++ b/kas/build.py @@ -25,7 +25,7 @@ import os from .config import load_config -from .libkas import find_program, run_cmd +from .libkas import find_program, run_cmd, kasplugin from .libcmds import (Macro, Command, SetupDir, SetupProxy, CleanupSSHAgent, SetupSSHAgent, SetupEnviron, WriteConfig, SetupHome, ReposFetch, @@ -35,12 +35,17 @@ __license__ = 'MIT' __copyright__ = 'Copyright (c) Siemens AG, 2017' +@kasplugin class Build: """ This class implements the build plugin for kas. """ - def __init__(self, parser): + @classmethod + def get_argparser(cls, parser): + """ + Returns an a parser for the build plugin + """ bld_psr = parser.add_parser('build') bld_psr.add_argument('config', diff --git a/kas/kas.py b/kas/kas.py index cd886d6..2b34b91 100644 --- a/kas/kas.py +++ b/kas/kas.py @@ -42,10 +42,16 @@ try: except ImportError: HAVE_COLORLOG = False -from .build import Build -from .shell import Shell from . import __version__ +# Import kas plugins +# Since they are added by decorators, they don't need to be called, +# just imported. +# pylint: disable=unused-import +from .libkas import kasplugin +from . import build +from . import shell + __license__ = 'MIT' __copyright__ = 'Copyright (c) Siemens AG, 2017' @@ -90,12 +96,10 @@ def _atexit_handler(loop): os.killpg(os.getpid(), signal.SIGTERM) -def kas(argv): +def kas_get_argparser(): """ - The main entry point of kas. + Creates a argparser for kas with all plugins. """ - create_logger() - parser = argparse.ArgumentParser(description='Steer ebs-yocto builds') parser.add_argument('--version', action='version', @@ -106,12 +110,22 @@ def kas(argv): help='Enable debug logging') subparser = parser.add_subparsers(help='sub command help', dest='cmd') - sub_cmds = [Build(subparser), Shell(subparser)] + for ext_plugin in pkg_resources.iter_entry_points('kas.plugins'): + ext_plugin.load() - for plugin in pkg_resources.iter_entry_points('kas.plugins'): - cmd = plugin.load() - sub_cmds.append(cmd(subparser)) + for plugin in getattr(kasplugin, 'plugins', []): + plugin.get_argparser(subparser) + return parser + + +def kas(argv): + """ + The main entry point of kas. + """ + create_logger() + + parser = kas_get_argparser() args = parser.parse_args(argv) if args.debug: @@ -123,8 +137,8 @@ def kas(argv): loop.add_signal_handler(sig, interruption) atexit.register(_atexit_handler, loop=loop) - for cmd in sub_cmds: - if cmd.run(args): + for plugin in getattr(kasplugin, 'plugins', []): + if plugin().run(args): return parser.print_help() diff --git a/kas/libkas.py b/kas/libkas.py index 9487c6c..9a8d53b 100644 --- a/kas/libkas.py +++ b/kas/libkas.py @@ -355,3 +355,12 @@ def ssh_no_host_key_check(_): os.mkdir(home + '/.ssh') with open(home + '/.ssh/config', 'w') as fds: fds.write('Host *\n\tStrictHostKeyChecking no\n\n') + + +def kasplugin(plugin_class): + """ + A decorator that registeres kas plugins + """ + if not hasattr(kasplugin, 'plugins'): + setattr(kasplugin, 'plugins', []) + getattr(kasplugin, 'plugins').append(plugin_class) diff --git a/kas/shell.py b/kas/shell.py index dd00beb..f007f7f 100644 --- a/kas/shell.py +++ b/kas/shell.py @@ -25,6 +25,7 @@ """ import subprocess +from kas.libkas import kasplugin from kas.config import load_config from kas.libcmds import (Macro, Command, SetupProxy, SetupEnviron, SetupHome) @@ -32,12 +33,17 @@ __license__ = 'MIT' __copyright__ = 'Copyright (c) Siemens AG, 2017' +@kasplugin class Shell: """ Implements a kas plugin that opens a shell within the kas environment. """ - def __init__(self, parser): + @classmethod + def get_argparser(cls, parser): + """ + Returns a parser for the shell plugin + """ sh_prs = parser.add_parser('shell') sh_prs.add_argument('config',