Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/cli.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 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 | ||||
# WARNING: do not import unnecessary things here to keep cli startup time under | # WARNING: do not import unnecessary things here to keep cli startup time under | ||||
# control | # control | ||||
import os | import os | ||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
import click | import click | ||||
import yaml | import yaml | ||||
from psycopg2.extensions import parse_dsn | |||||
from swh.core import config | from swh.core import config | ||||
from swh.core.cli import CONTEXT_SETTINGS | from swh.core.cli import CONTEXT_SETTINGS | ||||
from swh.core.cli import swh as swh_cli_group | from swh.core.cli import swh as swh_cli_group | ||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_bytes, hash_to_hex | ||||
# All generic config code should reside in swh.core.config | # All generic config code should reside in swh.core.config | ||||
CONFIG_ENVVAR = "SWH_CONFIG_FILE" | CONFIG_ENVVAR = "SWH_CONFIG_FILE" | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | if profile: | ||||
def exit(): | def exit(): | ||||
pr.disable() | pr.disable() | ||||
pr.dump_stats(profile) | pr.dump_stats(profile) | ||||
atexit.register(exit) | atexit.register(exit) | ||||
@cli.command(name="create") | @cli.command(name="create") | ||||
@click.option("--name", default=None) | @click.option("--maintenance-db", default=None) | ||||
@click.option("--drop/--no-drop", "drop_db", default=False) | |||||
@click.pass_context | @click.pass_context | ||||
def create(ctx, name): | def create(ctx, maintenance_db, drop_db): | ||||
"""Create new provenance database.""" | """Create new provenance database.""" | ||||
from .postgresql.db_utils import connect | from .postgresql.db_utils import connect | ||||
if ctx.obj["config"]["provenance"]["cls"] != "local": | |||||
raise ValueError( | |||||
"Unsupported provenance db cls: %s" | |||||
% (ctx.obj["config"]["provenance"]["cls"]) | |||||
) | |||||
# Connect to server without selecting a database | # Connect to server without selecting a database | ||||
conninfo = ctx.obj["config"]["provenance"]["db"] | dsn = ctx.obj["config"]["provenance"]["db"] | ||||
conn = connect(conninfo) | if isinstance(dsn, str): | ||||
dsn = parse_dsn(dsn) | |||||
dbname = dsn.pop("dbname") | |||||
if maintenance_db: | |||||
dsn["dbname"] = maintenance_db | |||||
if ctx.obj["config"]["provenance"]["cls"] == "ps": | conn = connect(dsn) | ||||
from .postgresql.provenance import create_database | |||||
create_database(conn, conninfo, name) | if ctx.obj["config"]["provenance"].get("with_path"): | ||||
elif ctx.obj["config"]["provenance"]["cls"] == "ps_np": | from .postgresql.provenance import create_database | ||||
else: | |||||
from .postgresql_nopath.provenance import create_database | from .postgresql_nopath.provenance import create_database | ||||
create_database(conn, conninfo, name) | create_database(conn, dbname, drop_db) | ||||
else: | |||||
raise NotImplementedError | |||||
@cli.command(name="iter-revisions") | @cli.command(name="iter-revisions") | ||||
@click.argument("filename") | @click.argument("filename") | ||||
@click.option("-l", "--limit", type=int) | @click.option("-l", "--limit", type=int) | ||||
@click.pass_context | @click.pass_context | ||||
def iter_revisions(ctx, filename, limit): | def iter_revisions(ctx, filename, limit): | ||||
# TODO: add file size filtering | # TODO: add file size filtering | ||||
▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines |