sbom/spdx: use compliant license expressions
Introduce a new class `SpdxLicenseExpressionCreator`, responsible for converting license texts extracted from packages, into an SPDX-compliant license expressions. If the `license_expression` Python package is available on the system, it is used to determine the license text extracted from a package is a valid SPDX license expression. If it is, it's returned as is back to the caller. If it is not, or of the package is not available on the system, the license text is wrapped in a `ExtractedLicensingInfo` instance. The `SpdxLicenseExpressionCreator` object keeps track of all generated `ExtractedLicensingInfo` instances and de-duplicates them based on the license text. This means that if two packages use the same SPDX-non-compliant license text, they will be wrapped by an `ExtractedLicensingInfo` instance with the same `LicenseRef-` ID. The reason for fallback when `license_expression` package is not available is that it is not available on RHEL and CentOS Stream. This implementation allows us to ship the functionality in RHEL and optionally enabling it by installing `license_expression` from a 3rd party repository. In any case, the generated SBOM document will always contain valid SPDX license expressions. Extend unit tests to cover the newly added functionality. Signed-off-by: Tomáš Hozza <thozza@redhat.com> FIXUP: sbom/spdx: use compliant license expressions Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
parent
0174173175
commit
a3428e282d
5 changed files with 204 additions and 35 deletions
40
test/test.py
40
test/test.py
|
|
@ -10,6 +10,9 @@ import subprocess
|
|||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
import osbuild.meta
|
||||
from osbuild.objectstore import ObjectStore
|
||||
|
|
@ -496,3 +499,40 @@ class OSBuild(contextlib.AbstractContextManager):
|
|||
"cp", "--reflink=auto", "-a",
|
||||
os.path.join(from_path, "."), to_path
|
||||
], check=True)
|
||||
|
||||
|
||||
class patch_license_expression:
|
||||
"""
|
||||
Context manager to patch the license-expression package availability.
|
||||
|
||||
The context manager simulates the unavailability of the license-expression package by mocking the
|
||||
`get_spdx_licensing()` module-level function. If the package should be made available
|
||||
and it is available on the system, the function is passed through. Otherwise, pytest.skip() is called.
|
||||
"""
|
||||
|
||||
PATCH_TARGET = "osbuild.util.sbom.spdx.get_spdx_licensing"
|
||||
|
||||
def __init__(self, make_package_available):
|
||||
self.make_package_available = make_package_available
|
||||
self.patcher = None
|
||||
|
||||
def __enter__(self):
|
||||
get_spdx_licensing = None
|
||||
try:
|
||||
# pylint: disable=import-outside-toplevel
|
||||
from license_expression import get_spdx_licensing
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
if self.make_package_available:
|
||||
if get_spdx_licensing:
|
||||
self.patcher = patch(self.PATCH_TARGET, new=get_spdx_licensing)
|
||||
else:
|
||||
pytest.skip("The license-expression package is not available.")
|
||||
else:
|
||||
# The package is either not available or should be made unavailable, so make sure the function var is None.
|
||||
self.patcher = patch(self.PATCH_TARGET, new=None)
|
||||
return self.patcher.start()
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.patcher.stop()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue