Skip to content

Commit 3118acd

Browse files
authored
fix: ignore broken licenses in env parser (#463)
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 889a83e commit 3118acd

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

cyclonedx_py/parser/environment.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import sys
3232

33+
from cyclonedx.exception.model import CycloneDxModelException
34+
3335
# See https://github.com/package-url/packageurl-python/issues/65
3436
from packageurl import PackageURL # type: ignore
3537
from pkg_resources import DistInfoDistribution # type: ignore
@@ -70,22 +72,30 @@ def __init__(self, use_purl_bom_ref: bool = False) -> None:
7072
if 'Author' in i_metadata:
7173
c.author = i_metadata['Author']
7274

73-
if 'License' in i_metadata and i_metadata['License'] != 'UNKNOWN':
75+
if 'License' in i_metadata and i_metadata['License'] and i_metadata['License'] != 'UNKNOWN':
7476
# Values might be ala `MIT` (SPDX id), `Apache-2.0 license` (arbitrary string), ...
7577
# Therefore, just go with a named license.
76-
c.licenses.add(LicenseChoice(license_=License(license_name=i_metadata['License'])))
78+
try:
79+
c.licenses.add(LicenseChoice(license_=License(license_name=i_metadata['License'])))
80+
except CycloneDxModelException:
81+
# write a debug message?
82+
pass
7783

7884
for classifier in i_metadata.get_all("Classifier", []):
7985
# Trove classifiers - https://packaging.python.org/specifications/core-metadata/#metadata-classifier
8086
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
8187
if str(classifier).startswith('License :: OSI Approved :: '):
82-
c.licenses.add(LicenseChoice(license_=License(
83-
license_name=str(classifier).replace('License :: OSI Approved :: ', '').strip()
84-
)))
88+
license_name = str(classifier).replace('License :: OSI Approved :: ', '').strip()
8589
elif str(classifier).startswith('License :: '):
86-
c.licenses.add(LicenseChoice(license_=License(
87-
license_name=str(classifier).replace('License :: ', '').strip()
88-
)))
90+
license_name = str(classifier).replace('License :: ', '').strip()
91+
else:
92+
license_name = ''
93+
if license_name:
94+
try:
95+
c.licenses.add(LicenseChoice(license_=License(license_name=license_name)))
96+
except CycloneDxModelException:
97+
# write a debug message?
98+
pass
8999

90100
self._components.append(c)
91101

0 commit comments

Comments
 (0)