Skip to content

Commit 0ddc50b

Browse files
nicu1989carlescufi
authored andcommitted
scripts: Adapt ncs-sbom to scancode-toolkit v32.4.1
Scancode outputs json format 4.1.0 now. Sbom has been adapted. Signed-off-by: Nicolae Dicu <[email protected]>
1 parent 49bb896 commit 0ddc50b

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

scripts/west_commands/sbom/scancode_toolkit_detector.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def detect(data: Data, optional: bool):
6767

6868
current = result['files'][0]
6969
if 'licenses' in current:
70-
licenses = result['files'][0]['licenses']
70+
licenses = current['licenses']
7171
elif 'license_detections' in current:
72-
licenses = result['files'][0]['license_detections']
72+
licenses = current['license_detections']
7373
else:
7474
print('No license information for {}'.format(current['path']))
7575
continue
@@ -81,11 +81,25 @@ def detect(data: Data, optional: bool):
8181
friendly_id = i['spdx_license_key']
8282
elif 'key' in i and i['key'] != '':
8383
friendly_id = i['key']
84+
elif 'license_expression_spdx' in i and i['license_expression_spdx'] != '':
85+
friendly_id = i['license_expression_spdx']
86+
elif 'license_expression' in i and i['license_expression'] != '':
87+
friendly_id = i['license_expression']
8488
id = friendly_id.upper()
85-
if id in ('UNKNOWN-SPDX', 'LICENSEREF-SCANCODE-UNKNOWN-SPDX'):
86-
friendly_id = re.sub(r'SPDX-License-Identifier:', '', i['matched_text'],
87-
flags=re.I).strip()
88-
id = friendly_id.upper()
89+
if id in ('UNKNOWN-SPDX', 'LICENSEREF-SCANCODE-UNKNOWN-SPDX') or id == '':
90+
matched_text = None
91+
if 'matched_text' in i:
92+
matched_text = i['matched_text']
93+
elif 'matches' in i and isinstance(i['matches'], list):
94+
matched_text = next((match.get('matched_text')
95+
for match in i['matches']
96+
if match.get('matched_text') is not None), None)
97+
if matched_text:
98+
friendly_id = re.sub(r'SPDX-License-Identifier:', '', matched_text,
99+
flags=re.I).strip()
100+
friendly_id = friendly_id.rstrip('*/').strip()
101+
friendly_id = friendly_id.lstrip('/*').strip()
102+
id = friendly_id.upper()
89103
if id == '':
90104
log.wrn(f'Invalid response from scancode-toolkit, file: {file.file_path}')
91105
continue

0 commit comments

Comments
 (0)