diff --git a/license_sh/commands/run_license_sh.py b/license_sh/commands/run_license_sh.py index 6850457..491b131 100644 --- a/license_sh/commands/run_license_sh.py +++ b/license_sh/commands/run_license_sh.py @@ -1,15 +1,12 @@ import questionary - +import sys from . import config_cmd from ..config import get_config, whitelist_licenses from ..helpers import get_dependency_tree_with_licenses from ..project_identifier import ProjectType, get_project_types from ..reporters.ConsoleReporter import ConsoleReporter from ..reporters.JSONConsoleReporter import JSONConsoleReporter -from ..runners.maven import MavenRunner -from ..runners.npm import NpmRunner -from ..runners.python import PythonRunner -from ..runners.yarn import YarnRunner +from ..runners import run_check try: from license_sh_private.licenses import COMMERCIAL_LICENSES as WHITELIST @@ -45,30 +42,29 @@ def run_license_sh(arguments): # docopt guarantees that output variable contains either console or json Reporter = {"console": ConsoleReporter, "json": JSONConsoleReporter}[output] - project_types = get_project_types(path) ignored_packages = [] - - dep_tree = None - - if ProjectType.PYTHON_PIPENV in project_types: - runner = PythonRunner(path, silent, debug) - dep_tree, license_map = runner.check() - ignored_packages = ignored_packages_map[ProjectType.PYTHON_PIPENV.value] - - if ProjectType.NPM in project_types: - runner = NpmRunner(path, silent, debug) - dep_tree, license_map = runner.check() - ignored_packages = ignored_packages_map[ProjectType.NPM.value] - - if ProjectType.MAVEN in project_types: - runner = MavenRunner(path, silent, debug) - dep_tree, license_map = runner.check() - ignored_packages = ignored_packages_map[ProjectType.MAVEN.value] - - if ProjectType.YARN in project_types: - runner = YarnRunner(path, silent, debug) - dep_tree, license_map = runner.check() - ignored_packages = ignored_packages_map[ProjectType.YARN.value] + project_list = [e.value for e in get_project_types(path)] + + if len(project_list) == 0: + supported_projects = [e.value for e in ProjectType] + print( + f"None of currently supported projects found. Supported {supported_projects}", + file=sys.stderr, + ) + exit(2) + + if len(project_list) > 1: + print( + f"Curretly there is no support for multi project/language repositories. Found {project_types}. Only '{project_types[0]}' will be checked.", + file=sys.stderr, + ) + + project_to_check = project_list[0] + dep_tree = run_check(project_to_check, path, silent, debug) + ignored_packages = ignored_packages_map.get(project_to_check, []) + if not dep_tree: + print(f"Unexpected issue, couldn't create dependency tree", file=sys.stderr) + exit(3) ( filtered_dep_tree, diff --git a/license_sh/runners/__init__.py b/license_sh/runners/__init__.py index e69de29..9b4f2b4 100644 --- a/license_sh/runners/__init__.py +++ b/license_sh/runners/__init__.py @@ -0,0 +1,20 @@ +from ..runners.maven import MavenRunner +from ..runners.npm import NpmRunner +from ..runners.python import PythonRunner +from ..runners.yarn import YarnRunner +from ..project_identifier import ProjectType + +RUNNERS = { + ProjectType.PYTHON_PIPENV.value: PythonRunner, + ProjectType.YARN.value: YarnRunner, + ProjectType.NPM.value: NpmRunner, + ProjectType.MAVEN.value: MavenRunner, +} + + +def run_check(project_to_check: str, path: str, silent: bool, debug: bool): + runner = RUNNERS.get(project_to_check) + if not runner: + return None + + return runner(path, silent, debug).check() diff --git a/license_sh/runners/maven/__init__.py b/license_sh/runners/maven/__init__.py index c61906f..4ad6046 100644 --- a/license_sh/runners/maven/__init__.py +++ b/license_sh/runners/maven/__init__.py @@ -221,4 +221,4 @@ def check(self): for node in PreOrderIter(dep_tree): node.license = license_map.get(f"{node.name}@{node.version}", "") - return dep_tree, license_map + return dep_tree diff --git a/license_sh/runners/npm/__init__.py b/license_sh/runners/npm/__init__.py index fb1c505..2e1bc40 100644 --- a/license_sh/runners/npm/__init__.py +++ b/license_sh/runners/npm/__init__.py @@ -150,4 +150,4 @@ def check(self): hasattr(node, "version_request") and delattr(node, "version_request") node.license = license_map.get(f"{node.name}@{node.version}", None) - return dep_tree, license_map + return dep_tree diff --git a/license_sh/runners/python/__init__.py b/license_sh/runners/python/__init__.py index 3cf5bf8..1191930 100644 --- a/license_sh/runners/python/__init__.py +++ b/license_sh/runners/python/__init__.py @@ -105,4 +105,4 @@ def check(self): for node in PreOrderIter(root): node.license = license_map.get(f"{node.name}@{node.version}", None) - return root, license_map + return root diff --git a/license_sh/runners/yarn/__init__.py b/license_sh/runners/yarn/__init__.py index c24bbf7..cc073f2 100644 --- a/license_sh/runners/yarn/__init__.py +++ b/license_sh/runners/yarn/__init__.py @@ -346,4 +346,4 @@ def check(self): for node in PreOrderIter(dep_tree): node.license = license_map.get(f"{node.name}@{node.version}", None) - return dep_tree, license_map + return dep_tree