Skip to content

Commit d7986e5

Browse files
authored
Add type hint (#184)
Signed-off-by: hkkim <[email protected]>
1 parent b3983bb commit d7986e5

File tree

10 files changed

+69
-47
lines changed

10 files changed

+69
-47
lines changed

src/fosslight_source/_help.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
--correct_fpath <path> Path to the sbom-info.yaml file"""
3131

3232

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

3636

37-
def print_help_msg_source_scanner():
37+
def print_help_msg_source_scanner() -> None:
3838
helpMsg = PrintHelpMsg(_HELP_MESSAGE_SOURCE_SCANNER)
3939
helpMsg.print_help_msg(True)

src/fosslight_source/_license_matched.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,41 @@ class MatchedLicense:
2020
matched_text = ""
2121
priority = 0
2222

23-
def __init__(self, lic, category, text, file):
23+
def __init__(self, lic: str, category: str, text: str, file: str) -> None:
2424
self.files = [file]
2525
self.license = lic
2626
self.matched_text = text
2727
self.set_category(category)
2828

29-
def __del__(self):
29+
def __del__(self) -> None:
3030
pass
3131

32-
def set_license(self, value):
32+
def set_license(self, value: str) -> None:
3333
self.license = value
3434

35-
def set_files(self, value):
35+
def set_files(self, value: str) -> None:
3636
if value not in self.files:
3737
self.files.append(value)
3838

39-
def set_category(self, value):
39+
def set_category(self, value: str) -> None:
4040
self.category = value
4141
if value in LOW_PRIORITY:
4242
self.priority = 1
4343
else:
4444
self.priority = 0
4545

46-
def set_matched_text(self, value):
46+
def set_matched_text(self, value: str) -> None:
4747
self.matched_text = value
4848

49-
def get_row_to_print(self, result_for_32_earlier=True):
49+
def get_row_to_print(self, result_for_32_earlier: bool = True) -> list:
5050
if result_for_32_earlier:
5151
print_rows = [self.category, self.license, self.matched_text, str(len(self.files)), ','.join(self.files)]
5252
else:
5353
print_rows = [self.license, self.matched_text, str(len(self.files)), ','.join(self.files)]
5454
return print_rows
5555

5656

57-
def get_license_list_to_print(license_list):
57+
def get_license_list_to_print(license_list: dict) -> list:
5858
result_for_32_earlier = any([value.category for key, value in license_list.items()])
5959
license_items = license_list.values()
6060
license_items = sorted(license_items, key=lambda row: (row.priority, row.category, row.license))

src/fosslight_source/_parsing_scancode_file_item.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ._scan_item import is_exclude_file
1414
from ._scan_item import replace_word
1515
from ._scan_item import is_notice_file
16+
from typing import Tuple
1617

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

3233

33-
def get_error_from_header(header_item):
34+
def get_error_from_header(header_item: list) -> Tuple[bool, str]:
3435
has_error = False
3536
str_error = ""
3637
key_error = "errors"
@@ -49,7 +50,7 @@ def get_error_from_header(header_item):
4950
return has_error, str_error
5051

5152

52-
def parsing_scancode_32_earlier(scancode_file_list, has_error=False):
53+
def parsing_scancode_32_earlier(scancode_file_list: list, has_error: bool = False) -> Tuple[bool, list, list, dict]:
5354
rc = True
5455
msg = []
5556
scancode_file_item = []
@@ -178,15 +179,17 @@ def parsing_scancode_32_earlier(scancode_file_list, has_error=False):
178179
return rc, scancode_file_item, msg, license_list
179180

180181

181-
def split_spdx_expression(spdx_string):
182+
def split_spdx_expression(spdx_string: str) -> list:
182183
license = []
183184
for replace in SPDX_REPLACE_WORDS:
184185
spdx_string = spdx_string.replace(replace, "")
185186
license = re.split(KEY_AND + "|" + KEY_OR, spdx_string)
186187
return license
187188

188189

189-
def parsing_scancode_32_later(scancode_file_list, has_error=False):
190+
def parsing_scancode_32_later(
191+
scancode_file_list: list, has_error: bool = False
192+
) -> Tuple[bool, list, list, dict]:
190193
rc = True
191194
msg = []
192195
scancode_file_item = []
@@ -274,7 +277,9 @@ def parsing_scancode_32_later(scancode_file_list, has_error=False):
274277
return rc, scancode_file_item, msg, license_list
275278

276279

277-
def parsing_file_item(scancode_file_list, has_error, need_matched_license=False):
280+
def parsing_file_item(
281+
scancode_file_list: list, has_error: bool, need_matched_license: bool = False
282+
) -> Tuple[bool, list, list, dict]:
278283

279284
rc = True
280285
msg = []

src/fosslight_source/_parsing_scanoss_file.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
from ._scan_item import SourceItem
1010
from ._scan_item import is_exclude_file
1111
from ._scan_item import replace_word
12+
from typing import Tuple
1213

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

1819

19-
def parsing_extraInfo(scanned_result):
20+
def parsing_extraInfo(scanned_result: dict) -> list:
2021
scanoss_extra_info = []
2122
for scan_item in scanned_result:
2223
license_w_source = scan_item.scanoss_reference
@@ -35,7 +36,7 @@ def parsing_extraInfo(scanned_result):
3536
return scanoss_extra_info
3637

3738

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

src/fosslight_source/_scan_item.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class SourceItem(FileItem):
3030

31-
def __init__(self, value):
31+
def __init__(self, value: str) -> None:
3232
super().__init__("")
3333
self.source_name_or_path = value
3434
self.is_license_text = False
@@ -42,18 +42,18 @@ def __init__(self, value):
4242
self.oss_name = ""
4343
self.oss_version = ""
4444

45-
def __del__(self):
45+
def __del__(self) -> None:
4646
pass
4747

48-
def __hash__(self):
48+
def __hash__(self) -> int:
4949
return hash(self.file)
5050

5151
@property
52-
def licenses(self):
52+
def licenses(self) -> list:
5353
return self._licenses
5454

5555
@licenses.setter
56-
def licenses(self, value):
56+
def licenses(self, value: list) -> None:
5757
if value:
5858
max_length_exceed = False
5959
for new_lic in value:
@@ -70,7 +70,7 @@ def licenses(self, value):
7070
if max_length_exceed and (SUBSTRING_LICENSE_COMMENT not in self.comment):
7171
self.comment = f"{self.comment}/ {SUBSTRING_LICENSE_COMMENT}" if self.comment else SUBSTRING_LICENSE_COMMENT
7272

73-
def set_oss_item(self):
73+
def set_oss_item(self) -> None:
7474
self.oss_items = []
7575
if self.download_location:
7676
for url in self.download_location:
@@ -84,7 +84,7 @@ def set_oss_item(self):
8484
item.comment = self.comment
8585
self.oss_items.append(item)
8686

87-
def get_print_array(self):
87+
def get_print_array(self) -> list:
8888
print_rows = []
8989
for item in self.oss_items:
9090
print_rows.append([self.source_name_or_path, item.name, item.version, ",".join(item.license),
@@ -93,14 +93,14 @@ def get_print_array(self):
9393
self.license_reference])
9494
return print_rows
9595

96-
def __eq__(self, other):
96+
def __eq__(self, other: object) -> bool:
9797
if type(other) == str:
9898
return self.source_name_or_path == other
9999
else:
100100
return self.source_name_or_path == other.source_name_or_path
101101

102102

103-
def is_exclude_dir(dir_path):
103+
def is_exclude_dir(dir_path: str) -> bool:
104104
if dir_path != "":
105105
dir_path = dir_path.lower()
106106
dir_path = dir_path if dir_path.endswith(
@@ -111,7 +111,7 @@ def is_exclude_dir(dir_path):
111111
return False
112112

113113

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

135135

136-
def is_notice_file(file_path):
136+
def is_notice_file(file_path: str) -> bool:
137137
pattern = r"({})(?<!w)".format("|".join(_notice_filename))
138138
file_path = file_path.lower()
139139
filename = os.path.basename(file_path)

src/fosslight_source/cli.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .run_spdx_extractor import get_spdx_downloads
2424
from ._scan_item import SourceItem
2525
from fosslight_util.oss_item import ScannerItem
26+
from typing import Tuple
2627

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

4142

42-
def main():
43+
def main() -> None:
4344
global logger
4445
_result_log = {}
4546

@@ -121,7 +122,7 @@ def main():
121122
sys.exit(1)
122123

123124

124-
def count_files(path_to_scan, path_to_exclude):
125+
def count_files(path_to_scan: str, path_to_exclude: list) -> Tuple[int, int]:
125126
total_files = 0
126127
excluded_files = 0
127128
abs_path_to_exclude = [os.path.abspath(os.path.join(path_to_scan, path)) for path in path_to_exclude]
@@ -138,9 +139,14 @@ def count_files(path_to_scan, path_to_exclude):
138139
return total_files, excluded_files
139140

140141

141-
def create_report_file(_start_time, merged_result, license_list, scanoss_result, selected_scanner, need_license=False,
142-
output_path="", output_files=[], output_extensions=[], correct_mode=True, correct_filepath="",
143-
path_to_scan="", path_to_exclude=[]):
142+
def create_report_file(
143+
_start_time: str, merged_result: list,
144+
license_list: list, scanoss_result: list,
145+
selected_scanner: str, need_license: bool = False,
146+
output_path: str = "", output_files: list = [],
147+
output_extensions: list = [], correct_mode: bool = True,
148+
correct_filepath: str = "", path_to_scan: str = "", path_to_exclude: list = []
149+
) -> 'ScannerItem':
144150
"""
145151
Create report files for given scanned result.
146152
@@ -227,7 +233,7 @@ def create_report_file(_start_time, merged_result, license_list, scanoss_result,
227233
return scan_item
228234

229235

230-
def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
236+
def merge_results(scancode_result: list = [], scanoss_result: list = [], spdx_downloads: dict = {}) -> list:
231237
"""
232238
Merge scanner results and spdx parsing result.
233239
:param scancode_result: list of scancode results in SourceItem.
@@ -257,9 +263,14 @@ def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
257263
return scancode_result
258264

259265

260-
def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_cores=-1, called_by_cli=True,
261-
print_matched_text=False, formats=[], time_out=120, correct_mode=True, correct_filepath="",
262-
selected_scanner='all', path_to_exclude=[]):
266+
def run_scanners(
267+
path_to_scan: str, output_file_name: str = "",
268+
write_json_file: bool = False, num_cores: int = -1,
269+
called_by_cli: bool = True, print_matched_text: bool = False,
270+
formats: list = [], time_out: int = 120,
271+
correct_mode: bool = True, correct_filepath: str = "",
272+
selected_scanner: str = 'all', path_to_exclude: list = []
273+
) -> Tuple[bool, str, 'ScannerItem', list, list]:
263274
"""
264275
Run Scancode and scanoss.py for the given path.
265276

src/fosslight_source/run_scancode.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616
from ._parsing_scancode_file_item import get_error_from_header
1717
from fosslight_util.output_format import check_output_formats
1818
from fosslight_binary.binary_analysis import check_binary
19+
from typing import Tuple
1920

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

2425

25-
def run_scan(path_to_scan, output_file_name="",
26-
_write_json_file=False, num_cores=-1, return_results=False, need_license=False, formats=[],
27-
called_by_cli=False, time_out=120, correct_mode=True, correct_filepath="", path_to_exclude=[]):
26+
def run_scan(
27+
path_to_scan: str, output_file_name: str = "",
28+
_write_json_file: bool = False, num_cores: int = -1,
29+
return_results: bool = False, need_license: bool = False,
30+
formats: list = [], called_by_cli: bool = False,
31+
time_out: int = 120, correct_mode: bool = True,
32+
correct_filepath: str = "", path_to_exclude: list = []
33+
) -> Tuple[bool, str, list, list]:
2834
if not called_by_cli:
2935
global logger
3036

src/fosslight_source/run_scanoss.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
SCANOSS_OUTPUT_FILE = "scanoss_raw_result.json"
2626

2727

28-
def get_scanoss_extra_info(scanned_result):
28+
def get_scanoss_extra_info(scanned_result: dict) -> list:
2929
return parsing_extraInfo(scanned_result)
3030

3131

32-
def run_scanoss_py(path_to_scan, output_file_name="", format="", called_by_cli=False,
33-
write_json_file=False, num_threads=-1, path_to_exclude=[]):
32+
def run_scanoss_py(path_to_scan: str, output_file_name: str = "", format: str = "", called_by_cli: bool = False,
33+
write_json_file: bool = False, num_threads: int = -1, path_to_exclude: list = []) -> list:
3434
"""
3535
Run scanoss.py for the given path.
3636

src/fosslight_source/run_spdx_extractor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
logger = logging.getLogger(constant.LOGGER_NAME)
1313

1414

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

2828

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

tox.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@ commands =
5050

5151
[testenv:flake8]
5252
deps = flake8
53-
commands = flake8
54-
53+
commands = flake8

0 commit comments

Comments
 (0)