Skip to content
Snippets Groups Projects
Commit 19b1bece authored by David Douard's avatar David Douard
Browse files

cli: add a generic main 'swh' command

This allows to declare click-based subcommands using the setuptools'
entry point "swh.cli.subcommands".

The main "swh" command group does only handle the "--log-level" option.

Also extract the db-init command in a dedicated submodule and ensure both
  swh db-init
and
  swh-db-init
work.

Related to T1671.
parent 22e60ae5
No related branches found
No related tags found
1 merge request!56cli: add a generic main 'swh' command and extract the db-init command
setup.py 100755 → 100644
......@@ -51,9 +51,13 @@ setup(
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
[swh.cli.subcommands]
db-init=swh.core.cli.db:db_init
''',
classifiers=[
"Programming Language :: Python :: 3",
"Intended Audience :: Developers",
......
# 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)
ctx.obj['log_level'] = logging._nameToLevel[log_level]
def main():
logging.basicConfig()
# load plugins that define cli sub commands
for entry_point in pkg_resources.iter_entry_points('swh.cli.subcommands'):
cmd = entry_point.load()
swh.add_command(cmd, name=entry_point.name)
return swh(auto_envvar_prefix='SWH')
if __name__ == '__main__':
main()
......@@ -4,20 +4,14 @@
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
import logging
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
)
logger = logging.getLogger(__name__)
@click.command()
......@@ -30,7 +24,7 @@ def db_init(module, db_name=None):
Example:
swh-db-init storage -d swh-test
swh db-init -d swh-test storage
If you want to specify non-default postgresql connection parameters,
please provide them using standard environment variables.
......@@ -38,9 +32,21 @@ def db_init(module, db_name=None):
Example:
PGPORT=5434 swh-db-init indexer -d swh-indexer
PGPORT=5434 swh db-init 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
)
logger.debug('db_init %s dn_name=%s', module, db_name)
dump_files = []
for modname in module:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment