From 44ce397d528f090670c48ba1536023926873cd27 Mon Sep 17 00:00:00 2001 From: Max Mehl Date: Tue, 13 Aug 2024 10:18:59 +0200 Subject: [PATCH] extend run_flict command --- complassist/_flict.py | 31 +++++++++++++++++++++++-------- complassist/_licensing.py | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/complassist/_flict.py b/complassist/_flict.py index 3dea59b..fab8e75 100644 --- a/complassist/_flict.py +++ b/complassist/_flict.py @@ -10,22 +10,34 @@ # We need to run flict as subprocess as usage as library is too complicated def _run_flict( - command: str, *arguments, options: list | None = None, warn_on_error: bool = True -) -> str: + command: str, + *arguments, + options: list | None = None, + warn_on_error: bool = True, +) -> tuple[int, str, str]: """ Run flict with a command (e.g. 'verify') and a list of arguments (e.g. '-il', 'GPL-2.0-only', '-ol', 'MIT'), and a list of general options (e.g. ["-ip"]) - Return output as str + Return: exit code, stdout, stderr """ if options is None: options = [] cmd = ["flict", *options, command, *arguments] + logging.debug("Running flict: %s", cmd) ret = subprocess.run(cmd, capture_output=True, check=False) - if ret.returncode != 0: + code = ret.returncode + stderr = ret.stderr.decode("UTF-8").strip() + stdout = ret.stdout.decode("UTF-8").strip() + if code != 0: + # If only warning requested, only log error, return normal output if warn_on_error: - logging.warning("flict exited with an error (%s): %s", ret.returncode, ret.stderr) + logging.warning( + "flict exited with an error (%s): %s", + code, + stderr, + ) - return ret.stdout.decode("UTF-8").strip() + return code, stdout, stderr def flict_simplify(expression: str, output_format: str, no_relicensing: bool = True) -> str: @@ -33,7 +45,7 @@ def flict_simplify(expression: str, output_format: str, no_relicensing: bool = T options = ["-of", output_format] if no_relicensing: options.append("-nr") - simplified = _run_flict("simplify", expression, options=options) + _, simplified, _ = _run_flict("simplify", expression, options=options) logging.debug("Simplified '%s' to '%s' using flict", expression, simplified) @@ -53,4 +65,7 @@ def flict_outbound_candidate(expression: str, output_format: str) -> str: """Get possible outbound license candidates using flict""" # TODO: `-el` would make this command more helpful but it has an error: # https://github.com/vinland-technology/flict/issues/391 - return _run_flict("outbound-candidate", expression, options=["-nr", "-of", output_format]) + _, outbound_candidate, _ = _run_flict( + "outbound-candidate", expression, options=["-nr", "-of", output_format] + ) + return outbound_candidate diff --git a/complassist/_licensing.py b/complassist/_licensing.py index 3cd3643..d468c80 100644 --- a/complassist/_licensing.py +++ b/complassist/_licensing.py @@ -83,7 +83,7 @@ def _craft_single_spdx_expression(licenses: list[str]): return " AND ".join(licenses) -def get_outbound_candidate(sbom_path: str, simplify: bool = True) -> dict[str, str]: +def get_outbound_candidate(sbom_path: str, simplify: bool = True) -> dict[str, str | list[str]]: """Get license outbound candidates from an SBOM""" licenses_in_sbom = list_all_licenses(sbom_path, use_flict=simplify)