Skip to content

Commit

Permalink
extend run_flict command
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmehl committed Aug 13, 2024
1 parent 1011c64 commit 44ce397
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
31 changes: 23 additions & 8 deletions complassist/_flict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,42 @@

# 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:
"""Simplify a license expression using flict"""
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)

Expand All @@ -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
2 changes: 1 addition & 1 deletion complassist/_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 44ce397

Please sign in to comment.