Skip to content

Commit

Permalink
Add type hint (#184)
Browse files Browse the repository at this point in the history
Signed-off-by: hkkim <khk990619@gmail.com>
  • Loading branch information
hkkim2021 authored Sep 14, 2024
1 parent b3983bb commit d7986e5
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/fosslight_source/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
--correct_fpath <path> Path to the sbom-info.yaml file"""


def print_version(pkg_name):
def print_version(pkg_name: str) -> None:
print_package_version(pkg_name, "FOSSLight Source Scanner Version:")


def print_help_msg_source_scanner():
def print_help_msg_source_scanner() -> None:
helpMsg = PrintHelpMsg(_HELP_MESSAGE_SOURCE_SCANNER)
helpMsg.print_help_msg(True)
16 changes: 8 additions & 8 deletions src/fosslight_source/_license_matched.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@ class MatchedLicense:
matched_text = ""
priority = 0

def __init__(self, lic, category, text, file):
def __init__(self, lic: str, category: str, text: str, file: str) -> None:
self.files = [file]
self.license = lic
self.matched_text = text
self.set_category(category)

def __del__(self):
def __del__(self) -> None:
pass

def set_license(self, value):
def set_license(self, value: str) -> None:
self.license = value

def set_files(self, value):
def set_files(self, value: str) -> None:
if value not in self.files:
self.files.append(value)

def set_category(self, value):
def set_category(self, value: str) -> None:
self.category = value
if value in LOW_PRIORITY:
self.priority = 1
else:
self.priority = 0

def set_matched_text(self, value):
def set_matched_text(self, value: str) -> None:
self.matched_text = value

def get_row_to_print(self, result_for_32_earlier=True):
def get_row_to_print(self, result_for_32_earlier: bool = True) -> list:
if result_for_32_earlier:
print_rows = [self.category, self.license, self.matched_text, str(len(self.files)), ','.join(self.files)]
else:
print_rows = [self.license, self.matched_text, str(len(self.files)), ','.join(self.files)]
return print_rows


def get_license_list_to_print(license_list):
def get_license_list_to_print(license_list: dict) -> list:
result_for_32_earlier = any([value.category for key, value in license_list.items()])
license_items = license_list.values()
license_items = sorted(license_items, key=lambda row: (row.priority, row.category, row.license))
Expand Down
15 changes: 10 additions & 5 deletions src/fosslight_source/_parsing_scancode_file_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ._scan_item import is_exclude_file
from ._scan_item import replace_word
from ._scan_item import is_notice_file
from typing import Tuple

logger = logging.getLogger(constant.LOGGER_NAME)
_exclude_directory = ["test", "tests", "doc", "docs"]
Expand All @@ -30,7 +31,7 @@
KEY_OR = r"(?<=\s)or(?=\s)"


def get_error_from_header(header_item):
def get_error_from_header(header_item: list) -> Tuple[bool, str]:
has_error = False
str_error = ""
key_error = "errors"
Expand All @@ -49,7 +50,7 @@ def get_error_from_header(header_item):
return has_error, str_error


def parsing_scancode_32_earlier(scancode_file_list, has_error=False):
def parsing_scancode_32_earlier(scancode_file_list: list, has_error: bool = False) -> Tuple[bool, list, list, dict]:
rc = True
msg = []
scancode_file_item = []
Expand Down Expand Up @@ -178,15 +179,17 @@ def parsing_scancode_32_earlier(scancode_file_list, has_error=False):
return rc, scancode_file_item, msg, license_list


def split_spdx_expression(spdx_string):
def split_spdx_expression(spdx_string: str) -> list:
license = []
for replace in SPDX_REPLACE_WORDS:
spdx_string = spdx_string.replace(replace, "")
license = re.split(KEY_AND + "|" + KEY_OR, spdx_string)
return license


def parsing_scancode_32_later(scancode_file_list, has_error=False):
def parsing_scancode_32_later(
scancode_file_list: list, has_error: bool = False
) -> Tuple[bool, list, list, dict]:
rc = True
msg = []
scancode_file_item = []
Expand Down Expand Up @@ -274,7 +277,9 @@ def parsing_scancode_32_later(scancode_file_list, has_error=False):
return rc, scancode_file_item, msg, license_list


def parsing_file_item(scancode_file_list, has_error, need_matched_license=False):
def parsing_file_item(
scancode_file_list: list, has_error: bool, need_matched_license: bool = False
) -> Tuple[bool, list, list, dict]:

rc = True
msg = []
Expand Down
5 changes: 3 additions & 2 deletions src/fosslight_source/_parsing_scanoss_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
from ._scan_item import SourceItem
from ._scan_item import is_exclude_file
from ._scan_item import replace_word
from typing import Tuple

logger = logging.getLogger(constant.LOGGER_NAME)
SCANOSS_INFO_HEADER = ['No', 'Source Path', 'Component Declared', 'SPDX Tag',
'File Header', 'License File', 'Scancode',
'Matched Rate (line number)', 'scanoss_fileURL']


def parsing_extraInfo(scanned_result):
def parsing_extraInfo(scanned_result: dict) -> list:
scanoss_extra_info = []
for scan_item in scanned_result:
license_w_source = scan_item.scanoss_reference
Expand All @@ -35,7 +36,7 @@ def parsing_extraInfo(scanned_result):
return scanoss_extra_info


def parsing_scanResult(scanoss_report, path_to_scan="", path_to_exclude=[]):
def parsing_scanResult(scanoss_report: dict, path_to_scan: str = "", path_to_exclude: list = []) -> Tuple[bool, list]:
scanoss_file_item = []
abs_path_to_exclude = [os.path.abspath(os.path.join(path_to_scan, path)) for path in path_to_exclude]

Expand Down
22 changes: 11 additions & 11 deletions src/fosslight_source/_scan_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class SourceItem(FileItem):

def __init__(self, value):
def __init__(self, value: str) -> None:
super().__init__("")
self.source_name_or_path = value
self.is_license_text = False
Expand All @@ -42,18 +42,18 @@ def __init__(self, value):
self.oss_name = ""
self.oss_version = ""

def __del__(self):
def __del__(self) -> None:
pass

def __hash__(self):
def __hash__(self) -> int:
return hash(self.file)

@property
def licenses(self):
def licenses(self) -> list:
return self._licenses

@licenses.setter
def licenses(self, value):
def licenses(self, value: list) -> None:
if value:
max_length_exceed = False
for new_lic in value:
Expand All @@ -70,7 +70,7 @@ def licenses(self, value):
if max_length_exceed and (SUBSTRING_LICENSE_COMMENT not in self.comment):
self.comment = f"{self.comment}/ {SUBSTRING_LICENSE_COMMENT}" if self.comment else SUBSTRING_LICENSE_COMMENT

def set_oss_item(self):
def set_oss_item(self) -> None:
self.oss_items = []
if self.download_location:
for url in self.download_location:
Expand All @@ -84,7 +84,7 @@ def set_oss_item(self):
item.comment = self.comment
self.oss_items.append(item)

def get_print_array(self):
def get_print_array(self) -> list:
print_rows = []
for item in self.oss_items:
print_rows.append([self.source_name_or_path, item.name, item.version, ",".join(item.license),
Expand All @@ -93,14 +93,14 @@ def get_print_array(self):
self.license_reference])
return print_rows

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
if type(other) == str:
return self.source_name_or_path == other
else:
return self.source_name_or_path == other.source_name_or_path


def is_exclude_dir(dir_path):
def is_exclude_dir(dir_path: str) -> bool:
if dir_path != "":
dir_path = dir_path.lower()
dir_path = dir_path if dir_path.endswith(
Expand All @@ -111,7 +111,7 @@ def is_exclude_dir(dir_path):
return False


def is_exclude_file(file_path, prev_dir=None, prev_dir_exclude_value=None):
def is_exclude_file(file_path: str, prev_dir: str = None, prev_dir_exclude_value: bool = None) -> bool:
file_path = file_path.lower()
filename = os.path.basename(file_path)
if os.path.splitext(filename)[1] in _exclude_extension:
Expand All @@ -133,7 +133,7 @@ def is_exclude_file(file_path, prev_dir=None, prev_dir_exclude_value=None):
return False


def is_notice_file(file_path):
def is_notice_file(file_path: str) -> bool:
pattern = r"({})(?<!w)".format("|".join(_notice_filename))
file_path = file_path.lower()
filename = os.path.basename(file_path)
Expand Down
29 changes: 20 additions & 9 deletions src/fosslight_source/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .run_spdx_extractor import get_spdx_downloads
from ._scan_item import SourceItem
from fosslight_util.oss_item import ScannerItem
from typing import Tuple

SRC_SHEET_NAME = 'SRC_FL_Source'
SCANOSS_HEADER = {SRC_SHEET_NAME: ['ID', 'Source Path', 'OSS Name',
Expand All @@ -39,7 +40,7 @@
RESULT_KEY = "Scan Result"


def main():
def main() -> None:
global logger
_result_log = {}

Expand Down Expand Up @@ -121,7 +122,7 @@ def main():
sys.exit(1)


def count_files(path_to_scan, path_to_exclude):
def count_files(path_to_scan: str, path_to_exclude: list) -> Tuple[int, int]:
total_files = 0
excluded_files = 0
abs_path_to_exclude = [os.path.abspath(os.path.join(path_to_scan, path)) for path in path_to_exclude]
Expand All @@ -138,9 +139,14 @@ def count_files(path_to_scan, path_to_exclude):
return total_files, excluded_files


def create_report_file(_start_time, merged_result, license_list, scanoss_result, selected_scanner, need_license=False,
output_path="", output_files=[], output_extensions=[], correct_mode=True, correct_filepath="",
path_to_scan="", path_to_exclude=[]):
def create_report_file(
_start_time: str, merged_result: list,
license_list: list, scanoss_result: list,
selected_scanner: str, need_license: bool = False,
output_path: str = "", output_files: list = [],
output_extensions: list = [], correct_mode: bool = True,
correct_filepath: str = "", path_to_scan: str = "", path_to_exclude: list = []
) -> 'ScannerItem':
"""
Create report files for given scanned result.
Expand Down Expand Up @@ -227,7 +233,7 @@ def create_report_file(_start_time, merged_result, license_list, scanoss_result,
return scan_item


def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
def merge_results(scancode_result: list = [], scanoss_result: list = [], spdx_downloads: dict = {}) -> list:
"""
Merge scanner results and spdx parsing result.
:param scancode_result: list of scancode results in SourceItem.
Expand Down Expand Up @@ -257,9 +263,14 @@ def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
return scancode_result


def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_cores=-1, called_by_cli=True,
print_matched_text=False, formats=[], time_out=120, correct_mode=True, correct_filepath="",
selected_scanner='all', path_to_exclude=[]):
def run_scanners(
path_to_scan: str, output_file_name: str = "",
write_json_file: bool = False, num_cores: int = -1,
called_by_cli: bool = True, print_matched_text: bool = False,
formats: list = [], time_out: int = 120,
correct_mode: bool = True, correct_filepath: str = "",
selected_scanner: str = 'all', path_to_exclude: list = []
) -> Tuple[bool, str, 'ScannerItem', list, list]:
"""
Run Scancode and scanoss.py for the given path.
Expand Down
12 changes: 9 additions & 3 deletions src/fosslight_source/run_scancode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
from ._parsing_scancode_file_item import get_error_from_header
from fosslight_util.output_format import check_output_formats
from fosslight_binary.binary_analysis import check_binary
from typing import Tuple

logger = logging.getLogger(constant.LOGGER_NAME)
warnings.filterwarnings("ignore", category=FutureWarning)
_PKG_NAME = "fosslight_source"


def run_scan(path_to_scan, output_file_name="",
_write_json_file=False, num_cores=-1, return_results=False, need_license=False, formats=[],
called_by_cli=False, time_out=120, correct_mode=True, correct_filepath="", path_to_exclude=[]):
def run_scan(
path_to_scan: str, output_file_name: str = "",
_write_json_file: bool = False, num_cores: int = -1,
return_results: bool = False, need_license: bool = False,
formats: list = [], called_by_cli: bool = False,
time_out: int = 120, correct_mode: bool = True,
correct_filepath: str = "", path_to_exclude: list = []
) -> Tuple[bool, str, list, list]:
if not called_by_cli:
global logger

Expand Down
6 changes: 3 additions & 3 deletions src/fosslight_source/run_scanoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
SCANOSS_OUTPUT_FILE = "scanoss_raw_result.json"


def get_scanoss_extra_info(scanned_result):
def get_scanoss_extra_info(scanned_result: dict) -> list:
return parsing_extraInfo(scanned_result)


def run_scanoss_py(path_to_scan, output_file_name="", format="", called_by_cli=False,
write_json_file=False, num_threads=-1, path_to_exclude=[]):
def run_scanoss_py(path_to_scan: str, output_file_name: str = "", format: str = "", called_by_cli: bool = False,
write_json_file: bool = False, num_threads: int = -1, path_to_exclude: list = []) -> list:
"""
Run scanoss.py for the given path.
Expand Down
4 changes: 2 additions & 2 deletions src/fosslight_source/run_spdx_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
logger = logging.getLogger(constant.LOGGER_NAME)


def get_file_list(path_to_scan, path_to_exclude=[]):
def get_file_list(path_to_scan: str, path_to_exclude: list = []) -> list:
file_list = []
abs_path_to_exclude = [os.path.abspath(os.path.join(path_to_scan, path)) for path in path_to_exclude]
for root, dirs, files in os.walk(path_to_scan):
Expand All @@ -26,7 +26,7 @@ def get_file_list(path_to_scan, path_to_exclude=[]):
return file_list


def get_spdx_downloads(path_to_scan, path_to_exclude=[]):
def get_spdx_downloads(path_to_scan: str, path_to_exclude: list = []) -> dict:
download_dict = {}
find_word = re.compile(rb"SPDX-PackageDownloadLocation\s*:\s*(\S+)", re.IGNORECASE)

Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ commands =

[testenv:flake8]
deps = flake8
commands = flake8

commands = flake8

0 comments on commit d7986e5

Please sign in to comment.