diff --git a/setup.py b/setup.py old mode 100755 new mode 100644 --- a/setup.py +++ b/setup.py @@ -51,9 +51,13 @@ extras_require={'testing': parse_requirements('test')}, vcversioner={}, include_package_data=True, - entry_points={ - 'console_scripts': ['swh-db-init=swh.core.cli:db_init'], - }, + entry_points=''' + [console_scripts] + swh=swh.core.cli:main + swh-db-init=swh.core.cli.db:db_init_cmd + [swh.cli] + swh-core-db=swh.core.cli.db + ''', classifiers=[ "Programming Language :: Python :: 3", "Intended Audience :: Developers", diff --git a/swh/core/cli/__init__.py b/swh/core/cli/__init__.py new file mode 100644 --- /dev/null +++ b/swh/core/cli/__init__.py @@ -0,0 +1,41 @@ +# Copyright (C) 2019 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import click +import logging +import pkg_resources + +logger = logging.getLogger(__name__) + + +CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) + + +@click.group(context_settings=CONTEXT_SETTINGS) +@click.option('--log-level', '-l', default='INFO', + type=click.Choice(logging._nameToLevel.keys()), + help="Log level (default to INFO)") +@click.pass_context +def swh(ctx, log_level): + """Software Heritage Tool + """ + logger.setLevel(log_level) + ctx.ensure_object(dict) + + +def main(): + logging.basicConfig() + # load plugins that define cli sub commands + cli_plugins = { # noqa + entry_point.name: entry_point.load() + for entry_point + in pkg_resources.iter_entry_points('swh.cli') + } + + return swh(auto_envvar_prefix='SWH') + + +if __name__ == '__main__': + main() diff --git a/swh/core/cli.py b/swh/core/cli/db.py rename from swh/core/cli.py rename to swh/core/cli/db.py --- a/swh/core/cli.py +++ b/swh/core/cli/db.py @@ -7,20 +7,11 @@ import warnings warnings.filterwarnings("ignore") # noqa prevent psycopg from telling us sh*t -from os import path -import glob - import click -from importlib import import_module -from swh.core.utils import numfile_sortkey as sortkey -from swh.core.tests.db_testing import ( - pg_createdb, pg_restore, DB_DUMP_TYPES, - swh_db_version -) +from swh.core.cli import swh -@click.command() @click.argument('module', nargs=-1, required=True) @click.option('--db-name', '-d', help='Database name.', default='softwareheritage-dev', show_default=True) @@ -30,7 +21,7 @@ Example: - swh-db-init storage -d swh-test + swh db-init storage -d swh-test If you want to specify non-default postgresql connection parameters, please provide them using standard environment variables. @@ -41,6 +32,16 @@ PGPORT=5434 swh-db-init indexer -d swh-indexer """ + # put import statements here so we can keep startup time of the main swh + # command as short as possible + from os import path + import glob + from importlib import import_module + from swh.core.utils import numfile_sortkey as sortkey + from swh.core.tests.db_testing import ( + pg_createdb, pg_restore, DB_DUMP_TYPES, + swh_db_version + ) dump_files = [] for modname in module: @@ -78,3 +79,7 @@ click.secho('DONE database is {} version {}'.format(db_name, db_version), fg='green', bold=True) + + +db_init_cmd = click.command()(db_init) # for bw compat +db_init_cli = swh.command()(db_init)