|
29 | 29 | from commoncode.fileutils import parent_directory |
30 | 30 | from commoncode.text import python_safe_name |
31 | 31 | from formattedcode import FileOptionType |
| 32 | +from licensedcode.detection import get_matches_from_detections |
32 | 33 | from plugincode.output import output_impl |
33 | 34 | from plugincode.output import OutputPlugin |
34 | 35 | import scancode_config |
@@ -276,42 +277,45 @@ def write_spdx( |
276 | 277 | chk_sum=Algorithm('SHA1', file_data.get('sha1') or '') |
277 | 278 | ) |
278 | 279 |
|
279 | | - file_licenses = file_data.get('licenses') |
280 | | - if file_licenses: |
| 280 | + file_license_detections = file_data.get('licenses') |
| 281 | + license_matches = get_matches_from_detections(file_license_detections) |
| 282 | + if license_matches: |
281 | 283 | all_files_have_no_license = False |
282 | | - for file_license in file_licenses: |
283 | | - license_key = file_license.get('key') |
284 | | - |
285 | | - spdx_id = file_license.get('spdx_license_key') |
286 | | - if not spdx_id: |
287 | | - spdx_id = f'LicenseRef-scancode-{license_key}' |
288 | | - is_license_ref = spdx_id.lower().startswith('licenseref-') |
289 | | - |
290 | | - if not is_license_ref: |
291 | | - spdx_license = License.from_identifier(spdx_id) |
292 | | - else: |
293 | | - spdx_license = ExtractedLicense(spdx_id) |
294 | | - spdx_license.name = file_license.get('short_name') |
295 | | - # FIXME: replace this with the licensedb URL |
296 | | - comment = ( |
297 | | - f'See details at https://github.com/nexB/scancode-toolkit' |
298 | | - f'/blob/develop/src/licensedcode/data/licenses/{license_key}.yml\n' |
299 | | - ) |
300 | | - spdx_license.comment = comment |
301 | | - text = file_license.get('matched_text') |
302 | | - # always set some text, even if we did not extract the |
303 | | - # matched text |
304 | | - if not text: |
305 | | - text = comment |
306 | | - spdx_license.text = text |
307 | | - doc.add_extr_lic(spdx_license) |
308 | | - |
309 | | - # Add licenses in the order they appear in the file. Maintaining |
310 | | - # the order might be useful for provenance purposes. |
311 | | - file_entry.add_lics(spdx_license) |
312 | | - package.add_lics_from_file(spdx_license) |
313 | | - |
314 | | - elif file_licenses is None: |
| 284 | + for match in license_matches: |
| 285 | + file_licenses = match["licenses"] |
| 286 | + for file_license in file_licenses: |
| 287 | + license_key = file_license.get('key') |
| 288 | + |
| 289 | + spdx_id = file_license.get('spdx_license_key') |
| 290 | + if not spdx_id: |
| 291 | + spdx_id = f'LicenseRef-scancode-{license_key}' |
| 292 | + is_license_ref = spdx_id.lower().startswith('licenseref-') |
| 293 | + |
| 294 | + if not is_license_ref: |
| 295 | + spdx_license = License.from_identifier(spdx_id) |
| 296 | + else: |
| 297 | + spdx_license = ExtractedLicense(spdx_id) |
| 298 | + spdx_license.name = file_license.get('short_name') |
| 299 | + # FIXME: replace this with the licensedb URL |
| 300 | + comment = ( |
| 301 | + f'See details at https://github.com/nexB/scancode-toolkit' |
| 302 | + f'/blob/develop/src/licensedcode/data/licenses/{license_key}.yml\n' |
| 303 | + ) |
| 304 | + spdx_license.comment = comment |
| 305 | + text = match.get('matched_text') |
| 306 | + # always set some text, even if we did not extract the |
| 307 | + # matched text |
| 308 | + if not text: |
| 309 | + text = comment |
| 310 | + spdx_license.text = text |
| 311 | + doc.add_extr_lic(spdx_license) |
| 312 | + |
| 313 | + # Add licenses in the order they appear in the file. Maintaining |
| 314 | + # the order might be useful for provenance purposes. |
| 315 | + file_entry.add_lics(spdx_license) |
| 316 | + package.add_lics_from_file(spdx_license) |
| 317 | + |
| 318 | + elif license_matches is None: |
315 | 319 | all_files_have_no_license = False |
316 | 320 | file_entry.add_lics(NoAssert()) |
317 | 321 |
|
|
0 commit comments