Skip to content

Commit 3bb87aa

Browse files
authored
feat: move output.BomRefDiscriminator to contrib.bom.utils.BomRefDiscriminator (#995)
no breaking changes - old symbols are still available, but deprecated. Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 150777e commit 3bb87aa

10 files changed

Lines changed: 202 additions & 39 deletions

File tree

cyclonedx/contrib/bom/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is part of CycloneDX Python Library
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
18+
"""Bom related functionality"""

cyclonedx/contrib/bom/utils.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# This file is part of CycloneDX Python Library
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
18+
"""Bom related utilities"""
19+
20+
__all__ = ['BomRefDiscriminator']
21+
22+
from collections.abc import Iterable
23+
from itertools import chain
24+
from random import random
25+
from typing import TYPE_CHECKING, Any
26+
27+
if TYPE_CHECKING: # pragma: no cover
28+
from ...model.bom import Bom
29+
from ...model.bom_ref import BomRef
30+
31+
32+
class BomRefDiscriminator:
33+
"""
34+
Ensure that a collection of BomRef objects has unique, non‑empty values.
35+
36+
The discriminator inspects the provided BomRef instances and assigns new,
37+
automatically generated identifiers to any BomRef whose value is missing
38+
or duplicates another. Original values are preserved so they can be
39+
restored later via `reset()` or by using this class as a context manager.
40+
"""
41+
42+
def __init__(self, bomrefs: Iterable['BomRef'], prefix: str = 'BomRef') -> None:
43+
# do not use dict/set here, different BomRefs with same value have same hash and would shadow each other
44+
self._bomrefs = tuple((bomref, bomref.value) for bomref in bomrefs)
45+
self._prefix = prefix
46+
47+
def __enter__(self) -> None:
48+
self.discriminate()
49+
50+
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
51+
self.reset()
52+
53+
def discriminate(self) -> None:
54+
"""
55+
Enforce uniqueness across all BomRef values.
56+
57+
Any BomRef whose value is `None` or duplicates a previously encountered
58+
value is assigned a newly generated unique identifier.
59+
"""
60+
known_values = []
61+
for bomref, _ in self._bomrefs:
62+
value = bomref.value
63+
if value is None or value in known_values:
64+
value = self._make_unique()
65+
bomref.value = value
66+
known_values.append(value)
67+
68+
def reset(self) -> None:
69+
"""
70+
Restore all BomRef values to their original state.
71+
"""
72+
for bomref, original_value in self._bomrefs:
73+
bomref.value = original_value
74+
75+
def _make_unique(self) -> str:
76+
return f'{self._prefix}{str(random())[1:]}{str(random())[1:]}' # nosec B311
77+
78+
@classmethod
79+
def from_bom(cls, bom: 'Bom', prefix: str = 'BomRef') -> 'BomRefDiscriminator':
80+
"""
81+
Create a discriminator for all BomRefs contained within a BOM.
82+
83+
This includes BomRefs from
84+
- components
85+
- services
86+
- vulnerabilities
87+
"""
88+
return cls(chain(
89+
map(lambda c: c.bom_ref, bom._get_all_components()),
90+
map(lambda s: s.bom_ref, bom.services),
91+
map(lambda v: v.bom_ref, bom.vulnerabilities)
92+
), prefix)

cyclonedx/output/__init__.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222
"""
2323

2424
import os
25+
import sys
2526
from abc import ABC, abstractmethod
26-
from collections.abc import Iterable, Mapping
27-
from itertools import chain
28-
from random import random
27+
from collections.abc import Mapping
2928
from typing import TYPE_CHECKING, Any, Literal, Optional, Union, overload
3029

30+
if sys.version_info >= (3, 13):
31+
from warnings import deprecated
32+
else:
33+
from typing_extensions import deprecated
34+
35+
from ..contrib.bom.utils import BomRefDiscriminator as _BomRefDiscriminator
3136
from ..schema import OutputFormat, SchemaVersion
3237

3338
if TYPE_CHECKING: # pragma: no cover
3439
from ..model.bom import Bom
35-
from ..model.bom_ref import BomRef
3640
from .json import Json as JsonOutputter
3741
from .xml import Xml as XmlOutputter
3842

@@ -138,40 +142,19 @@ def make_outputter(bom: 'Bom', output_format: OutputFormat, schema_version: Sche
138142
raise ValueError(f'Unknown {output_format.name}/schema_version: {schema_version!r}')
139143
return klass(bom)
140144

145+
# region deprecated re-export
141146

142-
class BomRefDiscriminator:
143-
144-
def __init__(self, bomrefs: Iterable['BomRef'], prefix: str = 'BomRef') -> None:
145-
# do not use dict/set here, different BomRefs with same value have same hash and would shadow each other
146-
self._bomrefs = tuple((bomref, bomref.value) for bomref in bomrefs)
147-
self._prefix = prefix
148-
149-
def __enter__(self) -> None:
150-
self.discriminate()
151147

152-
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
153-
self.reset()
148+
@deprecated('Deprecated re-export location - see docstring of "BomRefDiscriminator" for details.')
149+
class BomRefDiscriminator(_BomRefDiscriminator):
150+
"""Deprecated — Alias of :class:`cyclonedx.contrib.bom.utils.BomRefDiscriminator`.
154151
155-
def discriminate(self) -> None:
156-
known_values = []
157-
for bomref, _ in self._bomrefs:
158-
value = bomref.value
159-
if value is None or value in known_values:
160-
value = self._make_unique()
161-
bomref.value = value
162-
known_values.append(value)
163-
164-
def reset(self) -> None:
165-
for bomref, original_value in self._bomrefs:
166-
bomref.value = original_value
152+
.. deprecated:: next
153+
This re-export location is deprecated.
154+
Use ``from cyclonedx.contrib.bom.utils import BomRefDiscriminator`` instead.
155+
The exported symbol itself is NOT deprecated — only this import path.
156+
"""
157+
pass
167158

168-
def _make_unique(self) -> str:
169-
return f'{self._prefix}{str(random())[1:]}{str(random())[1:]}' # nosec B311
170159

171-
@classmethod
172-
def from_bom(cls, bom: 'Bom', prefix: str = 'BomRef') -> 'BomRefDiscriminator':
173-
return cls(chain(
174-
map(lambda c: c.bom_ref, bom._get_all_components()),
175-
map(lambda s: s.bom_ref, bom.services),
176-
map(lambda v: v.bom_ref, bom.vulnerabilities)
177-
), prefix)
160+
# endregion deprecated re-export

cyclonedx/output/json.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from json import dumps as json_dumps, loads as json_loads
2020
from typing import TYPE_CHECKING, Any, Literal, Optional, Union
2121

22+
from ..contrib.bom.utils import BomRefDiscriminator
2223
from ..exception.output import FormatNotSupportedException
2324
from ..schema import OutputFormat, SchemaVersion
2425
from ..schema.schema import (
@@ -33,7 +34,7 @@
3334
SchemaVersion1Dot6,
3435
SchemaVersion1Dot7,
3536
)
36-
from . import BaseOutput, BomRefDiscriminator
37+
from . import BaseOutput
3738

3839
if TYPE_CHECKING: # pragma: no cover
3940
from ..model.bom import Bom

cyclonedx/output/xml.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from xml.dom.minidom import parseString as dom_parseString # nosec B408
2121
from xml.etree.ElementTree import Element as XmlElement, tostring as xml_dumps # nosec B405
2222

23+
from ..contrib.bom.utils import BomRefDiscriminator
2324
from ..schema import OutputFormat, SchemaVersion
2425
from ..schema.schema import (
2526
SCHEMA_VERSIONS,
@@ -33,7 +34,7 @@
3334
SchemaVersion1Dot6,
3435
SchemaVersion1Dot7,
3536
)
36-
from . import BaseOutput, BomRefDiscriminator
37+
from . import BaseOutput
3738

3839
if TYPE_CHECKING: # pragma: no cover
3940
from ..model.bom import Bom

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from sortedcontainers import SortedSet
2727

28-
from cyclonedx.output import BomRefDiscriminator as _BomRefDiscriminator
28+
from cyclonedx.contrib.bom.utils import BomRefDiscriminator as _BomRefDiscriminator
2929
from cyclonedx.schema import OutputFormat, SchemaVersion
3030

3131
if TYPE_CHECKING:

tests/test_contrib/.gitkeep

Whitespace-only changes.

tests/test_contrib/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This file is part of CycloneDX Python Library
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This file is part of CycloneDX Python Library
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
18+
19+
from unittest import TestCase
20+
21+
from cyclonedx.contrib.bom.utils import BomRefDiscriminator
22+
from cyclonedx.model.bom_ref import BomRef
23+
24+
25+
class TestBomRefDiscriminator(TestCase):
26+
27+
def test_discriminate_and_reset_manually(self) -> None:
28+
bomref1 = BomRef('djdlkfjdslkf')
29+
bomref2 = BomRef('djdlkfjdslkf')
30+
self.assertEqual(bomref1.value, bomref2.value, 'blank')
31+
discr = BomRefDiscriminator([bomref1, bomref2])
32+
self.assertEqual(bomref1.value, bomref2.value, 'init')
33+
discr.discriminate()
34+
self.assertNotEqual(bomref1.value, bomref2.value, 'should be discriminated')
35+
discr.reset()
36+
self.assertEqual('djdlkfjdslkf', bomref1.value)
37+
self.assertEqual('djdlkfjdslkf', bomref2.value)
38+
39+
def test_discriminate_and_reset_with(self) -> None:
40+
bomref1 = BomRef('djdlkfjdslkf')
41+
bomref2 = BomRef('djdlkfjdslkf')
42+
self.assertEqual(bomref1.value, bomref2.value, 'blank')
43+
discr = BomRefDiscriminator([bomref1, bomref2])
44+
self.assertEqual(bomref1.value, bomref2.value, 'init')
45+
with discr:
46+
self.assertNotEqual(bomref1.value, bomref2.value, 'should be discriminated')
47+
self.assertEqual('djdlkfjdslkf', bomref1.value)
48+
self.assertEqual('djdlkfjdslkf', bomref2.value)

tests/test_output.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def test_fails_on_wrong_args(self, of: OutputFormat, sv: SchemaVersion, raises_r
5252

5353

5454
class TestBomRefDiscriminator(TestCase):
55+
"""
56+
System under test `BomRefDiscriminator` is a deprecated re-export.
57+
We keep the old tests untouched, to assert old behavior.
58+
"""
5559

5660
def test_discriminate_and_reset_with(self) -> None:
5761
bomref1 = BomRef('djdlkfjdslkf')

0 commit comments

Comments
 (0)