12
12
from faker .providers .misc import Provider as MiscProvider
13
13
from faker .providers .person import Provider as PersonProvider
14
14
15
+ from opossum_lib .core .entities .scan_results import ScanResults
15
16
from opossum_lib .shared .entities .opossum_input_file_model import (
16
17
OpossumPackageIdentifierModel ,
17
- OpossumPackageModel ,
18
18
ResourceInFileModel ,
19
19
)
20
20
from opossum_lib .shared .entities .opossum_output_file_model import (
@@ -52,16 +52,36 @@ def output_file(
52
52
manual_attributions : dict [str , ManualAttributions ] | None = None ,
53
53
resources_to_attributions : dict [str , list [str ]] | None = None ,
54
54
resolved_external_attributions : list [str ] | None = None ,
55
+ scan_results : ScanResults | None = None ,
55
56
) -> OpossumOutputFileModel :
56
57
if metadata is None :
57
- metadata = self .outfile_metadata ()
58
+ project_id = scan_results .metadata .project_id if scan_results else None
59
+ file_creation_date = (
60
+ scan_results .metadata .file_creation_date if scan_results else None
61
+ )
62
+ metadata = self .outfile_metadata (
63
+ project_id = project_id ,
64
+ file_creation_date = file_creation_date ,
65
+ )
58
66
if manual_attributions is None :
59
67
manual_attributions = self .manual_attributions ()
60
68
if resolved_external_attributions is None :
61
- resolved_external_attributions = self .resolved_external_attributions ()
69
+ external_attributions_ids = (
70
+ list (scan_results .attribution_to_id .values ()) if scan_results else None
71
+ )
72
+ resolved_external_attributions = self .resolved_external_attributions (
73
+ external_attributions_ids = external_attributions_ids
74
+ )
62
75
if resources_to_attributions is None :
76
+ all_paths = None
77
+ if scan_results :
78
+ all_paths = [
79
+ str (resource .path )
80
+ for resource in scan_results .resources .all_resources ()
81
+ ]
63
82
resources_to_attributions = self .resources_to_attributions (
64
- manual_attributions = manual_attributions
83
+ manual_attributions = manual_attributions ,
84
+ all_paths = all_paths ,
65
85
)
66
86
return OpossumOutputFileModel (
67
87
metadata = metadata ,
@@ -185,32 +205,54 @@ def manual_attribution(
185
205
def resolved_external_attributions (
186
206
self ,
187
207
* ,
188
- external_attributions : dict [OpossumPackageIdentifierModel , OpossumPackageModel ]
189
- | None = None ,
208
+ external_attributions_ids : list [OpossumPackageIdentifierModel ] | None = None ,
190
209
min_count : int = 1 ,
191
210
max_count : int = 5 ,
192
211
) -> list [str ]:
193
212
length = self .random_int (min = min_count , max = max_count )
194
- if external_attributions is not None :
195
- length = min (length , len (external_attributions ))
196
- ids = list (external_attributions .keys ())
197
- return list (self .random_elements (elements = ids , length = length , unique = True ))
213
+ if external_attributions_ids is not None :
214
+ length = min (length , len (external_attributions_ids ))
215
+ return list (
216
+ self .random_elements (
217
+ external_attributions_ids , length = length , unique = True
218
+ )
219
+ )
198
220
else :
199
221
return [str (self .misc_provider .uuid4 ()) for _ in range (length )]
200
222
223
+ def _resources_to_path (
224
+ self ,
225
+ resources : ResourceInFileModel ,
226
+ ) -> list [str ]:
227
+ paths : list [str ] = []
228
+ if isinstance (resources , int ):
229
+ return paths
230
+ for path_segment , children in resources .items ():
231
+ if isinstance (children , int ):
232
+ paths .append (path_segment )
233
+ else :
234
+ subpaths = self ._resources_to_path (children )
235
+ paths .extend (path_segment + "/" + subpath for subpath in subpaths )
236
+ return paths
237
+
238
+ def _ensure_root_prefix (self , path : str ) -> str :
239
+ if not path .startswith ("/" ):
240
+ return "/" + path
241
+ else :
242
+ return path
243
+
201
244
def resources_to_attributions (
202
245
self ,
203
246
* ,
204
247
resources : ResourceInFileModel | None = None ,
205
248
manual_attributions : dict [str , ManualAttributions ] | None = None ,
249
+ all_paths : list [str ] | None ,
206
250
min_count : int = 1 ,
207
251
max_count : int = 5 ,
208
252
num_attributions : int = 3 ,
209
253
) -> dict [str , list [str ]]:
210
254
if manual_attributions is None :
211
255
manual_attributions = self .manual_attributions ()
212
- if resources is None :
213
- resources = self .file_information_provider .resource_in_file ()
214
256
if manual_attributions is not None :
215
257
attribution_ids = list (manual_attributions .keys ())
216
258
else :
@@ -219,21 +261,14 @@ def resources_to_attributions(
219
261
for _ in range (self .random_int (max = max_count * num_attributions ))
220
262
]
221
263
222
- def resources_to_path (
223
- resources : ResourceInFileModel ,
224
- ) -> list [str ]:
225
- paths : list [str ] = []
226
- if isinstance (resources , int ):
227
- return paths
228
- for path_segment , children in resources .items ():
229
- if isinstance (children , int ):
230
- paths .append (path_segment )
231
- else :
232
- subpaths = resources_to_path (children )
233
- paths .extend (path_segment + "/" + subpath for subpath in subpaths )
234
- return paths
264
+ all_paths = all_paths or []
265
+ if resources :
266
+ all_paths .extend (self ._resources_to_path (resources ))
267
+ if not all_paths :
268
+ resources = self .file_information_provider .resource_in_file ()
269
+ all_paths .extend (self ._resources_to_path (resources ))
270
+ all_paths = [self ._ensure_root_prefix (path ) for path in all_paths ]
235
271
236
- all_paths = ["/" + path for path in resources_to_path (resources )]
237
272
number_of_paths = min (
238
273
self .random_int (min = min_count , max = max_count ), len (all_paths )
239
274
)
0 commit comments