diff --git a/setup.py b/setup.py index 5755b09..25fc474 100755 --- a/setup.py +++ b/setup.py @@ -1,66 +1,69 @@ #!/usr/bin/env python3 # Copyright (C) 2015-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os from setuptools import setup, find_packages from os import path from io import open here = path.abspath(path.dirname(__file__)) # Get the long description from the README file with open(path.join(here, 'README.md'), encoding='utf-8') as f: long_description = f.read() def parse_requirements(name=None): if name: reqf = 'requirements-%s.txt' % name else: reqf = 'requirements.txt' requirements = [] if not os.path.exists(reqf): return requirements with open(reqf) as f: for line in f.readlines(): line = line.strip() if not line or line.startswith('#'): continue requirements.append(line) return requirements setup( name='swh.core', description='Software Heritage core utilities', long_description=long_description, long_description_content_type='text/markdown', author='Software Heritage developers', author_email='swh-devel@inria.fr', url='https://forge.softwareheritage.org/diffusion/DCORE/', packages=find_packages(), scripts=[], install_requires=parse_requirements() + parse_requirements('swh'), setup_requires=['vcversioner'], extras_require={'testing': parse_requirements('test')}, vcversioner={}, include_package_data=True, + entry_points={ + 'console_scripts': ['swh-db-init=swh.core.cli:db_init'], + }, classifiers=[ "Programming Language :: Python :: 3", "Intended Audience :: Developers", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Operating System :: OS Independent", "Development Status :: 5 - Production/Stable", ], project_urls={ 'Bug Reports': 'https://forge.softwareheritage.org/maniphest', 'Funding': 'https://www.softwareheritage.org/donate', 'Source': 'https://forge.softwareheritage.org/source/swh-core', }, ) diff --git a/swh/core/cli.py b/swh/core/cli.py new file mode 100755 index 0000000..1da8bdb --- /dev/null +++ b/swh/core/cli.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# Copyright (C) 2018 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import warnings +warnings.filterwarnings("ignore") # noqa prevent psycopg from telling us sh*t + +from os import path +import glob + +import click +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 + + +@click.command() +@click.argument('module', nargs=-1, required=True) +@click.option('--db-name', '-d', help='Database name.', + default='softwareheritage-dev', show_default=True) +@click.option('--no-create', '-C', + help='Do not attempt to create the database', default=False) +def db_init(module, db_name=None, no_create=None): + """Create and initialise a database for the Software Heritage . + + Example: + + swh-db-init storage -d swh-test + + If you want to specify non-default postgresql connection parameters, + please provide them using standard environment variables. + See psql(1) man page (section ENVIRONMENTS) for details. + + Example: + + PGPORT=5434 swh-db-init indexer -d swh-indexer + + """ + + dump_files = [] + + for modname in module: + if not modname.startswith('swh.'): + modname = 'swh.{}'.format(modname) + try: + m = import_module(modname) + except ImportError: + raise click.BadParameter( + 'Unable to load module {}'.format(modname)) + + sqldir = path.join(path.dirname(m.__file__), 'sql') + if not path.isdir(sqldir): + raise click.BadParameter( + 'Module {} does not provide a db schema ' + '(no sql/ dir)'.format(modname)) + dump_files.extend(sorted(glob.glob(path.join(sqldir, '*.sql')), + key=sortkey)) + if not no_create: + pg_createdb(db_name) + + dump_files = [(x, DB_DUMP_TYPES[path.splitext(x)[1]]) + for x in dump_files] + for dump, dtype in dump_files: + click.secho('Loading {}'.format(dump), fg='yellow') + pg_restore(db_name, dump, dtype) + + click.secho('DONE database is {}'.format(db_name), fg='green', bold=True)