Skip to content

Commit

Permalink
Fix SPDX and other output plugins
Browse files Browse the repository at this point in the history
Modify and update spdx and other output plugins to use LicenseDetection
output data format for licenses.

Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
  • Loading branch information
AyanSinhaMahapatra committed Jun 1, 2022
1 parent 5826429 commit e68f0de
Show file tree
Hide file tree
Showing 9 changed files with 2,620 additions and 1,343 deletions.
74 changes: 39 additions & 35 deletions src/formattedcode/output_spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from commoncode.fileutils import parent_directory
from commoncode.text import python_safe_name
from formattedcode import FileOptionType
from licensedcode.detection import get_matches_from_detections
from plugincode.output import output_impl
from plugincode.output import OutputPlugin
import scancode_config
Expand Down Expand Up @@ -276,42 +277,45 @@ def write_spdx(
chk_sum=Algorithm('SHA1', file_data.get('sha1') or '')
)

file_licenses = file_data.get('licenses')
if file_licenses:
file_license_detections = file_data.get('licenses')
license_matches = get_matches_from_detections(file_license_detections)
if license_matches:
all_files_have_no_license = False
for file_license in file_licenses:
license_key = file_license.get('key')

spdx_id = file_license.get('spdx_license_key')
if not spdx_id:
spdx_id = f'LicenseRef-scancode-{license_key}'
is_license_ref = spdx_id.lower().startswith('licenseref-')

if not is_license_ref:
spdx_license = License.from_identifier(spdx_id)
else:
spdx_license = ExtractedLicense(spdx_id)
spdx_license.name = file_license.get('short_name')
# FIXME: replace this with the licensedb URL
comment = (
f'See details at https://github.com/nexB/scancode-toolkit'
f'/blob/develop/src/licensedcode/data/licenses/{license_key}.yml\n'
)
spdx_license.comment = comment
text = file_license.get('matched_text')
# always set some text, even if we did not extract the
# matched text
if not text:
text = comment
spdx_license.text = text
doc.add_extr_lic(spdx_license)

# Add licenses in the order they appear in the file. Maintaining
# the order might be useful for provenance purposes.
file_entry.add_lics(spdx_license)
package.add_lics_from_file(spdx_license)

elif file_licenses is None:
for match in license_matches:
file_licenses = match["licenses"]
for file_license in file_licenses:
license_key = file_license.get('key')

spdx_id = file_license.get('spdx_license_key')
if not spdx_id:
spdx_id = f'LicenseRef-scancode-{license_key}'
is_license_ref = spdx_id.lower().startswith('licenseref-')

if not is_license_ref:
spdx_license = License.from_identifier(spdx_id)
else:
spdx_license = ExtractedLicense(spdx_id)
spdx_license.name = file_license.get('short_name')
# FIXME: replace this with the licensedb URL
comment = (
f'See details at https://github.com/nexB/scancode-toolkit'
f'/blob/develop/src/licensedcode/data/licenses/{license_key}.yml\n'
)
spdx_license.comment = comment
text = match.get('matched_text')
# always set some text, even if we did not extract the
# matched text
if not text:
text = comment
spdx_license.text = text
doc.add_extr_lic(spdx_license)

# Add licenses in the order they appear in the file. Maintaining
# the order might be useful for provenance purposes.
file_entry.add_lics(spdx_license)
package.add_lics_from_file(spdx_license)

elif license_matches is None:
all_files_have_no_license = False
file_entry.add_lics(NoAssert())

Expand Down
3 changes: 3 additions & 0 deletions src/licensedcode/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ def get_matches_from_detections(license_detections):
`license_detections` list of LicenseDetection dicts.
"""
license_matches = []
if not license_detections:
return license_matches

for detection in license_detections:
license_matches.extend(detection["matches"])

Expand Down
Loading

0 comments on commit e68f0de

Please sign in to comment.