diff --git a/src/fosslight_scanner/_get_input.py b/src/fosslight_scanner/_get_input.py index 08fb904..7922601 100755 --- a/src/fosslight_scanner/_get_input.py +++ b/src/fosslight_scanner/_get_input.py @@ -4,11 +4,12 @@ # SPDX-License-Identifier: Apache-2.0 import sys from pip._vendor.distlib.compat import raw_input +from typing import Tuple, List _PYTHON_VERSION = 3 -def get_input(ask_msg, default_value=""): +def get_input(ask_msg: str, default_value: str ="") -> str: return_value = raw_input( ask_msg) if _PYTHON_VERSION < 3 else input(ask_msg) @@ -18,14 +19,14 @@ def get_input(ask_msg, default_value=""): return return_value -def ask_to_run(ask_msg): +def ask_to_run(ask_msg: str) -> bool: return_true_item = ["y", "Y", "1"] return_value = raw_input( ask_msg) if _PYTHON_VERSION < 3 else input(ask_msg) return return_value in return_true_item -def get_input_mode(_executed_path, mode_list=["all"]): +def get_input_mode(_executed_path: str, mode_list: List[str] =["all"]) -> Tuple[str, str, str]: global _PYTHON_VERSION _PYTHON_VERSION = sys.version_info[0] diff --git a/src/fosslight_scanner/_help.py b/src/fosslight_scanner/_help.py index 27c3a29..364b10b 100644 --- a/src/fosslight_scanner/_help.py +++ b/src/fosslight_scanner/_help.py @@ -48,6 +48,6 @@ -d \t Additional arguments for running dependency analysis""" -def print_help_msg(): +def print_help_msg() -> None: helpMsg = PrintHelpMsg(_HELP_MESSAGE_SCANNER) helpMsg.print_help_msg(True) diff --git a/src/fosslight_scanner/_parse_setting.py b/src/fosslight_scanner/_parse_setting.py index 83c73ad..34c6385 100644 --- a/src/fosslight_scanner/_parse_setting.py +++ b/src/fosslight_scanner/_parse_setting.py @@ -2,9 +2,10 @@ # -*- coding: utf-8 -*- # Copyright (c) 2021 LG Electronics Inc. # SPDX-License-Identifier: Apache-2.0 +from typing import Tuple, List -def parse_setting_json(data): +def parse_setting_json(data: dict) -> Tuple[List[str], List[str], str, str, str, str, str, bool, bool, int, bool, str, bool, List[str]]: # check type, if invalid = init value mode = data.get('mode', []) path = data.get('path', []) diff --git a/src/fosslight_scanner/_run_compare.py b/src/fosslight_scanner/_run_compare.py index d547796..8abe97c 100644 --- a/src/fosslight_scanner/_run_compare.py +++ b/src/fosslight_scanner/_run_compare.py @@ -15,6 +15,7 @@ import fosslight_util.constant as constant from fosslight_util.compare_yaml import compare_yaml from fosslight_util.convert_excel_to_yaml import convert_excel_to_yaml +from typing import Tuple, Union, List, Dict, Any logger = logging.getLogger(constant.LOGGER_NAME) ADD = "add" @@ -28,7 +29,7 @@ XLSX_EXT = '.xlsx' -def write_result_json_yaml(output_file, compared_result, file_ext): +def write_result_json_yaml(output_file: str, compared_result: Dict[str, Any], file_ext: str) -> bool: ret = True try: with open(output_file, 'w') as f: @@ -41,7 +42,7 @@ def write_result_json_yaml(output_file, compared_result, file_ext): return ret -def parse_result_for_table(oi, status): +def parse_result_for_table(oi: Dict[str, Any], status: str) -> List[str]: compared_row = [] if status == ADD or status == DELETE: oi_ver = '' if oi['version'] == '' else f"({oi['version']})" @@ -69,7 +70,7 @@ def parse_result_for_table(oi, status): return compared_row -def get_sample_html(): +def get_sample_html() -> Union[codecs.StreamReaderWriter, str]: RESOURCES_DIR = 'resources' SAMPLE_HTML = f'bom_compare{HTML_EXT}' html_file = os.path.join(RESOURCES_DIR, SAMPLE_HTML) @@ -89,7 +90,7 @@ def get_sample_html(): return html_f -def write_result_html(output_file, compared_result, before_f, after_f): +def write_result_html(output_file: str, compared_result: Dict[str, Any], before_f: str, after_f: str) -> bool: ret = True html_f = get_sample_html() if html_f != '': @@ -147,7 +148,7 @@ def write_result_html(output_file, compared_result, before_f, after_f): return ret -def write_result_xlsx(output_file, compared_result): +def write_result_xlsx(output_file: str, compared_result: Dict[str, Any]) -> bool: HEADER = ['Status', 'OSS_Before', 'License_Before', 'OSS_After', 'License_After'] ret = True @@ -176,7 +177,7 @@ def write_result_xlsx(output_file, compared_result): return ret -def write_compared_result(output_file, compared_result, file_ext, before_f='', after_f=''): +def write_compared_result(output_file: str, compared_result: Dict[str, Any], file_ext: str, before_f: str ='', after_f: str ='') -> Tuple[bool, str]: success = False if file_ext == "" or file_ext == XLSX_EXT: success = write_result_xlsx(output_file, compared_result) @@ -199,7 +200,7 @@ def write_compared_result(output_file, compared_result, file_ext, before_f='', a return success, output_file -def get_comparison_result_filename(output_path, output_file, output_extension, _start_time): +def get_comparison_result_filename(output_path: str, output_file: str, output_extension: str, _start_time: str) -> str: result_file = "" compare_prefix = f"fosslight_compare_{_start_time}" if output_file != "": @@ -221,7 +222,7 @@ def get_comparison_result_filename(output_path, output_file, output_extension, _ return result_file -def count_compared_result(compared_result): +def count_compared_result(compared_result: Dict[str, Any]) -> None: comp_len = [len(compared_result[st]) for st in COMP_STATUS] if sum(comp_len) == 0: count_str = "all oss lists are the same." @@ -231,7 +232,7 @@ def count_compared_result(compared_result): logger.info(f"Comparison result: {count_str}") -def run_compare(before_f, after_f, output_path, output_file, file_ext, _start_time, _output_dir): +def run_compare(before_f: str, after_f: str, output_path: str, output_file: str, file_ext: str, _start_time: str, _output_dir: str) -> bool: ret = False before_yaml = '' after_yaml = '' diff --git a/src/fosslight_scanner/cli.py b/src/fosslight_scanner/cli.py index 2a61c65..201198a 100644 --- a/src/fosslight_scanner/cli.py +++ b/src/fosslight_scanner/cli.py @@ -10,10 +10,12 @@ from ._parse_setting import parse_setting_json from fosslight_util.help import print_package_version import os.path +from typing import Tuple, List, Union -def set_args(mode, path, dep_argument, output, format, link, db_url, timer, - raw, core, no_correction, correct_fpath, ui, setting, exclude_path): +def set_args(mode: List[str], path: List[str], dep_argument: str, output: str, format: str, link: str, db_url: str, + timer: bool, raw: bool, core: int, no_correction: bool, correct_fpath: str, ui: bool, setting: str, + exclude_path: List[str]) -> Tuple[List[str], List[str], str, str, str, str, str, bool, bool, int, bool, str, bool, List[str]]: if setting and os.path.isfile(setting): try: with open(setting, 'r', encoding='utf-8') as file: @@ -43,7 +45,7 @@ def set_args(mode, path, dep_argument, output, format, link, db_url, timer, raw, core, no_correction, correct_fpath, ui, exclude_path -def main(): +def main() -> None: parser = ArgumentParser(description='FOSSLight Scanner', prog='fosslight_scanner', add_help=False) parser.add_argument('mode', nargs='*', help='source| dependency| binary| all| compare', default="") parser.add_argument('--path', '-p', help='Path to analyze (In compare mode, two FOSSLight reports', diff --git a/src/fosslight_scanner/common.py b/src/fosslight_scanner/common.py index 503cba1..ddbf1dd 100755 --- a/src/fosslight_scanner/common.py +++ b/src/fosslight_scanner/common.py @@ -16,6 +16,7 @@ from fosslight_util.read_excel import read_oss_report from fosslight_util.output_format import write_output_file from fosslight_util.oss_item import OssItem +from typing import List, Tuple, Dict, Union, Any logger = logging.getLogger(constant.LOGGER_NAME) SRC_SHEET = 'SRC_FL_Source' @@ -27,7 +28,7 @@ BIN_HIDDEN_HEADER = {'TLSH', "SHA1"} -def copy_file(source, destination): +def copy_file(source: str, destination: str) -> Tuple[bool, str]: copied_file = "" try: shutil.copy(source, destination) @@ -42,7 +43,7 @@ def copy_file(source, destination): return True, copied_file -def run_analysis(path_to_run, params, func, str_run_start, output, exe_path): +def run_analysis(path_to_run: str, params: List[str], func: Any, str_run_start: str, output: str, exe_path: str) -> Any: # This function will be replaced by call_analysis_api(). logger.info("## Start to run "+str_run_start) return_value = "" @@ -62,7 +63,7 @@ def run_analysis(path_to_run, params, func, str_run_start, output, exe_path): return return_value -def call_analysis_api(path_to_run, str_run_start, return_idx, func, *args, **kwargs): +def call_analysis_api(path_to_run: str, str_run_start: str, return_idx: int, func: Any, *args: Any, **kwargs: Any) -> Tuple[bool, List[Any]]: # return_idx == -1 : Raw return value itself logger.info(f"## Start to run {str_run_start}") success = True @@ -93,7 +94,7 @@ def call_analysis_api(path_to_run, str_run_start, return_idx, func, *args, **kwa return success, result -def overwrite_excel(excel_file_path, oss_name, column_name='OSS Name'): +def overwrite_excel(excel_file_path: str, oss_name: str, column_name: str ='OSS Name') -> None: if oss_name != "": try: files = os.listdir(excel_file_path) @@ -115,8 +116,8 @@ def overwrite_excel(excel_file_path, oss_name, column_name='OSS Name'): logger.debug(f"overwrite_excel:{ex}") -def merge_yamls(_output_dir, merge_yaml_files, final_report, remove_src_data=False, - default_oss_name='', default_oss_version='', url=''): +def merge_yamls(_output_dir: str, merge_yaml_files: List[str], final_report: str, remove_src_data: bool =False, + default_oss_name: str ='', default_oss_version: str ='', url: str ='') -> Tuple[bool, Union[str, Exception]]: success = True err_msg = '' @@ -154,7 +155,7 @@ def merge_yamls(_output_dir, merge_yaml_files, final_report, remove_src_data=Fal return success, err_msg -def create_scancodejson(final_report, output_extension, ui_mode_report, src_path=""): +def create_scancodejson(final_report: str, output_extension: str, ui_mode_report: str, src_path: str ="") -> Tuple[bool, str]: success = True err_msg = '' @@ -199,7 +200,7 @@ def create_scancodejson(final_report, output_extension, ui_mode_report, src_path return success, err_msg -def correct_scanner_result(_output_dir, output_files, output_extension, exist_src, exist_bin): +def correct_scanner_result(_output_dir: str, output_files: Dict[str, str], output_extension: str, exist_src: bool, exist_bin: bool) -> None: src_oss_list = [] bin_oss_list = [] duplicates = False @@ -252,7 +253,7 @@ def correct_scanner_result(_output_dir, output_files, output_extension, exist_sr return -def write_output_with_osslist(oss_list, output_dir, output_file, output_extension, sheetname, extended_hdr={}, hidden_hdr={}): +def write_output_with_osslist(oss_list: List[OssItem], output_dir: str, output_file: str, output_extension: str, sheetname: str, extended_hdr: Dict[str, List[str]] ={}, hidden_hdr: set ={}) -> Tuple[bool, str]: new_oss_list = [] sheet_list = {} sheet_list[sheetname] = [] @@ -268,7 +269,7 @@ def write_output_with_osslist(oss_list, output_dir, output_file, output_extensio return success, err_msg -def get_osslist(_output_dir, output_file, output_extension, sheet_name=''): +def get_osslist(_output_dir: str, output_file: str, output_extension: str, sheet_name: str ='') -> List[OssItem]: err_reason = '' oss_list = [] oss_file_with_fullpath = os.path.join(_output_dir, output_file) @@ -285,7 +286,7 @@ def get_osslist(_output_dir, output_file, output_extension, sheet_name=''): return oss_list -def check_exclude_dir(oss_list): +def check_exclude_dir(oss_list: List[OssItem]) -> List[OssItem]: _exclude_dirs = ["venv", "node_modules", "Pods", "Carthage"] for oss_item in oss_list: diff --git a/src/fosslight_scanner/fosslight_scanner.py b/src/fosslight_scanner/fosslight_scanner.py index 964810c..532eae4 100755 --- a/src/fosslight_scanner/fosslight_scanner.py +++ b/src/fosslight_scanner/fosslight_scanner.py @@ -30,6 +30,7 @@ from fosslight_util.write_excel import merge_excels, merge_cover_comment from ._run_compare import run_compare from fosslight_util.cover import CoverItem +from typing import List, Any, Tuple, Dict, Union fosslight_source_installed = True try: from fosslight_source.cli import run_scanners as source_analysis @@ -48,7 +49,7 @@ SCANNER_MODE = ["all", "compare", "reuse", "prechecker", "binary", "bin", "src", "source", "dependency", "dep"] -def run_dependency(path_to_analyze, output_file_with_path, params="", path_to_exclude=[]): +def run_dependency(path_to_analyze: str, output_file_with_path: str, params: str = "", path_to_exclude: List[str] = []) -> List[Any]: result_list = [] package_manager = "" @@ -101,12 +102,12 @@ def run_dependency(path_to_analyze, output_file_with_path, params="", path_to_ex return result_list -def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False, - run_src=True, run_bin=True, run_dep=True, run_prechecker=False, - remove_src_data=True, result_log={}, output_file="", - output_extension="", num_cores=-1, db_url="", - default_oss_name="", default_oss_version="", url="", - correct_mode=True, correct_fpath="", ui_mode=False, path_to_exclude=[]): +def run_scanner(src_path: str, dep_arguments: str, output_path: str, keep_raw_data: bool =False, + run_src: bool =True, run_bin: bool =True, run_dep: bool =True, run_prechecker: bool =False, + remove_src_data: bool =True, result_log: Dict[str, Any] ={}, output_file: str ="", + output_extension: str ="", num_cores: int =-1, db_url: str ="", + default_oss_name: str ="", default_oss_version: str ="", url: str ="", + correct_mode: bool =True, correct_fpath: str ="", ui_mode: bool =False, path_to_exclude: List[str] =[]) ->None: final_excel_dir = output_path success = True temp_output_fiiles = [] @@ -254,7 +255,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False, logger.debug(f"Error to remove temp files:{ex}") -def download_source(link, out_dir): +def download_source(link: str, out_dir: str) -> Tuple[bool, str, str, str]: start_time = datetime.now().strftime('%Y%m%d_%H%M%S') success = False temp_src_dir = "" @@ -281,7 +282,7 @@ def download_source(link, out_dir): return success, temp_src_dir, oss_name, oss_version -def init(output_path="", make_outdir=True): +def init(output_path: str ="", make_outdir: bool =True) -> Tuple[bool, str, Dict[str, Any]]: global _output_dir, _log_file, _start_time, logger result_log = {} @@ -305,9 +306,9 @@ def init(output_path="", make_outdir=True): return os.path.isdir(_output_dir), output_root_dir, result_log -def run_main(mode_list, path_arg, dep_arguments, output_file_or_dir, file_format, url_to_analyze, - db_url, hide_progressbar=False, keep_raw_data=False, num_cores=-1, - correct_mode=True, correct_fpath="", ui_mode=False, path_to_exclude=[]): +def run_main(mode_list: List[str], path_arg: Union[str, List[str]], dep_arguments: str, output_file_or_dir: str, file_format: str, url_to_analyze: str, + db_url, hide_progressbar: bool =False, keep_raw_data: bool =False, num_cores: int=-1, + correct_mode: bool =True, correct_fpath: str ="", ui_mode: bool =False, path_to_exclude: List[str] =[]) -> bool: global _executed_path, _start_time output_file = ""