Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/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 | |||||
# control | |||||
import click | import click | ||||
import asyncio | |||||
import glob | |||||
import re | |||||
import fnmatch | |||||
from pathlib import PosixPath | from pathlib import PosixPath | ||||
from typing import Tuple | from typing import Tuple | ||||
from .scanner import run | |||||
from .model import Tree | |||||
from .plot import generate_sunburst | |||||
from .dashboard.dashboard import run_app | |||||
from .exceptions import InvalidDirectoryPath | |||||
from swh.core.cli import CONTEXT_SETTINGS | from swh.core.cli import CONTEXT_SETTINGS | ||||
@click.group(name="scanner", context_settings=CONTEXT_SETTINGS) | @click.group(name="scanner", context_settings=CONTEXT_SETTINGS) | ||||
@click.pass_context | @click.pass_context | ||||
def scanner(ctx): | def scanner(ctx): | ||||
"""Software Heritage Scanner tools.""" | """Software Heritage Scanner tools.""" | ||||
pass | pass | ||||
Show All 9 Lines | |||||
def extract_regex_objs(root_path: PosixPath, patterns: Tuple[str]) -> object: | def extract_regex_objs(root_path: PosixPath, patterns: Tuple[str]) -> object: | ||||
"""Generates a regex object for each pattern given in input and checks if | """Generates a regex object for each pattern given in input and checks if | ||||
the path is a subdirectory or relative to the root path. | the path is a subdirectory or relative to the root path. | ||||
Yields: | Yields: | ||||
an SRE_Pattern object | an SRE_Pattern object | ||||
""" | """ | ||||
import glob | |||||
import fnmatch | |||||
import re | |||||
from .exceptions import InvalidDirectoryPath | |||||
for pattern in patterns: | for pattern in patterns: | ||||
for path in glob.glob(pattern): | for path in glob.glob(pattern): | ||||
dirpath = PosixPath(path) | dirpath = PosixPath(path) | ||||
if root_path not in dirpath.parents: | if root_path not in dirpath.parents: | ||||
error_msg = ( | error_msg = ( | ||||
f'The path "{dirpath}" is not a subdirectory or relative ' | f'The path "{dirpath}" is not a subdirectory or relative ' | ||||
f'to the root directory path: "{root_path}"' | f'to the root directory path: "{root_path}"' | ||||
) | ) | ||||
Show All 31 Lines | |||||
) | ) | ||||
@click.option( | @click.option( | ||||
"-i", "--interactive", is_flag=True, help="show the result in a dashboard" | "-i", "--interactive", is_flag=True, help="show the result in a dashboard" | ||||
) | ) | ||||
@click.pass_context | @click.pass_context | ||||
def scan(ctx, root_path, api_url, patterns, format, interactive): | def scan(ctx, root_path, api_url, patterns, format, interactive): | ||||
"""Scan a source code project to discover files and directories already | """Scan a source code project to discover files and directories already | ||||
present in the archive""" | present in the archive""" | ||||
import asyncio | |||||
from .scanner import run | |||||
from .model import Tree | |||||
from .plot import generate_sunburst | |||||
from .dashboard.dashboard import run_app | |||||
sre_patterns = set() | sre_patterns = set() | ||||
if patterns: | if patterns: | ||||
sre_patterns = { | sre_patterns = { | ||||
reg_obj for reg_obj in extract_regex_objs(PosixPath(root_path), patterns) | reg_obj for reg_obj in extract_regex_objs(PosixPath(root_path), patterns) | ||||
} | } | ||||
api_url = parse_url(api_url) | api_url = parse_url(api_url) | ||||
source_tree = Tree(PosixPath(root_path)) | source_tree = Tree(PosixPath(root_path)) | ||||
Show All 14 Lines |