Changeset View
Changeset View
Standalone View
Standalone View
swh/core/cli/__init__.py
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | |||||
def clean_exit_on_signal(signal, frame): | def clean_exit_on_signal(signal, frame): | ||||
"""Raise a SystemExit exception to let command-line clients wind themselves | """Raise a SystemExit exception to let command-line clients wind themselves | ||||
down on exit""" | down on exit""" | ||||
raise SystemExit(0) | raise SystemExit(0) | ||||
@click.group( | @click.group( | ||||
context_settings=CONTEXT_SETTINGS, cls=AliasedGroup, | context_settings=CONTEXT_SETTINGS, | ||||
cls=AliasedGroup, | |||||
option_notes='''\ | option_notes='''\ | ||||
If both options are present, --log-level will override the root logger | If both options are present, --log-level will override the root logger | ||||
configuration set in --log-config. | configuration set in --log-config. | ||||
The --log-config YAML must conform to the logging.config.dictConfig schema | The --log-config YAML must conform to the logging.config.dictConfig schema | ||||
documented at https://docs.python.org/3/library/logging.config.html. | documented at https://docs.python.org/3/library/logging.config.html. | ||||
''' | ''', | ||||
) | ) | ||||
@click.option('--log-level', '-l', default=None, | @click.option( | ||||
'--log-level', | |||||
'-l', | |||||
default=None, | |||||
type=click.Choice(LOG_LEVEL_NAMES), | type=click.Choice(LOG_LEVEL_NAMES), | ||||
help="Log level (defaults to INFO).") | help="Log level (defaults to INFO).", | ||||
@click.option('--log-config', default=None, | ) | ||||
@click.option( | |||||
'--log-config', | |||||
default=None, | |||||
type=click.File('r'), | type=click.File('r'), | ||||
help="Python yaml logging configuration file.") | help="Python yaml logging configuration file.", | ||||
@click.option('--sentry-dsn', default=None, | ) | ||||
help="DSN of the Sentry instance to report to") | @click.option( | ||||
@click.option('--sentry-debug/--no-sentry-debug', | '--sentry-dsn', default=None, help="DSN of the Sentry instance to report to" | ||||
default=False, hidden=True, | ) | ||||
help="Enable debugging of sentry") | @click.option( | ||||
'--sentry-debug/--no-sentry-debug', | |||||
default=False, | |||||
hidden=True, | |||||
help="Enable debugging of sentry", | |||||
) | |||||
@click.pass_context | @click.pass_context | ||||
def swh(ctx, log_level, log_config, sentry_dsn, sentry_debug): | def swh(ctx, log_level, log_config, sentry_dsn, sentry_debug): | ||||
"""Command line interface for Software Heritage. | """Command line interface for Software Heritage. | ||||
""" | """ | ||||
signal.signal(signal.SIGTERM, clean_exit_on_signal) | signal.signal(signal.SIGTERM, clean_exit_on_signal) | ||||
signal.signal(signal.SIGINT, clean_exit_on_signal) | signal.signal(signal.SIGINT, clean_exit_on_signal) | ||||
init_sentry(sentry_dsn, debug=sentry_debug) | init_sentry(sentry_dsn, debug=sentry_debug) | ||||
Show All 17 Lines | def main(): | ||||
# for the next few logging statements | # for the next few logging statements | ||||
logging.basicConfig() | logging.basicConfig() | ||||
# load plugins that define cli sub commands | # load plugins that define cli sub commands | ||||
for entry_point in pkg_resources.iter_entry_points('swh.cli.subcommands'): | for entry_point in pkg_resources.iter_entry_points('swh.cli.subcommands'): | ||||
try: | try: | ||||
cmd = entry_point.load() | cmd = entry_point.load() | ||||
swh.add_command(cmd, name=entry_point.name) | swh.add_command(cmd, name=entry_point.name) | ||||
except Exception as e: | except Exception as e: | ||||
logger.warning('Could not load subcommand %s: %s', | logger.warning('Could not load subcommand %s: %s', entry_point.name, str(e)) | ||||
entry_point.name, str(e)) | |||||
return swh(auto_envvar_prefix='SWH') | return swh(auto_envvar_prefix='SWH') | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
main() | main() |