diff --git a/swh/scanner/cli.py b/swh/scanner/cli.py --- a/swh/scanner/cli.py +++ b/swh/scanner/cli.py @@ -30,20 +30,26 @@ @scanner.command(name='scan') @click.argument('path', required=True, type=click.Path(exists=True)) -@click.option('--api-url', +@click.option('-u', '--api-url', default='https://archive.softwareheritage.org/api/1', metavar='API_URL', show_default=True, help="url for the api request") +@click.option('-j', '--json-file', type=str, + help="output result to json file") @click.pass_context -def scan(ctx, path, api_url): +def scan(ctx, path, api_url, json_file): """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, api_url, source_tree)) + source_tree.show() + if json_file: + source_tree.writeToJson(json_file) + if __name__ == '__main__': scan() diff --git a/swh/scanner/model.py b/swh/scanner/model.py --- a/swh/scanner/model.py +++ b/swh/scanner/model.py @@ -5,8 +5,9 @@ from __future__ import annotations import sys +import json from pathlib import PosixPath -from typing import Any, Dict +from typing import Dict from enum import Enum from swh.model.identifiers import ( @@ -35,7 +36,7 @@ self.pid = '' self.children: Dict[PosixPath, Tree] = {} - def addNode(self, path: PosixPath, pid: str = None) -> None: + def addNode(self, path: PosixPath, pid: str = '') -> None: """Recursively add a new node path """ relative_path = path.relative_to(self.path) @@ -65,20 +66,34 @@ if node.children: node.printChildren(isatty, inc+1) - def printNode(self, node: Any, isatty: bool, inc: int) -> None: + def printNode(self, node: Tree, isatty: bool, inc: int) -> None: rel_path = str(node.path.relative_to(self.path)) - print('│ '*inc, end='') - if node.otype == DIRECTORY: - if node.pid: - print(colorize(rel_path, Color.blue) if isatty else rel_path, - end='') + begin = '│ ' * inc + end = '/' if node.otype == DIRECTORY else '' + + if isatty: + if not node.pid: + rel_path = colorize(rel_path, Color.red) + elif node.otype == DIRECTORY: + rel_path = colorize(rel_path, Color.blue) + elif node.otype == CONTENT: + rel_path = colorize(rel_path, Color.green) + + print(f'{begin}{rel_path}{end}') + + def getJsonChild(self): + child_dir = {} + for path, child_node in self.children.items(): + rel_path = str(child_node.path.relative_to(self.path)) + if child_node.pid: + child_dir[rel_path] = child_node.pid else: - print(colorize(rel_path, Color.red) if isatty else rel_path, - end='') - print('/') + next_dir = child_node.getJsonChild() + if next_dir: + child_dir[rel_path] = child_node.getJsonChild() - elif node.otype == CONTENT: - if node.pid: - print(colorize(rel_path, Color.green) if isatty else rel_path) - else: - print(colorize(rel_path, Color.red) if isatty else rel_path) + return child_dir + + def writeToJson(self, filename): + with open(filename, 'w') as json_file: + json.dump(self.getJsonChild(), json_file)