Skip to content

Commit 2c6020a

Browse files
authored
FIX: update Conda package parsing to handle build containing underscore (#66)
* fix: update conda package parsing to handle `build` containing underscore Signed-off-by: Paul Horton <[email protected]> * updated some typings Signed-off-by: Paul Horton <[email protected]>
1 parent b8acf9f commit 2c6020a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

cyclonedx/utils/conda.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CondaPackage(TypedDict):
3434
Internal package for unifying Conda package definitions to.
3535
"""
3636
base_url: str
37-
build_number: int
37+
build_number: Optional[int]
3838
build_string: str
3939
channel: str
4040
dist_name: str
@@ -96,13 +96,20 @@ def parse_conda_list_str_to_conda_package(conda_list_str: str) -> Optional[Conda
9696
pos = build_number_with_opt_string.find('.')
9797
build_number_with_opt_string = build_number_with_opt_string[0:pos]
9898

99+
build_string: str
100+
build_number: Optional[int]
101+
99102
if '_' in build_number_with_opt_string:
100103
bnbs_parts = build_number_with_opt_string.split('_')
101-
if len(bnbs_parts) == 2:
102-
build_number = int(bnbs_parts.pop())
104+
# Build number will be the last part - check if it's an integer
105+
# Updated logic given https://github.com/CycloneDX/cyclonedx-python-lib/issues/65
106+
candidate_build_number: str = bnbs_parts.pop()
107+
if candidate_build_number.isdigit():
108+
build_number = int(candidate_build_number)
103109
build_string = build_number_with_opt_string
104110
else:
105-
raise ValueError(f'Unexpected build version string for Conda Package: {conda_list_str}')
111+
build_number = None
112+
build_string = build_number_with_opt_string
106113
else:
107114
build_string = ''
108115
build_number = int(build_number_with_opt_string)

tests/test_utils_conda.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,20 @@ def test_parse_conda_list_str_with_hash_3(self) -> None:
107107
self.assertEqual(cp['platform'], 'noarch')
108108
self.assertEqual(cp['version'], '2.10')
109109
self.assertEqual(cp['md5_hash'], '153ff132f593ea80aae2eea61a629c92')
110+
111+
def test_parse_conda_list_str_with_hash_4(self) -> None:
112+
cp: CondaPackage = parse_conda_list_str_to_conda_package(
113+
conda_list_str='https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2'
114+
'#d7c89558ba9fa0495403155b64376d81'
115+
)
116+
117+
self.assertIsInstance(cp, dict)
118+
self.assertEqual(cp['base_url'], 'https://conda.anaconda.org/conda-forge')
119+
self.assertIsNone(cp['build_number'])
120+
self.assertEqual(cp['build_string'], 'conda_forge')
121+
self.assertEqual(cp['channel'], 'conda-forge')
122+
self.assertEqual(cp['dist_name'], '_libgcc_mutex-0.1-conda_forge')
123+
self.assertEqual(cp['name'], '_libgcc_mutex')
124+
self.assertEqual(cp['platform'], 'linux-64')
125+
self.assertEqual(cp['version'], '0.1')
126+
self.assertEqual(cp['md5_hash'], 'd7c89558ba9fa0495403155b64376d81')

0 commit comments

Comments
 (0)