Seperated argument parser creation from main kas entry point
With Sphinx it is possible to create the command line documentation automatically if there is a function that returns just the command line parser. Currently the creation of the argument parser is rather entangled with the rest of kas. This patches seperates this. Signed-off-by: Claudius Heine <ch@denx.de>
This commit is contained in:
parent
546b51f450
commit
503fd3d47a
@ -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',
|
||||
|
38
kas/kas.py
38
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()
|
||||
|
@ -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)
|
||||
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user