Skip to content

Commit 3e68f76

Browse files
Fix SPDX and other output plugins
Modify and update spdx and other output plugins to use LicenseDetection output data format for licenses. Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 5826429 commit 3e68f76

File tree

10 files changed

+2751
-1490
lines changed

10 files changed

+2751
-1490
lines changed

src/formattedcode/output_spdx.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from commoncode.fileutils import parent_directory
3030
from commoncode.text import python_safe_name
3131
from formattedcode import FileOptionType
32+
from licensedcode.detection import get_matches_from_detections
3233
from plugincode.output import output_impl
3334
from plugincode.output import OutputPlugin
3435
import scancode_config
@@ -276,42 +277,45 @@ def write_spdx(
276277
chk_sum=Algorithm('SHA1', file_data.get('sha1') or '')
277278
)
278279

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:
281283
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:
315319
all_files_have_no_license = False
316320
file_entry.add_lics(NoAssert())
317321

src/licensedcode/detection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,9 @@ def get_matches_from_detections(license_detections):
753753
`license_detections` list of LicenseDetection dicts.
754754
"""
755755
license_matches = []
756+
if not license_detections:
757+
return license_matches
758+
756759
for detection in license_detections:
757760
license_matches.extend(detection["matches"])
758761

0 commit comments

Comments
 (0)