Skip to content
Snippets Groups Projects

cli: add a generic main 'swh' command and extract the db-init command

3 unresolved threads

in a dedicated submodule. Ensure both:

swh db-init

and

swh-db-init

work.

Related to #1671 (closed).


Migrated from D1426 (view on Phabricator)

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
20 @click.pass_context
21 def swh(ctx, log_level):
22 """Software Heritage Tool
23 """
24 logger.setLevel(log_level)
25 ctx.ensure_object(dict)
26 ctx.obj['log_level'] = logging._nameToLevel[log_level]
27
28
29 def main():
30 logging.basicConfig()
31 # load plugins that define cli sub commands
32 for entry_point in pkg_resources.iter_entry_points('swh.cli.subcommands'):
33 cmd = entry_point.load()
34 swh.add_command(cmd, name=entry_point.name)
35
  • Do we really need the dict here? If so, would you mind adding a comment as to how/where it's being used in addition to the noqa? (and if not, I guess we can just flip around the for loop)

  • Author Maintainer

    we probably do not need to keep this dict, but we need to ensure packages are loaded. Let me check this.

  • Please register or sign in to reply
  • Merge request was returned for changes

  • setup.py 100755 → 100644
    51 51 extras_require={'testing': parse_requirements('test')},
    52 52 vcversioner={},
    53 53 include_package_data=True,
    54 entry_points={
    55 'console_scripts': ['swh-db-init=swh.core.cli:db_init'],
    56 },
    54 entry_points='''
    55 [console_scripts]
    56 swh=swh.core.cli:main
    57 swh-db-init=swh.core.cli.db:db_init
    58 [swh.cli.subcommands]
  • setup.py 100755 → 100644
    51 51 extras_require={'testing': parse_requirements('test')},
    52 52 vcversioner={},
    53 53 include_package_data=True,
    54 entry_points={
    55 'console_scripts': ['swh-db-init=swh.core.cli:db_init'],
    56 },
    54 entry_points='''
    55 [console_scripts]
    56 swh=swh.core.cli:main
    57 swh-db-init=swh.core.cli.db:db_init
    58 [swh.cli.subcommands]
    59 db-init=swh.core.cli.db:db_init
  • So, from what I understand, the subcommand name is set by the name of the function in the plugin, when the swh.command() decorator is called.

    I think it would be less surprising, if the entry points were used to actually define which functions are to be decorated, and the subcommand name.

    So your entry point definition could be

    [swh.cli.subcommands]
    db-init=swh.core.cli.db:db_init

    And you could make the main or swh command lazy-load and decorate the proper function at runtime.

    Does that make sense?

  • Author Maintainer

    no need to keep the cli plugins in a dict

  • Author Maintainer

    Implement the entrypoint loading mechanism using functions instead of modules

    as suggested by olasd. This does not implement any generic lazy loading magic stuff for now.

  • Nicolas Dandrimont mentioned in merge request !57 (closed)

    mentioned in merge request !57 (closed)

  • mentioned in merge request swh-deposit!23 (closed)

  • Merge request was accepted

  • Nicolas Dandrimont approved this merge request

    approved this merge request

  • Author Maintainer

    include forgotten patch: add log_level to 'swh' context obj

    so subcommands can do specific stuff with current log level if needed.

    See swh/core/cli/init.py:26

  • Author Maintainer

    Merge request was merged

  • closed

  • Please register or sign in to reply
    Loading