Changeset View
Changeset View
Standalone View
Standalone View
swh/core/cli/db.py
- This file was moved from swh/core/cli.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 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 | ||||
from os import path | |||||
import glob | |||||
import click | import click | ||||
from importlib import import_module | |||||
from swh.core.utils import numfile_sortkey as sortkey | |||||
from swh.core.tests.db_testing import ( | logger = logging.getLogger(__name__) | ||||
pg_createdb, pg_restore, DB_DUMP_TYPES, | |||||
swh_db_version | |||||
) | |||||
@click.command() | @click.command() | ||||
@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): | def db_init(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 storage -d swh-test | swh db-init storage -d swh-test | ||||
If you want to specify non-default postgresql connection parameters, | If you want to specify non-default postgresql connection parameters, | ||||
please provide them using standard environment variables. | please provide them using standard environment variables. | ||||
See psql(1) man page (section ENVIRONMENTS) for details. | See psql(1) man page (section ENVIRONMENTS) for details. | ||||
Example: | Example: | ||||
PGPORT=5434 swh-db-init indexer -d swh-indexer | 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 | |||||
) | |||||
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) | ||||
except ImportError: | except ImportError: | ||||
Show All 29 Lines |