Skip to content

Commit c2af8f5

Browse files
authored
Merge pull request #459 from sschuberth/spdx-sha1-fixes
cli: Add a fallback to calculate the SHA1 if it is not present in the cache
2 parents b2ef3e4 + e37fec9 commit c2af8f5

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

src/scancode/cli.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -642,31 +642,52 @@ def save_results(files_count, scanned_files, format, input, output_file):
642642
from spdx.utils import SPDXNone
643643
from spdx.version import Version
644644

645+
input = abspath(input)
646+
647+
if os.path.isdir(input):
648+
input_path = input
649+
else:
650+
input_path = os.path.dirname(input)
651+
645652
doc = Document(Version(2, 1), License.from_identifier('CC0-1.0'))
646653

647654
doc.creation_info.add_creator(Tool('ScanCode ' + version))
648655
doc.creation_info.set_created_now()
649656

650-
doc.package = Package(input, NoAssert())
657+
doc.package = Package(os.path.basename(input_path), NoAssert())
651658

652659
for file_data in scanned_files:
660+
# Construct the absolute path in case we need to access the file
661+
# to calculate its SHA1.
662+
file_entry = File(os.path.join(input_path, file_data.get('path')))
663+
653664
file_sha1 = file_data.get('sha1')
654665
if not file_sha1:
655-
# Skip directories.
656-
continue
657-
658-
file_entry = File(file_data.get('path'), Algorithm('SHA1', file_sha1))
666+
if os.path.isfile(file_entry.name):
667+
# Calculate the SHA1 in case it is missing, e.g. for empty files.
668+
file_sha1 = file_entry.calc_chksum()
669+
else:
670+
# Skip directories.
671+
continue
672+
673+
# Restore the relative file name as that is what we want in
674+
# SPDX output.
675+
file_entry.name = file_data.get('path')
676+
file_entry.chk_sum = Algorithm('SHA1', file_sha1)
659677

660678
file_licenses = file_data.get('licenses')
661679
if file_licenses:
662680
for file_license in file_licenses:
663681
spdx_id = file_license.get('spdx_license_key')
664-
# TODO: we should create a "LicenseRef:xxx" identifier
665-
# if the license is not known to SPDX
666682
if spdx_id:
667683
spdx_license = License.from_identifier(spdx_id)
668684
file_entry.add_lics(spdx_license)
669685
doc.package.add_lics_from_file(spdx_license)
686+
else:
687+
license_key = 'LicenseRef-' + file_license.get('key')
688+
license_ref = License(file_license.get('short_name'), license_key)
689+
file_entry.add_lics(license_ref)
690+
doc.package.add_lics_from_file(license_ref)
670691

671692
else:
672693
file_entry.add_lics(SPDXNone())

0 commit comments

Comments
 (0)