Changeset View
Changeset View
Standalone View
Standalone View
swh/core/cli/db.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (C) 2018-2020 The Software Heritage developers | # Copyright (C) 2018-2020 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 glob | |||||
import logging | import logging | ||||
from os import path, environ | from os import path, environ | ||||
import subprocess | |||||
import warnings | import warnings | ||||
import click | import click | ||||
from swh.core.cli import CONTEXT_SETTINGS | from swh.core.cli import CONTEXT_SETTINGS | ||||
from swh.core.config import read as config_read | |||||
warnings.filterwarnings("ignore") # noqa prevent psycopg from telling us sh*t | warnings.filterwarnings("ignore") # noqa prevent psycopg from telling us sh*t | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
@click.group(name="db", context_settings=CONTEXT_SETTINGS) | @click.group(name="db", context_settings=CONTEXT_SETTINGS) | ||||
@click.option( | @click.option( | ||||
"--config-file", | "--config-file", | ||||
"-C", | "-C", | ||||
default=None, | default=None, | ||||
type=click.Path(exists=True, dir_okay=False), | type=click.Path(exists=True, dir_okay=False), | ||||
help="Configuration file.", | help="Configuration file.", | ||||
) | ) | ||||
@click.pass_context | @click.pass_context | ||||
def db(ctx, config_file): | def db(ctx, config_file): | ||||
"""Software Heritage database generic tools. | """Software Heritage database generic tools. | ||||
""" | """ | ||||
from swh.core.config import read as config_read | |||||
ctx.ensure_object(dict) | ctx.ensure_object(dict) | ||||
if config_file is None: | if config_file is None: | ||||
config_file = environ.get("SWH_CONFIG_FILENAME") | config_file = environ.get("SWH_CONFIG_FILENAME") | ||||
cfg = config_read(config_file) | cfg = config_read(config_file) | ||||
ctx.obj["config"] = cfg | ctx.obj["config"] = cfg | ||||
@db.command(name="init", context_settings=CONTEXT_SETTINGS) | @db.command(name="init", context_settings=CONTEXT_SETTINGS) | ||||
Show All 23 Lines | def init(ctx): | ||||
the command: | the command: | ||||
swh db -C /path/to/config.yml init | swh db -C /path/to/config.yml init | ||||
will initialize the database for the `storage` section using initialization | will initialize the database for the `storage` section using initialization | ||||
scripts from the `swh.storage` package. | scripts from the `swh.storage` package. | ||||
""" | """ | ||||
import subprocess | |||||
for modname, cfg in ctx.obj["config"].items(): | for modname, cfg in ctx.obj["config"].items(): | ||||
if cfg.get("cls") == "local" and cfg.get("args"): | if cfg.get("cls") == "local" and cfg.get("args"): | ||||
try: | try: | ||||
sqlfiles = get_sql_for_package(modname) | sqlfiles = get_sql_for_package(modname) | ||||
except click.BadParameter: | except click.BadParameter: | ||||
logger.info( | logger.info( | ||||
"Failed to load/find sql initialization files for %s", modname | "Failed to load/find sql initialization files for %s", modname | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | def db_init(module, db_name, create_db): | ||||
click.secho( | click.secho( | ||||
"DONE database is {} version {}".format(db_name, db_version), | "DONE database is {} version {}".format(db_name, db_version), | ||||
fg="green", | fg="green", | ||||
bold=True, | bold=True, | ||||
) | ) | ||||
def get_sql_for_package(modname): | def get_sql_for_package(modname): | ||||
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 | ||||
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: | ||||
raise click.BadParameter("Unable to load module {}".format(modname)) | raise click.BadParameter("Unable to load module {}".format(modname)) | ||||
sqldir = path.join(path.dirname(m.__file__), "sql") | sqldir = path.join(path.dirname(m.__file__), "sql") | ||||
if not path.isdir(sqldir): | if not path.isdir(sqldir): | ||||
raise click.BadParameter( | raise click.BadParameter( | ||||
"Module {} does not provide a db schema " "(no sql/ dir)".format(modname) | "Module {} does not provide a db schema " "(no sql/ dir)".format(modname) | ||||
) | ) | ||||
return list(sorted(glob.glob(path.join(sqldir, "*.sql")), key=sortkey)) | return list(sorted(glob.glob(path.join(sqldir, "*.sql")), key=sortkey)) |