diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -15,6 +15,44 @@ # Basic use +The main entry point to import a Mercurial repository is the `main` function +defined in the `swh.loader.mercurial.cli` module: + +``` bash +python3 -m swh.loader.mercurial.cli +``` + + +If the Python package has been installed via `pip`, you should be able +to type: + +``` bash +user@host:~$ swh-loader-hg --help + +Usage: swh-loader-hg [OPTIONS] ORIGIN_URL + +Options: + -d, --hg-directory TEXT Path to the hg (local) directory to load + from. If unset, the hg repo will ben cloned + from the given (origin) url + -a, --hg-archive TEXT Path to the hg (local) archive file to load + from. + -D, --visit-date TEXT Visit date (defaults to now) + -l, --log-level [NOTSET|DEBUG|INFO|WARNING|ERROR|CRITICAL] + Log level + --help Show this message and exit. + +``` + +For example: + +``` bash +user@host:~$ swh-loader-hg https://www.mercurial-scm.org/repo/hello +[...] +``` + + +# From Python From python3's toplevel: ## Remote diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -25,4 +25,7 @@ setup_requires=['vcversioner'], vcversioner={}, include_package_data=True, + entry_points={ + 'console_scripts': ['swh-loader-hg=swh.loader.mercurial.cli:main'], + }, ) diff --git a/swh/loader/mercurial/bundle20_loader_verifier.py b/swh/loader/mercurial/bundle20_loader_verifier.py --- a/swh/loader/mercurial/bundle20_loader_verifier.py +++ b/swh/loader/mercurial/bundle20_loader_verifier.py @@ -15,7 +15,7 @@ from swh.model.hashutil import MultiHash -from .bundle20_loader import HgBundle20Loader +from .loader import HgBundle20Loader from .converters import PRIMARY_ALGO as ALGO from .objects import SimpleTree diff --git a/swh/loader/mercurial/cli.py b/swh/loader/mercurial/cli.py new file mode 100644 --- /dev/null +++ b/swh/loader/mercurial/cli.py @@ -0,0 +1,50 @@ +# 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 datetime +import click +import logging + +from .loader import HgBundle20Loader, HgArchiveBundle20Loader + +LOGLEVELS = [logging._levelToName[lvl] + for lvl in sorted(logging._levelToName.keys())] + + +@click.command() +@click.argument('origin-url') +@click.option('--hg-directory', '-d', + help=('Path to the hg (local) directory to load from. ' + 'If unset, the hg repo will be cloned from the ' + 'given (origin) url.')) +@click.option('--hg-archive', '-a', + help=('Path to the hg archive file to load from.')) +@click.option('--visit-date', '-D', help='Visit date (defaults to now).') +@click.option('--log-level', '-l', + type=click.Choice(LOGLEVELS, case_sensitive=False), + help='Log level.') +def main(origin_url, hg_directory=None, + hg_archive=None, visit_date=None, log_level=None): + + logging.basicConfig( + level=(log_level or 'DEBUG').upper(), + format='%(asctime)s %(process)d %(message)s') + + if not visit_date: + visit_date = datetime.datetime.now(tz=datetime.timezone.utc) + kwargs = {'visit_date': visit_date, + 'origin_url': origin_url} + if hg_archive: + HgLoader = HgArchiveBundle20Loader + kwargs['archive_path'] = hg_archive + else: + HgLoader = HgBundle20Loader + kwargs['directory'] = hg_directory + + return HgLoader().load(**kwargs) + + +if __name__ == '__main__': + main() diff --git a/swh/loader/mercurial/bundle20_loader.py b/swh/loader/mercurial/loader.py rename from swh/loader/mercurial/bundle20_loader.py rename to swh/loader/mercurial/loader.py diff --git a/swh/loader/mercurial/tasks.py b/swh/loader/mercurial/tasks.py --- a/swh/loader/mercurial/tasks.py +++ b/swh/loader/mercurial/tasks.py @@ -5,7 +5,7 @@ from swh.scheduler.task import Task -from .bundle20_loader import HgBundle20Loader, HgArchiveBundle20Loader +from .loader import HgBundle20Loader, HgArchiveBundle20Loader class LoadMercurialTsk(Task):