|
18 | 18 | from abc import abstractmethod |
19 | 19 | from itertools import chain |
20 | 20 | 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 |
22 | 22 |
|
23 | | -from ..contrib.dependency.utils import flatten as flatten_dep |
24 | 23 | from ..exception.output import FormatNotSupportedException |
| 24 | +from ..model.dependency import Dependency |
25 | 25 | from ..schema import OutputFormat, SchemaVersion |
26 | 26 | from ..schema.schema import ( |
27 | 27 | SCHEMA_VERSIONS, |
@@ -61,9 +61,22 @@ def reset(self) -> None: |
61 | 61 |
|
62 | 62 | def flatten(self) -> None: |
63 | 63 | 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 |
65 | 65 | ) |
66 | 66 |
|
| 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 | + |
67 | 80 |
|
68 | 81 | class Json(BaseOutput, BaseSchemaVersion): |
69 | 82 |
|
|
0 commit comments