Skip to content

Commit a26899c

Browse files
committed
wip: deps flatten
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 0993a4c commit a26899c

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

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+
"""Dependency related functionality"""
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
from typing import Iterable
19+
20+
from ...model.dependency import Dependency
21+
22+
def flatten(dep: Dependency) -> Iterable[Dependency]:
23+
if len(dep.dependencies) == 0:
24+
return (dep, )
25+
flat: list[Dependency] = [Dependency(dep.ref, (Dependency(d.ref) for d in dep.dependencies))]
26+
todos: list[Dependency] = list(dep.dependencies)
27+
while todos:
28+
todo = todos.pop()
29+
if todo.dependencies:
30+
flat.append(Dependency(dep.ref, (Dependency(d.ref) for d in todo.dependencies)))
31+
todos.extend(todo.dependencies)
32+
return flat
33+

0 commit comments

Comments
 (0)