Changeset View
Changeset View
Standalone View
Standalone View
swh/core/cli/db.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (C) 2018 The Software Heritage developers | # Copyright (C) 2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import warnings | import warnings | ||||
warnings.filterwarnings("ignore") # noqa prevent psycopg from telling us sh*t | warnings.filterwarnings("ignore") # noqa prevent psycopg from telling us sh*t | ||||
import click | import click | ||||
from swh.core.cli import CONTEXT_SETTINGS | |||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
@click.command() | @click.command(context_settings=CONTEXT_SETTINGS) | ||||
@click.argument('module', nargs=-1, required=True) | @click.argument('module', nargs=-1, required=True) | ||||
@click.option('--db-name', '-d', help='Database name.', | @click.option('--db-name', '-d', help='Database name.', | ||||
default='softwareheritage-dev', show_default=True) | default='softwareheritage-dev', show_default=True) | ||||
def db_init(module, db_name=None): | @click.pass_context | ||||
def db_init(ctx, module, db_name=None): | |||||
"""Initialise a database for the Software Heritage <module>. By | """Initialise a database for the Software Heritage <module>. By | ||||
default, attempts to create the database first. | default, attempts to create the database first. | ||||
Example: | Example: | ||||
swh db-init -d swh-test storage | swh db-init -d swh-test storage | ||||
If you want to specify non-default postgresql connection parameters, | If you want to specify non-default postgresql connection parameters, | ||||
Show All 10 Lines | def db_init(ctx, module, db_name=None): | ||||
from os import path | from os import path | ||||
import glob | import glob | ||||
from importlib import import_module | from importlib import import_module | ||||
from swh.core.utils import numfile_sortkey as sortkey | from swh.core.utils import numfile_sortkey as sortkey | ||||
from swh.core.tests.db_testing import ( | from swh.core.tests.db_testing import ( | ||||
pg_createdb, pg_restore, DB_DUMP_TYPES, | pg_createdb, pg_restore, DB_DUMP_TYPES, | ||||
swh_db_version | swh_db_version | ||||
) | ) | ||||
ctx.ensure_object(dict) | |||||
# TODO: Remove the two lines below once the swh-db-init command | |||||
# is dropped | |||||
log_level = logging.INFO | |||||
douardda: This should not be needed there since the logging is set up in the main 'swh' group... | |||||
Done Inline ActionsWhat do you mean ? Every sub-commands proceed like this. anlambert: What do you mean ? Every sub-commands proceed like this. | |||||
Done Inline ActionsOh I see. If the log level is set to the root swh namespace, it will be inherited for sub ones ? This means we can remove the call to setLevel in subcommands ? anlambert: Oh I see. If the log level is set to the root `swh` namespace, it will be inherited for sub… | |||||
Not Done Inline Actionsyes. There are subcommands that do logging config stuff, but it is in general more than just a call to setLevel() (eg. in scheduler when calling setup_log_handler and so) but this is "just" some legacy parts that should be improved at some point IMHO. douardda: yes. There are subcommands that do logging config stuff, but it is in general more than just a… | |||||
if 'log_level' in ctx.obj: | |||||
log_level = ctx.obj['log_level'] | |||||
logger.setLevel(log_level) | |||||
logger.debug('db_init %s dn_name=%s', module, db_name) | logger.debug('db_init %s dn_name=%s', module, db_name) | ||||
dump_files = [] | dump_files = [] | ||||
for modname in module: | for modname in module: | ||||
if not modname.startswith('swh.'): | if not modname.startswith('swh.'): | ||||
modname = 'swh.{}'.format(modname) | modname = 'swh.{}'.format(modname) | ||||
try: | try: | ||||
m = import_module(modname) | m = import_module(modname) | ||||
Show All 30 Lines |
This should not be needed there since the logging is set up in the main 'swh' group...