5
5
import logging
6
6
import sys
7
7
import uuid
8
+ from pathlib import PurePath
9
+ from typing import Any
8
10
9
- from networkx import DiGraph , shortest_path
11
+ from networkx import DiGraph
10
12
from spdx_tools .spdx .model .document import CreationInfo
11
13
from spdx_tools .spdx .model .document import Document as SpdxDocument
12
14
from spdx_tools .spdx .model .file import File
16
18
from spdx_tools .spdx .parser .parse_anything import parse_file
17
19
from spdx_tools .spdx .validation .document_validator import validate_full_spdx_document
18
20
19
- from opossum_lib .opossum .opossum_file import (
21
+ from opossum_lib .opossum .opossum_file_content import OpossumFileContent
22
+ from opossum_lib .opossum_model import (
20
23
ExternalAttributionSource ,
21
24
Metadata ,
22
- OpossumInformation ,
25
+ Opossum ,
23
26
OpossumPackage ,
24
- OpossumPackageIdentifier ,
25
27
Resource ,
26
- ResourceType ,
28
+ ScanResults ,
27
29
SourceInfo ,
28
30
)
29
- from opossum_lib .opossum .opossum_file_content import OpossumFileContent
30
31
from opossum_lib .spdx .attribution_generation import (
31
32
create_document_attribution ,
32
33
create_file_attribution ,
40
41
)
41
42
from opossum_lib .spdx .graph_generation import generate_graph_from_spdx
42
43
from opossum_lib .spdx .helper_methods import (
43
- _create_file_path_from_graph_path ,
44
+ _get_file_path ,
45
+ _get_resource_type ,
44
46
_get_source_for_graph_traversal ,
45
47
_node_represents_a_spdx_element ,
46
- _replace_node_ids_with_labels_and_add_resource_type ,
47
48
_weakly_connected_component_sub_graphs ,
48
49
)
49
50
from opossum_lib .spdx .tree_generation import generate_tree_from_graph
@@ -69,15 +70,13 @@ def convert_spdx_to_opossum_information(filename: str) -> OpossumFileContent:
69
70
)
70
71
graph = generate_graph_from_spdx (document )
71
72
tree = generate_tree_from_graph (graph )
72
- opossum_information = convert_tree_to_opossum_information (tree )
73
- return OpossumFileContent (input_file = opossum_information )
73
+ return convert_tree_to_opossum (tree ).to_opossum_file_format ()
74
74
75
75
76
- def convert_tree_to_opossum_information (tree : DiGraph ) -> OpossumInformation :
76
+ def convert_tree_to_opossum (tree : DiGraph ) -> Opossum :
77
77
metadata = create_metadata (tree )
78
- resources = Resource (type = ResourceType .TOP_LEVEL )
79
- resources_to_attributions : dict [str , list [str ]] = dict ()
80
- external_attributions : dict [str , OpossumPackage ] = dict ()
78
+ resources = []
79
+ attribution_to_id : dict [OpossumPackage , str ] = {}
81
80
attribution_breakpoints = []
82
81
external_attribution_sources = {
83
82
SPDX_FILE_IDENTIFIER : ExternalAttributionSource (
@@ -97,78 +96,53 @@ def convert_tree_to_opossum_information(tree: DiGraph) -> OpossumInformation:
97
96
raise RuntimeError (
98
97
"A tree should always have a node without incoming edge."
99
98
)
100
- for node in connected_subgraph .nodes ():
101
- path : list [str ] = shortest_path (connected_subgraph , source , node )
102
- path_with_labels : list [tuple [str , ResourceType ]] = (
103
- _replace_node_ids_with_labels_and_add_resource_type (
104
- path , connected_subgraph
105
- )
106
- )
107
- resources = resources .add_path (path_with_labels )
108
- file_path : str = _create_file_path_from_graph_path (path , connected_subgraph )
109
- if _node_represents_a_spdx_element (connected_subgraph , node ):
110
- create_attribution_and_link_with_resource (
111
- external_attributions ,
112
- resources_to_attributions ,
113
- file_path ,
114
- node ,
115
- connected_subgraph ,
99
+ source_file_path = _get_file_path (connected_subgraph , source , source )
100
+ rootnode = Resource (path = PurePath (source_file_path ))
101
+ resources .append (rootnode )
102
+ for node_label in connected_subgraph .nodes ():
103
+ node = connected_subgraph .nodes [node_label ]
104
+ file_path : str = _get_file_path (connected_subgraph , source , node_label )
105
+ new_resource = Resource (path = file_path , type = _get_resource_type (node ))
106
+ if _node_represents_a_spdx_element (connected_subgraph , node_label ):
107
+ attribution = create_attribution (node )
108
+ attribution_to_id [attribution ] = (
109
+ get_attribution_id (node ["element" ]) or node_label
116
110
)
117
-
111
+ new_resource . attributions . append ( attribution )
118
112
else :
119
- attribution_breakpoints .append (file_path )
113
+ attribution_breakpoints .append ("/" + file_path )
114
+ rootnode .add_resource (new_resource )
120
115
121
- opossum_information = OpossumInformation (
116
+ scan_results = ScanResults (
122
117
metadata = metadata ,
123
- resources = resources .convert_to_file_resource (),
124
- external_attributions = external_attributions ,
125
- resources_to_attributions = resources_to_attributions ,
118
+ resources = resources ,
119
+ attribution_to_id = attribution_to_id ,
126
120
attribution_breakpoints = attribution_breakpoints ,
127
121
external_attribution_sources = external_attribution_sources ,
128
122
)
129
- return opossum_information
123
+ return Opossum ( scan_results = scan_results )
130
124
131
125
132
- def create_attribution_and_link_with_resource (
133
- external_attributions : dict [OpossumPackageIdentifier , OpossumPackage ],
134
- resources_to_attributions : dict [OpossumPackageIdentifier , list [str ]],
135
- file_path : str ,
136
- node : str ,
137
- tree : DiGraph ,
138
- ) -> None :
139
- node_element = tree .nodes [node ]["element" ]
126
+ def create_attribution (
127
+ node : dict [str , Any ],
128
+ ) -> OpossumPackage :
129
+ node_element = node ["element" ]
140
130
if isinstance (node_element , Package ):
141
- external_attributions [node_element .spdx_id ] = create_package_attribution (
142
- package = node_element
143
- )
144
- resources_to_attributions [file_path ] = [
145
- node_element .spdx_id ,
146
- ]
131
+ return create_package_attribution (package = node_element )
147
132
elif isinstance (node_element , File ):
148
- external_attributions [node_element .spdx_id ] = create_file_attribution (
149
- node_element
150
- )
151
- resources_to_attributions [file_path ] = [
152
- node_element .spdx_id ,
153
- ]
133
+ return create_file_attribution (node_element )
154
134
elif isinstance (node_element , Snippet ):
155
- external_attributions [node_element .spdx_id ] = create_snippet_attribution (
156
- node_element
157
- )
158
- resources_to_attributions [file_path ] = [
159
- node_element .spdx_id ,
160
- ]
135
+ return create_snippet_attribution (node_element )
161
136
elif isinstance (node_element , CreationInfo ):
162
- external_attributions [node_element .spdx_id ] = create_document_attribution (
163
- node_element
164
- )
165
- resources_to_attributions [file_path ] = [node_element .spdx_id ]
166
-
137
+ return create_document_attribution (node_element )
167
138
else :
168
- external_attributions [node ] = OpossumPackage (
169
- source = SourceInfo (name = tree .nodes [node ]["label" ])
170
- )
171
- resources_to_attributions [file_path ] = [node ]
139
+ return OpossumPackage (source = SourceInfo (name = node ["label" ]))
140
+
141
+
142
+ def get_attribution_id (element : Any ) -> str | None :
143
+ if isinstance (element , Package | File | Snippet | CreationInfo ):
144
+ return element .spdx_id
145
+ return None
172
146
173
147
174
148
def create_metadata (tree : DiGraph ) -> Metadata :
0 commit comments