Skip to content

Commit ff60025

Browse files
committed
chore:SP-2746 Prioritizes licenses by source in inspect copyleft subcommand
1 parent 80f2678 commit ff60025

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
## [1.25.2] - 2025-06-18
1313
### Fixed
14-
- Avoids errors when no versions are declared on scanner results in `inspect` subcommand
14+
- Fixed errors when no versions are declared in scanner results for `inspect` subcommand
15+
### Changed
16+
- Prioritized licenses by source priority in `inspect copyleft` subcommand
1517

1618
## [1.25.1] - 2025-06-12
1719
### Fixed

src/scanoss/inspection/policy_check.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,11 @@ def _append_component(
226226
if not new_component.get('licenses'):
227227
self.print_debug(f'WARNING: Results missing licenses. Skipping: {new_component}')
228228
return components
229+
230+
231+
licenses_order_by_source_priority = self._get_licenses_order_by_source_priority(new_component['licenses'])
229232
# Process licenses for this component
230-
for license_item in new_component['licenses']:
233+
for license_item in licenses_order_by_source_priority:
231234
if license_item.get('name'):
232235
spdxid = license_item['name']
233236
source = license_item.get('source')
@@ -434,6 +437,48 @@ def _convert_components_to_list(self, components: dict):
434437
self.print_debug(f'WARNING: Licenses missing for: {component}')
435438
component['licenses'] = []
436439
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+
437482
#
438483
# End of PolicyCheck Class
439484
#

0 commit comments

Comments
 (0)