Skip to content

Commit 29a0398

Browse files
authored
Fix the spdx bug (#194)
Signed-off-by: jiyeong.seok <[email protected]>
1 parent 3035d1d commit 29a0398

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/fosslight_util/oss_item.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import logging
77
import os
8+
import hashlib
89
from fosslight_util.constant import LOGGER_NAME, FOSSLIGHT_SCANNER
910
from fosslight_util.cover import CoverItem
1011
from typing import List, Dict
@@ -171,6 +172,22 @@ def get_print_json(self):
171172
return items
172173

173174

175+
def get_checksum_sha1(source_name_or_path) -> str:
176+
checksum = CHECKSUM_NULL
177+
try:
178+
checksum = str(hashlib.sha1(source_name_or_path.encode()).hexdigest())
179+
except Exception:
180+
try:
181+
f = open(source_name_or_path, "rb")
182+
byte = f.read()
183+
checksum = str(hashlib.sha1(byte).hexdigest())
184+
f.close()
185+
except Exception as ex:
186+
_logger.info(f"(Error) Get_checksum: {ex}")
187+
188+
return checksum
189+
190+
174191
def invalid(cmd):
175192
_logger.info('[{}] is invalid'.format(cmd))
176193

src/fosslight_util/write_spdx.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from fosslight_util.spdx_licenses import get_spdx_licenses_json, get_license_from_nick
1313
from fosslight_util.constant import (LOGGER_NAME, FOSSLIGHT_DEPENDENCY, FOSSLIGHT_SCANNER,
1414
FOSSLIGHT_BINARY, FOSSLIGHT_SOURCE)
15+
from fosslight_util.oss_item import CHECKSUM_NULL, get_checksum_sha1
1516
import traceback
1617

1718
logger = logging.getLogger(LOGGER_NAME)
@@ -85,6 +86,14 @@ def write_spdx(output_file_without_ext, output_extension, scan_item, spdx_versio
8586
for file_item in file_items:
8687
file = '' # file의 license, copyright은 oss item에서 append
8788
if scanner_name in [FOSSLIGHT_BINARY, FOSSLIGHT_SOURCE]:
89+
if file_item.exclude:
90+
continue
91+
if file_item.checksum == CHECKSUM_NULL:
92+
if os.path.exists(file_item.source_name_or_path):
93+
file_item.checksum = get_checksum_sha1(file_item.source_name_or_path)
94+
if file_item.checksum == CHECKSUM_NULL:
95+
logger.info(f'Failed to get checksum, Skip: {file_item.source_name_or_path}')
96+
continue
8897
file_id += 1
8998
file = File(name=file_item.source_name_or_path,
9099
spdx_id=f'SPDXRef-File{file_id}',

0 commit comments

Comments
 (0)