Skip to content

Commit 8e797d6

Browse files
committed
fix
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent d740913 commit 8e797d6

File tree

51 files changed

+6669
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+6669
-11
lines changed

cyclonedx_py/_internal/environment.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Tuple
2828

2929
from cyclonedx.model import Property
30-
from cyclonedx.model.component import Component, ComponentType
30+
from cyclonedx.model.component import Component, ComponentEvidence, ComponentType
3131
from packageurl import PackageURL
3232
from packaging.requirements import Requirement
3333

3434
from . import BomBuilder, PropertyName, PurlTypePypi
3535
from .cli_common import add_argument_mc_type, add_argument_pyproject
36-
from .utils.cdx import licenses_fixup, make_bom
36+
from .utils.cdx import find_LicenseExpression, licenses_fixup, make_bom
3737
from .utils.packaging import metadata2extrefs, metadata2licenses, normalize_packagename
3838
from .utils.pep610 import PackageSourceArchive, PackageSourceVcs, packagesource2extref, packagesource4dist
3939
from .utils.pep639 import dist2licenses as dist2licenses_pep639
@@ -183,10 +183,21 @@ def __add_components(self, bom: 'Bom',
183183
# path of dist-package on disc? naaa... a package may have multiple files/folders on disc
184184
)
185185
if self._pep639:
186-
component.licenses.update(
187-
dist2licenses_pep639(dist,
188-
self._gather_license_texts,
189-
self._logger))
186+
pep639_licenses = list(dist2licenses_pep639(dist, self._gather_license_texts, self._logger))
187+
pep639_lexp = find_LicenseExpression(pep639_licenses)
188+
if pep639_lexp is not None:
189+
component.licenses = (pep639_lexp,)
190+
pep639_licenses.remove(pep639_lexp)
191+
if len(pep639_licenses) > 0:
192+
if find_LicenseExpression(component.licenses) is None:
193+
component.licenses.update(pep639_licenses)
194+
else:
195+
# hack for preventing expressions AND named licenses.
196+
# see https://github.com/CycloneDX/cyclonedx-python/issues/826
197+
# see https://github.com/CycloneDX/specification/issues/454
198+
component.evidence = ComponentEvidence(licenses=pep639_licenses)
199+
del pep639_lexp, pep639_licenses
200+
190201
del dist_meta, dist_name, dist_version
191202
self.__component_add_extref_and_purl(component, packagesource4dist(dist))
192203
all_components[normalize_packagename(component.name)] = (

cyclonedx_py/_internal/utils/cdx.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"""
2222

2323
from re import compile as re_compile
24-
from typing import Any, Dict, Iterable
24+
from typing import Any, Dict, Iterable, Optional
2525

2626
from cyclonedx.builder.this import this_component as lib_component
2727
from cyclonedx.model import ExternalReference, ExternalReferenceType, XsUri
@@ -87,11 +87,17 @@ def make_bom(**kwargs: Any) -> Bom:
8787
return bom
8888

8989

90-
def licenses_fixup(licenses: Iterable['License']) -> Iterable['License']:
91-
licenses = set(licenses)
90+
def find_LicenseExpression(licenses: Iterable['License']) -> Optional[LicenseExpression]:
9291
for license in licenses:
9392
if isinstance(license, LicenseExpression):
94-
return (license,)
93+
return license
94+
return None
95+
96+
97+
def licenses_fixup(licenses: Iterable['License']) -> Iterable['License']:
98+
licenses = set(licenses)
99+
if (lexp := find_LicenseExpression(licenses)) is not None:
100+
return (lexp,)
95101
return licenses
96102

97103

tests/_data/infiles/environment/with-license-pep639/init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def main() -> None:
7070
'jsonpointer',
7171
'license_expression',
7272
'lxml',
73-
# with License-Expression AND License-File
73+
# with expression as License AND License-File
7474
'cryptography==43.0.1', # see https://github.com/CycloneDX/cyclonedx-python/issues/826
7575
)
7676

tests/_data/snapshots/environment/pep639-texts_with-license-pep639_1.0.xml.bin

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/snapshots/environment/pep639-texts_with-license-pep639_1.1.xml.bin

Lines changed: 153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)