Skip to content

Commit 23f8e10

Browse files
authored
Merge branch 'main' into main
2 parents 98db7ce + ebb168a commit 23f8e10

135 files changed

Lines changed: 11474 additions & 71 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ jobs:
140140
architecture: 'x64'
141141
- name: Install and configure Poetry
142142
# Seehttps://github.com/snok/install-poetry
143-
uses: snok/install-poetry@76e04a911780d5b312d89783f7b1cd627778900a # v1.4.1
143+
uses: snok/install-poetry@a783c322200f0519c7926aa6faa857c4e23e9263 # v1.4.2
144144
with:
145145
version: ${{ env.POETRY_VERSION }}
146146
virtualenvs-create: true

CHANGELOG.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,50 @@
22

33
<!-- version list -->
44

5+
## v11.10.0 (2026-06-11)
6+
7+
### Bug Fixes
8+
9+
- Lossless flattening of dependency graph during JSON serialization
10+
([#993](https://github.com/CycloneDX/cyclonedx-python-lib/pull/993),
11+
[`d0e10ca`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/d0e10ca147dfece8bd7c76b104d7579255879679))
12+
13+
- Typing in `contrib.bom.utils.BomDependencyGraphFlatMerger`
14+
([#998](https://github.com/CycloneDX/cyclonedx-python-lib/pull/998),
15+
[`988a937`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/988a9372a676358b29e67573c9f39d94121824bc))
16+
17+
### Documentation
18+
19+
- Improve docs of `contrib.bom.utils.BomRefDiscriminator`
20+
([#996](https://github.com/CycloneDX/cyclonedx-python-lib/pull/996),
21+
[`9beaf5c`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/9beaf5c6b35a8739727572c0e351063849d876fc))
22+
23+
### Features
24+
25+
- Add `contrib.bom.utils.BomDependencyGraphFlatMerger`
26+
([#997](https://github.com/CycloneDX/cyclonedx-python-lib/pull/997),
27+
[`78b8d8b`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/78b8d8b94b26df14d0194b3d44a5db8a76f1fa47))
28+
29+
- Move `output.BomRefDiscriminator` to `contrib.bom.utils.BomRefDiscriminator`
30+
([#995](https://github.com/CycloneDX/cyclonedx-python-lib/pull/995),
31+
[`3bb87aa`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/3bb87aabbc421aabac307305217c7975e63b43eb))
32+
33+
### Performance Improvements
34+
35+
- `contrib.bom.utils.bomdependencygraphflatmerger._flatten_merge`
36+
([#999](https://github.com/CycloneDX/cyclonedx-python-lib/pull/999),
37+
[`a8579b8`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/a8579b87f7e121ebbec16b9f05bdb3b4de11e71c))
38+
39+
40+
## v11.9.0 (2026-06-08)
41+
42+
### Features
43+
44+
- Add support for license expression details
45+
([#908](https://github.com/CycloneDX/cyclonedx-python-lib/pull/908),
46+
[`b502381`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/b50238102553dc215b08796ea914072294f69489))
47+
48+
549
## v11.8.0 (2026-06-04)
650

751
### Documentation

cyclonedx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222

2323
# !! version is managed by semantic_release
2424
# do not use typing here, or else `semantic_release` might have issues finding the variable
25-
__version__ = "11.8.0" # noqa:Q000
25+
__version__ = "11.10.0" # noqa:Q000

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: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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', 'BomDependencyGraphFlatMerger']
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+
from ...model.dependency import Dependency
28+
29+
if TYPE_CHECKING: # pragma: no cover
30+
from ...model.bom import Bom
31+
from ...model.bom_ref import BomRef
32+
33+
34+
class BomRefDiscriminator:
35+
"""
36+
Ensure that a collection of BomRef objects
37+
has unique, non‑empty :attr:`cyclonedx.model.bom_ref.BomRef.value`.
38+
39+
The discriminator inspects each provided BomRef and assigns a newly
40+
generated identifier to any instance whose ``value`` is missing or
41+
duplicates an earlier one.
42+
All original values are preserved and can be restored via :meth:`reset()`
43+
or by using this class as a context manager.
44+
"""
45+
46+
def __init__(self, bomrefs: Iterable['BomRef'], prefix: str = 'BomRef') -> None:
47+
# NOTE: do not use dict/set here, different BomRefs with same value
48+
# have same hash and would shadow each other.
49+
self._bomrefs = tuple((bomref, bomref.value) for bomref in bomrefs)
50+
self._prefix = prefix
51+
52+
def __enter__(self) -> None:
53+
self.discriminate()
54+
55+
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
56+
self.reset()
57+
58+
def discriminate(self) -> None:
59+
"""
60+
Enforce uniqueness across all
61+
:attr:`cyclonedx.model.bom_ref.BomRef.value`s.
62+
63+
Any BomRef whose ``value`` is ``None`` or duplicates a previously
64+
encountered value is assigned a newly generated unique identifier.
65+
"""
66+
known_values = []
67+
for bomref, _ in self._bomrefs:
68+
value = bomref.value
69+
if value is None or value in known_values:
70+
value = self._make_unique()
71+
bomref.value = value
72+
known_values.append(value)
73+
74+
def reset(self) -> None:
75+
"""
76+
Restore all :attr:`cyclonedx.model.bom_ref.BomRef.value`s to
77+
their original state.
78+
"""
79+
for bomref, original_value in self._bomrefs:
80+
bomref.value = original_value
81+
82+
def _make_unique(self) -> str:
83+
return f'{self._prefix}{str(random())[1:]}{str(random())[1:]}' # nosec B311
84+
85+
@classmethod
86+
def from_bom(cls, bom: 'Bom', prefix: str = 'BomRef') -> 'BomRefDiscriminator':
87+
"""
88+
Create a discriminator for all :class:`cyclonedx.model.bom_ref.BomRefs`
89+
contained within a Bom.
90+
91+
This includes BomRefs from
92+
* :attr:`cyclonedx.model.bom.Bom.components`
93+
* :attr:`cyclonedx.model.bom.Bom.services`
94+
* :attr:`cyclonedx.model.bom.Bom.vulnerabilities`
95+
"""
96+
return cls(chain(
97+
map(lambda c: c.bom_ref, bom._get_all_components()),
98+
map(lambda s: s.bom_ref, bom.services),
99+
map(lambda v: v.bom_ref, bom.vulnerabilities)
100+
), prefix)
101+
102+
103+
class BomDependencyGraphFlatMerger:
104+
"""
105+
Context‑manager utility that temporarily flattens and merges all
106+
:attr:`cyclonedx.model.bom.Bom.dependencies`.
107+
108+
When used as a context manager, the :class:`cyclonedx.model.bom.Bom`'s
109+
dependency graph is replaced with a flattened, merged representation
110+
for the duration of the ``with`` block and automatically restored
111+
afterward.
112+
"""
113+
114+
def __init__(self, bom: 'Bom') -> None:
115+
self._bom = bom
116+
# NOTE: do not use the getter - see `reset()` for reasons.
117+
self._deps = self._bom._dependencies
118+
119+
def __enter__(self) -> None:
120+
self.flatten_merge()
121+
122+
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
123+
self.reset()
124+
125+
def flatten_merge(self) -> None:
126+
"""
127+
Flatten and merge all :attr:`cyclonedx.model.bom.Bom.dependencies`.
128+
129+
This produces a non‑recursive, merged representation of the entire
130+
dependency graph and assigns it to the Bom.
131+
132+
.. note::
133+
The original dependency graph is not modified. A new, flattened
134+
dependency structure is assigned to the Bom.
135+
"""
136+
self._bom.dependencies = self._flatten_merge(self._deps)
137+
138+
def reset(self) -> None:
139+
"""
140+
Restore the :class:`cyclonedx.model.bom.Bom`'s dependency graph to
141+
its original state.
142+
143+
.. note::
144+
This does not modify the dependency graph. It simply reassigns
145+
the original dependency collection back to the Bom.
146+
"""
147+
# NOTE: not using the setter, which would create overhead,
148+
# and - most importantly - this could cause deduplication of an existing malformed set.
149+
# Just access the internal field directly!
150+
self._bom._dependencies = self._deps
151+
152+
@staticmethod
153+
def _flatten_merge(deps: Iterable[Dependency]) -> Iterable[Dependency]:
154+
flat: dict['BomRef', list['BomRef']] = {}
155+
todos = list(deps)
156+
seen: set[int] = set()
157+
while todos:
158+
todo = todos.pop()
159+
if (todo_id := id(todo)) in seen:
160+
continue
161+
seen.add(todo_id)
162+
ds = flat.setdefault(todo.ref, [])
163+
if todo_deps := todo.dependencies:
164+
ds.extend(d.ref for d in todo_deps)
165+
todos.extend(todo_deps)
166+
return (
167+
Dependency(br, (Dependency(d) for d in ds))
168+
for br, ds
169+
in flat.items()
170+
)

0 commit comments

Comments
 (0)