Skip to content

Commit

Permalink
fix: properly declare licenses from environment
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Sep 19, 2022
1 parent 049a5b3 commit e1ce8d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 13 additions & 7 deletions cyclonedx_py/parser/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
else:
from importlib_metadata import metadata, PackageMetadata as _MetadataReturn

from cyclonedx.model import LicenseChoice
from cyclonedx.model import License, LicenseChoice
from cyclonedx.model.component import Component
from cyclonedx.parser import BaseParser

Expand Down Expand Up @@ -71,16 +71,22 @@ def __init__(self, use_purl_bom_ref: bool = False) -> None:
c.author = i_metadata['Author']

if 'License' in i_metadata and i_metadata['License'] != 'UNKNOWN':
c.licenses.add(LicenseChoice(license_expression=i_metadata['License']))
# Values might be ala `MIT` (SPDX id), `Apache-2.0 license` (arbitrary string), ...
# Therefore, just go with a named license.
c.licenses.add(LicenseChoice(license_=License(license_name=i_metadata['License'])))

if 'Classifier' in i_metadata:
for classifier in i_metadata['Classifier']:
# Trove classifiers - https://packaging.python.org/specifications/core-metadata/#metadata-classifier
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
if str(classifier).startswith('License :: OSI Approved :: '):
c.licenses.add(
LicenseChoice(
license_expression=str(classifier).replace('License :: OSI Approved :: ', '').strip()
)
)
c.licenses.add(LicenseChoice(license_=License(
license_name=str(classifier).replace('License :: OSI Approved :: ', '').strip()
)))
elif str(classifier).startswith('License :: '):
c.licenses.add(LicenseChoice(license_=License(
license_name=str(classifier).replace('License :: ', '').strip()
)))

self._components.append(c)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_parser_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_simple(self) -> None:
self.assertIsNotNone(c_tox)
self.assertNotEqual(c_tox.purl.to_string(), c_tox.bom_ref.value)
self.assertIsNotNone(c_tox.licenses)
self.assertEqual('MIT', c_tox.licenses.pop().expression)
self.assertEqual('MIT', c_tox.licenses.pop().license.name)

def test_simple_use_purl_bom_ref(self) -> None:
"""
Expand All @@ -56,4 +56,4 @@ def test_simple_use_purl_bom_ref(self) -> None:
self.assertIsNotNone(c_tox)
self.assertEqual(c_tox.purl.to_string(), c_tox.bom_ref.value)
self.assertIsNotNone(c_tox.licenses)
self.assertEqual('MIT', c_tox.licenses.pop().expression)
self.assertEqual('MIT', c_tox.licenses.pop().license.name)

0 comments on commit e1ce8d6

Please sign in to comment.