@@ -226,8 +226,11 @@ def _append_component(
226
226
if not new_component .get ('licenses' ):
227
227
self .print_debug (f'WARNING: Results missing licenses. Skipping: { new_component } ' )
228
228
return components
229
+
230
+
231
+ licenses_order_by_source_priority = self ._get_licenses_order_by_source_priority (new_component ['licenses' ])
229
232
# Process licenses for this component
230
- for license_item in new_component [ 'licenses' ] :
233
+ for license_item in licenses_order_by_source_priority :
231
234
if license_item .get ('name' ):
232
235
spdxid = license_item ['name' ]
233
236
source = license_item .get ('source' )
@@ -434,6 +437,48 @@ def _convert_components_to_list(self, components: dict):
434
437
self .print_debug (f'WARNING: Licenses missing for: { component } ' )
435
438
component ['licenses' ] = []
436
439
return results_list
440
+
441
+ def _get_licenses_order_by_source_priority (self ,licenses_data ):
442
+ """
443
+ Select licenses based on source priority:
444
+ 1. component_declared (highest priority)
445
+ 2. license_file
446
+ 3. file_header
447
+ 4. scancode (lowest priority)
448
+
449
+ If any high-priority source is found, return only licenses from that source.
450
+ If none found, return all licenses.
451
+
452
+ Returns: list with ordered licenses by source.
453
+ """
454
+ # Define priority order (highest to lowest)
455
+ priority_sources = ['component_declared' , 'license_file' , 'file_header' , 'scancode' ]
456
+
457
+ # Group licenses by source
458
+ licenses_by_source = {}
459
+ for license_item in licenses_data :
460
+
461
+ source = license_item .get ('source' , 'unknown' )
462
+ if source not in licenses_by_source :
463
+ licenses_by_source [source ] = {}
464
+
465
+ license_name = license_item .get ('name' )
466
+ if license_name :
467
+ # Use license name as key, store full license object as value
468
+ # If duplicate license names exist in same source, the last one wins
469
+ licenses_by_source [source ][license_name ] = license_item
470
+
471
+ # Find the highest priority source that has licenses
472
+ for priority_source in priority_sources :
473
+ if priority_source in licenses_by_source :
474
+ self .print_trace (f'Choosing { priority_source } as source' )
475
+ return list (licenses_by_source [priority_source ].values ())
476
+
477
+ # If no priority sources found, combine all licenses into a single list
478
+ self .print_debug ("No priority sources found, returning all licenses as list" )
479
+ return licenses_data
480
+
481
+
437
482
#
438
483
# End of PolicyCheck Class
439
484
#
0 commit comments