Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/fosslight_util/oss_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import logging
import os
import hashlib
from fosslight_util.constant import LOGGER_NAME, FOSSLIGHT_SCANNER
from fosslight_util.cover import CoverItem
from typing import List, Dict
Expand Down Expand Up @@ -171,6 +172,22 @@ def get_print_json(self):
return items


def get_checksum_sha1(source_name_or_path) -> str:
checksum = CHECKSUM_NULL
try:
checksum = str(hashlib.sha1(source_name_or_path.encode()).hexdigest())
except Exception:
try:
f = open(source_name_or_path, "rb")
byte = f.read()
checksum = str(hashlib.sha1(byte).hexdigest())
f.close()
except Exception as ex:
_logger.info(f"(Error) Get_checksum: {ex}")

return checksum


def invalid(cmd):
_logger.info('[{}] is invalid'.format(cmd))

Expand Down
9 changes: 9 additions & 0 deletions src/fosslight_util/write_spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from fosslight_util.spdx_licenses import get_spdx_licenses_json, get_license_from_nick
from fosslight_util.constant import (LOGGER_NAME, FOSSLIGHT_DEPENDENCY, FOSSLIGHT_SCANNER,
FOSSLIGHT_BINARY, FOSSLIGHT_SOURCE)
from fosslight_util.oss_item import CHECKSUM_NULL, get_checksum_sha1
import traceback

logger = logging.getLogger(LOGGER_NAME)
Expand Down Expand Up @@ -85,6 +86,14 @@ def write_spdx(output_file_without_ext, output_extension, scan_item, spdx_versio
for file_item in file_items:
file = '' # file의 license, copyright은 oss item에서 append
if scanner_name in [FOSSLIGHT_BINARY, FOSSLIGHT_SOURCE]:
if file_item.exclude:
continue
if file_item.checksum == CHECKSUM_NULL:
if os.path.exists(file_item.source_name_or_path):
file_item.checksum = get_checksum_sha1(file_item.source_name_or_path)
if file_item.checksum == CHECKSUM_NULL:
logger.info(f'Failed to get checksum, Skip: {file_item.source_name_or_path}')
continue
file_id += 1
file = File(name=file_item.source_name_or_path,
spdx_id=f'SPDXRef-File{file_id}',
Expand Down
Loading