Skip to content

Commit 4ab075e

Browse files
authored
fix(EnvironmentParser): remove code break when classifier parsing in py>=3.8 (#431)
Signed-off-by: a1lu <github.foreshoe@slmail.me>
1 parent ddea61e commit 4ab075e

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

cyclonedx_py/parser/environment.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,17 @@ def __init__(self, use_purl_bom_ref: bool = False) -> None:
7575
# Therefore, just go with a named license.
7676
c.licenses.add(LicenseChoice(license_=License(license_name=i_metadata['License'])))
7777

78-
if 'Classifier' in i_metadata:
79-
for classifier in i_metadata['Classifier']:
80-
# Trove classifiers - https://packaging.python.org/specifications/core-metadata/#metadata-classifier
81-
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
82-
if str(classifier).startswith('License :: OSI Approved :: '):
83-
c.licenses.add(LicenseChoice(license_=License(
84-
license_name=str(classifier).replace('License :: OSI Approved :: ', '').strip()
85-
)))
86-
elif str(classifier).startswith('License :: '):
87-
c.licenses.add(LicenseChoice(license_=License(
88-
license_name=str(classifier).replace('License :: ', '').strip()
89-
)))
78+
for classifier in i_metadata.get_all("Classifier"):
79+
# Trove classifiers - https://packaging.python.org/specifications/core-metadata/#metadata-classifier
80+
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
81+
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+
)))
85+
elif str(classifier).startswith('License :: '):
86+
c.licenses.add(LicenseChoice(license_=License(
87+
license_name=str(classifier).replace('License :: ', '').strip()
88+
)))
9089

9190
self._components.append(c)
9291

tests/test_parser_environment.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
from unittest import TestCase
2121

22+
from cyclonedx.model import License, LicenseChoice
23+
2224
from cyclonedx_py.parser.environment import EnvironmentParser
2325

2426

@@ -39,7 +41,9 @@ def test_simple(self) -> None:
3941
self.assertIsNotNone(c_tox)
4042
self.assertNotEqual(c_tox.purl.to_string(), c_tox.bom_ref.value)
4143
self.assertIsNotNone(c_tox.licenses)
42-
self.assertEqual('MIT', c_tox.licenses.pop().license.name)
44+
self.assertEqual(len(c_tox.licenses), 2)
45+
self.assertEqual({LicenseChoice(license_=License(license_name="MIT License")),
46+
LicenseChoice(license_=License(license_name="MIT"))}, c_tox.licenses)
4347

4448
def test_simple_use_purl_bom_ref(self) -> None:
4549
"""
@@ -56,4 +60,6 @@ def test_simple_use_purl_bom_ref(self) -> None:
5660
self.assertIsNotNone(c_tox)
5761
self.assertEqual(c_tox.purl.to_string(), c_tox.bom_ref.value)
5862
self.assertIsNotNone(c_tox.licenses)
59-
self.assertEqual('MIT', c_tox.licenses.pop().license.name)
63+
self.assertEqual(len(c_tox.licenses), 2)
64+
self.assertEqual({LicenseChoice(license_=License(license_name="MIT License")),
65+
LicenseChoice(license_=License(license_name="MIT"))}, c_tox.licenses)

0 commit comments

Comments
 (0)