diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f4dc64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +version.txt diff --git a/swh/scanner/cli.py b/swh/scanner/cli.py index 245a44a..8554210 100644 --- a/swh/scanner/cli.py +++ b/swh/scanner/cli.py @@ -1,56 +1,47 @@ # Copyright (C) 2020 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 click import asyncio -import os from pathlib import PosixPath -from urllib.parse import urlparse from .scanner import run -from .exceptions import InvalidPath -from .logger import setup_logger, log_counters from .model import Tree from swh.core.cli import CONTEXT_SETTINGS +@click.group(name='scanner', context_settings=CONTEXT_SETTINGS) +@click.pass_context +def scanner(ctx): + '''Software Heritage Scanner tools.''' + pass + def parse_url(url): - if url.port == 80: - return 'https://' + url.hostname - else: - return url.geturl() - - -@click.command(context_settings=CONTEXT_SETTINGS) -@click.argument('path', required=True) -@click.option('--host', '-h', default='localhost', - metavar='IP', show_default=True, - help="web api endpoint ip") -@click.option('--port', '-p', default='', - metavar='PORT', show_default=True, - help="web api endpoint port") -@click.option('--debug/--no-debug', default=True, - help="enable debug") -@click.option('--verbose', '-v', is_flag=True, default=False, - help="show debug information") -def scanner(path, host, port, debug, verbose): - """Software Heritage tool to scan the source code of a project""" - if not os.path.exists(path): - raise InvalidPath(path) - - if debug: - setup_logger(bool(verbose)) - - url = parse_url(urlparse('https://%s:%s' % (host, port))) - source_tree = Tree(None, PosixPath(path)) + if not url.startswith('http://') or not url.startswith('https://'): + url = 'https://' + url + if not url.endswith('/'): + url += '/' + return url + + +@scanner.command(name='scan') +@click.argument('path', required=True, type=click.Path(exists=True)) +@click.option('--api-url', default='archive.softwareheritage.org/api/1', + metavar='API_URL', show_default=True, + help="url for the api request") +@click.pass_context +def scan(ctx, path, api_url): + """Scan a source code project to discover files and directories already + present in the archive""" + api_url = parse_url(api_url) + source_tree = Tree(PosixPath(path)) loop = asyncio.get_event_loop() - loop.run_until_complete(run(path, url, source_tree)) + loop.run_until_complete(run(path, api_url, source_tree)) source_tree.show() - log_counters() if __name__ == '__main__': - scanner() + scan() diff --git a/swh/scanner/exceptions.py b/swh/scanner/exceptions.py index 5c8d6d8..952cad7 100644 --- a/swh/scanner/exceptions.py +++ b/swh/scanner/exceptions.py @@ -1,8 +1,9 @@ -class InvalidPath(Exception): - def __str__(self): - return 'the provided path is invalid: "%s"' % self.args +# Copyright (C) 2020 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 class APIError(Exception): def __str__(self): return 'API Error: "%s"' % self.args diff --git a/version.txt b/version.txt deleted file mode 100644 index 2bc1fa1..0000000 --- a/version.txt +++ /dev/null @@ -1 +0,0 @@ -v0.0.1-0-gdd03798 \ No newline at end of file