Skip to content

Commit 92b812c

Browse files
committed
cli: Add a fallback to calculate the SHA1 if it is not present in the cache
1 parent 185675b commit 92b812c

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

src/scancode/cli.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,20 +642,38 @@ 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:

0 commit comments

Comments
 (0)