Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly declare licenses from environment #417

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a SPDX license expression like Mozilla Public License 1.0 (MPL) was NEVER valid.

)
)
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)