Skip to content

Commit 3af721b

Browse files
committed
refactor
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent e1b4ab9 commit 3af721b

3 files changed

Lines changed: 16 additions & 54 deletions

File tree

cyclonedx/contrib/dependency/__init__.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

cyclonedx/contrib/dependency/utils.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

cyclonedx/output/json.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
from abc import abstractmethod
1919
from itertools import chain
2020
from json import dumps as json_dumps, loads as json_loads
21-
from typing import TYPE_CHECKING, Any, Literal, Optional, Union
21+
from typing import TYPE_CHECKING, Any, Iterable, Literal, Optional, Union
2222

23-
from ..contrib.dependency.utils import flatten as flatten_dep
2423
from ..exception.output import FormatNotSupportedException
24+
from ..model.dependency import Dependency
2525
from ..schema import OutputFormat, SchemaVersion
2626
from ..schema.schema import (
2727
SCHEMA_VERSIONS,
@@ -61,9 +61,22 @@ def reset(self) -> None:
6161

6262
def flatten(self) -> None:
6363
self._bom.dependencies = chain.from_iterable(
64-
flatten_dep(dep) for dep in self._deps
64+
self.__flatten_dep(dep) for dep in self._deps
6565
)
6666

67+
@staticmethod
68+
def __flatten_dep(dep: Dependency) -> Iterable[Dependency]:
69+
if not dep.dependencies:
70+
return dep,
71+
flat: list[Dependency] = []
72+
todos: list[Dependency] = [dep]
73+
while todos:
74+
todo = todos.pop()
75+
if todo.dependencies:
76+
flat.append(Dependency(todo.ref, (Dependency(d.ref) for d in todo.dependencies)))
77+
todos.extend(todo.dependencies)
78+
return flat
79+
6780

6881
class Json(BaseOutput, BaseSchemaVersion):
6982

0 commit comments

Comments
 (0)