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