From 797fc7ca29ae82c8ef725f78c63d15d26aeda242 Mon Sep 17 00:00:00 2001 From: ParkSangsin Date: Sun, 4 Aug 2024 14:33:28 +0900 Subject: [PATCH] Add type hints to each function Signed-off-by: ParkSangsin --- src/fosslight_oss_pkg/_convert.py | 7 ++- src/fosslight_oss_pkg/_parsing_excel.py | 9 ++- src/fosslight_prechecker/_add.py | 70 +++++++++++++++++------- src/fosslight_prechecker/_help.py | 2 +- src/fosslight_prechecker/_precheck.py | 46 +++++++++++----- src/fosslight_prechecker/_result_html.py | 27 ++++++--- src/fosslight_prechecker/cli.py | 2 +- 7 files changed, 118 insertions(+), 45 deletions(-) diff --git a/src/fosslight_oss_pkg/_convert.py b/src/fosslight_oss_pkg/_convert.py index 495a01e..a7f5dde 100755 --- a/src/fosslight_oss_pkg/_convert.py +++ b/src/fosslight_oss_pkg/_convert.py @@ -19,7 +19,12 @@ logger = logging.getLogger(LOGGER_NAME) -def convert_report(base_path, output_name, format, need_log_file=True): +def convert_report( + base_path: str, + output_name: str, + format: str, + need_log_file=True +) -> None: oss_yaml_files = [] file_option_on = False output_report = "" diff --git a/src/fosslight_oss_pkg/_parsing_excel.py b/src/fosslight_oss_pkg/_parsing_excel.py index 0fd2d8d..a3fedfe 100644 --- a/src/fosslight_oss_pkg/_parsing_excel.py +++ b/src/fosslight_oss_pkg/_parsing_excel.py @@ -17,7 +17,7 @@ MAX_SHEET_NAME_LEN = 31 -def get_sheet_name(yaml_file, sheet_list): +def get_sheet_name(yaml_file: str, sheet_list: dict[str, list]) -> str: if len(yaml_file) > MAX_SHEET_NAME_LEN: yaml_file = yaml_file[0:MAX_SHEET_NAME_LEN] @@ -33,7 +33,12 @@ def get_sheet_name(yaml_file, sheet_list): return yaml_file -def convert_yml_to_excel(oss_yaml_files, output_file, file_option_on, base_path): +def convert_yml_to_excel( + oss_yaml_files: list[str], + output_file: str, + file_option_on: bool, + base_path: str +) -> None: sheet_list = {} header = {} diff --git a/src/fosslight_prechecker/_add.py b/src/fosslight_prechecker/_add.py index 79da87a..9a83f91 100644 --- a/src/fosslight_prechecker/_add.py +++ b/src/fosslight_prechecker/_add.py @@ -9,6 +9,7 @@ import sys import fosslight_util.constant as constant import urllib.request +import argparse from yaml import safe_dump from fosslight_util.set_log import init_log from fosslight_util.spdx_licenses import get_spdx_licenses_json, get_license_from_nick @@ -37,13 +38,13 @@ logger = logging.getLogger(constant.LOGGER_NAME) -def convert_to_spdx_style(input_string): +def convert_to_spdx_style(input_string: str) -> str: input_string = input_string.replace(" ", "-") input_converted = f"LicenseRef-{input_string}" return input_converted -def check_input_license_format(input_license): +def check_input_license_format(input_license: str) -> str: for spdx in spdx_licenses: if input_license.casefold() == spdx.casefold(): return spdx @@ -66,7 +67,7 @@ def check_input_license_format(input_license): return converted_license -def check_input_copyright_format(input_copyright): +def check_input_copyright_format(input_copyright: str) -> bool: regex = re.compile(r'Copyright(\s)+(\(c\)\s)?\s*\d{4}(-\d{4})*(\s)+(\S)+') check_ok = True @@ -77,7 +78,7 @@ def check_input_copyright_format(input_copyright): return check_ok -def input_license_while_running(): +def input_license_while_running() -> str: input_license = "" logger.info("# Select a license to write in the license missing files ") @@ -95,7 +96,7 @@ def input_license_while_running(): return input_license -def input_copyright_while_running(): +def input_copyright_while_running() -> None | str: input_copyright = "" input_copyright = input("# Input Copyright to write in the copyright missing files (ex, ): ") if input_copyright == 'Quit' or input_copyright == 'quit' or input_copyright == 'Q': @@ -104,7 +105,7 @@ def input_copyright_while_running(): return input_copyright -def input_dl_url_while_running(): +def input_dl_url_while_running() -> None | str: input_dl_url = "" input_dl_url = input("# Input Download URL to write to missing files (ex, https://github.com/fosslight/fosslight-prechecker): ") if input_dl_url == 'Quit' or input_dl_url == 'quit' or input_dl_url == 'Q': @@ -113,7 +114,13 @@ def input_dl_url_while_running(): return input_dl_url -def add_dl_url_into_file(main_parser, project, path_to_find, input_dl_url, file_list): +def add_dl_url_into_file( + main_parser: argparse.ArgumentParser, + project: Project, + path_to_find: str, + input_dl_url: str, + file_list: list[str] +) -> None: logger.info("\n# Adding Download Location into your files") logger.warning(f" * Your input DownloadLocation : {input_dl_url}") add_dl_url_list = [os.path.join(path_to_find, file) for file in file_list] @@ -125,7 +132,12 @@ def add_dl_url_into_file(main_parser, project, path_to_find, input_dl_url, file_ dump_error_msg(f"Error_to_add_url : {ex}") -def add_license_into_file(main_parser, project, input_license, file_list): +def add_license_into_file( + main_parser: argparse.ArgumentParser, + project: Project, + input_license: str, + file_list: list[str] +) -> None: converted_license = check_input_license_format(input_license) logger.warning(f" * Your input license : {converted_license}") parsed_args = main_parser.parse_args(['addheader', '--license', str(converted_license)] + file_list) @@ -135,7 +147,12 @@ def add_license_into_file(main_parser, project, input_license, file_list): dump_error_msg(f"Error_call_run_in_license : {ex}") -def add_copyright_into_file(main_parser, project, input_copyright, file_list): +def add_copyright_into_file( + main_parser: argparse.ArgumentParser, + project: Project, + input_copyright: str, + file_list: list[str] +) -> None: input_copyright = f"Copyright {input_copyright}" input_ok = check_input_copyright_format(input_copyright) @@ -152,8 +169,16 @@ def add_copyright_into_file(main_parser, project, input_copyright, file_list): dump_error_msg(f"Error_call_run_in_copyright : {ex}") -def set_missing_license_copyright(missing_license_filtered, missing_copyright_filtered, project, - path_to_find, license, copyright, total_files_excluded, input_dl_url): +def set_missing_license_copyright( + missing_license_filtered: list[str] | None, + missing_copyright_filtered: list[str] | None, + project: Project, + path_to_find: str, + license: str, + copyright: str, + total_files_excluded: list[str], + input_dl_url: str +) -> None: input_license = "" input_copyright = "" @@ -218,7 +243,7 @@ def get_allfiles_list(path): dump_error_msg(f"Error - get all files list : {ex}") -def save_result_log(): +def save_result_log() -> None: try: _str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True) logger.info(_str_final_result_log) @@ -226,7 +251,7 @@ def save_result_log(): logger.warning(f"Failed to print add result log. : {ex}") -def copy_to_root(path_to_find, input_license): +def copy_to_root(path_to_find: str, input_license: str) -> None: lic_file = f"{input_license}.txt" try: source = os.path.join(path_to_find, 'LICENSES', f'{lic_file}') @@ -236,7 +261,7 @@ def copy_to_root(path_to_find, input_license): dump_error_msg(f"Error - Can't copy license file: {ex}") -def lge_lic_download(path_to_find, input_license): +def lge_lic_download(path_to_find: str, input_license: str) -> bool: success = False input_license_url = input_license.replace(' ', '_').replace('/', '_').replace('LicenseRef-', '').replace('-', '_') @@ -269,7 +294,7 @@ def lge_lic_download(path_to_find, input_license): return success -def present_license_file(path_to_find, lic): +def present_license_file(path_to_find: str, lic: str) -> bool: present = False lic_file_path = os.path.join(os.getcwd(), path_to_find, 'LICENSES') file_name = f"{lic}.txt" @@ -278,7 +303,7 @@ def present_license_file(path_to_find, lic): return present -def find_representative_license(path_to_find, input_license): +def find_representative_license(path_to_find: str, input_license: str) -> None: files = [] found_file = [] found_license_file = False @@ -324,7 +349,7 @@ def find_representative_license(path_to_find, input_license): dump_error_msg(f"Error - download representative license text: {ex}") -def is_exclude_dir(dir_path): +def is_exclude_dir(dir_path: str) -> bool | None: if dir_path != "": dir_path = dir_path.lower() dir_path = dir_path if dir_path.endswith( @@ -335,7 +360,7 @@ def is_exclude_dir(dir_path): return -def download_oss_info_license(base_path, input_license=""): +def download_oss_info_license(base_path: str, input_license: str = "") -> None: license_list = [] converted_lic_list = [] oss_yaml_files = [] @@ -370,7 +395,14 @@ def download_oss_info_license(base_path, input_license=""): logger.info(" # There is no license in the path \n") -def add_content(target_path="", input_license="", input_copyright="", input_dl_url="", output_path="", need_log_file=True): +def add_content( + target_path: str = "", + input_license: str = "", + input_copyright: str = "", + input_dl_url: str = "", + output_path: str = "", + need_log_file: bool = True +) -> None: global _result_log, spdx_licenses _check_only_file_mode = False file_to_check_list = [] diff --git a/src/fosslight_prechecker/_help.py b/src/fosslight_prechecker/_help.py index 0e49197..ea4e161 100644 --- a/src/fosslight_prechecker/_help.py +++ b/src/fosslight_prechecker/_help.py @@ -40,6 +40,6 @@ -u \t Download Location to add(ex, "https://github.com/fosslight/fosslight_prechecker")""" -def print_help_msg(exitOpt=True): +def print_help_msg(exitOpt: bool = True) -> None: helpMsg = PrintHelpMsg(_HELP_MESSAGE_PRECHECKER) helpMsg.print_help_msg(exitOpt) diff --git a/src/fosslight_prechecker/_precheck.py b/src/fosslight_prechecker/_precheck.py index 5ab78f0..754968f 100755 --- a/src/fosslight_prechecker/_precheck.py +++ b/src/fosslight_prechecker/_precheck.py @@ -11,6 +11,7 @@ import re import subprocess from datetime import datetime +from typing import Tuple from binaryornot.check import is_binary import fosslight_util.constant as constant from fosslight_util.set_log import init_log @@ -35,7 +36,7 @@ logger = logging.getLogger(constant.LOGGER_NAME) -def exclude_untracked_files(path): +def exclude_untracked_files(path: str) -> None: global DEFAULT_EXCLUDE_EXTENSION_FILES try: cmd_result = subprocess.check_output(['git', 'ls-files', '-o'], universal_newlines=True) @@ -49,7 +50,7 @@ def exclude_untracked_files(path): logger.warning(f"Error to get git untracked files : {ex}") -def exclude_gitignore_files(path): +def exclude_gitignore_files(path: str) -> None: global DEFAULT_EXCLUDE_EXTENSION_FILES try: root_path = VCSStrategyGit.find_root(os.getcwd()) @@ -70,7 +71,7 @@ def exclude_gitignore_files(path): logger.warning(f"Error to get git ignored files : {ex}") -def exclude_git_related_files(path): +def exclude_git_related_files(path: str) -> None: try: # Change currnt path for git command current_path = os.getcwd() @@ -87,7 +88,7 @@ def exclude_git_related_files(path): logger.warning(f"Error to get git related files : {ex}") -def find_oss_pkg_info_and_exclude_file(path): +def find_oss_pkg_info_and_exclude_file(path: str) -> list[str]: global DEFAULT_EXCLUDE_EXTENSION_FILES oss_pkg_info = [] git_present = shutil.which("git") @@ -138,7 +139,7 @@ def find_oss_pkg_info_and_exclude_file(path): return oss_pkg_info -def create_reuse_dep5_file(path): +def create_reuse_dep5_file(path: str) -> Tuple[bool, str, str]: global DEFAULT_EXCLUDE_EXTENSION_FILES # Create .reuse/dep5 for excluding directories from reuse. _DEFAULT_CONFIG_PREFIX = "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\nUpstream-Name: \ @@ -180,7 +181,11 @@ def create_reuse_dep5_file(path): return need_rollback, file_to_remove, dir_to_remove -def remove_reuse_dep5_file(rollback, file_to_remove, temp_dir_name): +def remove_reuse_dep5_file( + rollback: bool, + file_to_remove: str, + temp_dir_name: str +) -> None: try: if rollback: _origin_file = os.path.join(os.path.dirname(file_to_remove), os.path.basename(REUSE_CONFIG_FILE)) @@ -195,7 +200,7 @@ def remove_reuse_dep5_file(rollback, file_to_remove, temp_dir_name): dump_error_msg(f"Error_Remove_Dep5 : {ex}") -def precheck_for_files(path, files): +def precheck_for_files(path: str, files: list[str]) -> Tuple[list[str], list[str], Project]: global DEFAULT_EXCLUDE_EXTENSION_FILES missing_license_list = [] @@ -238,7 +243,7 @@ def precheck_for_files(path, files): return missing_license_list, missing_copyright_list, prj -def precheck_for_project(path_to_find): +def precheck_for_project(path_to_find: str) -> Tuple[list[str], list[str], list[str], Project, ProjectReport]: missing_license = [] missing_copyright = [] @@ -272,7 +277,7 @@ def precheck_for_project(path_to_find): return missing_license, missing_copyright, oss_pkg_info_files, project, report -def filter_missing_list(missing_list): +def filter_missing_list(missing_list: list[str]) -> list[str]: filtered_list = [] for file in missing_list: abs_path = os.path.abspath(file) @@ -281,7 +286,7 @@ def filter_missing_list(missing_list): return filtered_list -def dump_error_msg(error_msg: str, exit=False): +def dump_error_msg(error_msg: str, exit=False) -> None: global error_items error_items.append(error_msg) if exit: @@ -289,7 +294,13 @@ def dump_error_msg(error_msg: str, exit=False): sys.exit(1) -def init(path_to_find, output_path, file_list, need_log_file=True, exclude_path=[]): +def init( + path_to_find: str, + output_path: str, + file_list: list[str], + need_log_file=True, + exclude_path=[] +) -> None: global logger, _result_log if file_list: @@ -299,7 +310,7 @@ def init(path_to_find, output_path, file_list, need_log_file=True, exclude_path= need_log_file, logging.INFO, logging.DEBUG, PKG_NAME, path_to_find, exclude_path) -def get_path_to_find(target_path, _check_only_file_mode): +def get_path_to_find(target_path: str, _check_only_file_mode: bool) -> Tuple[str, list[str], bool]: is_file = False is_folder = False file_to_check_list = [] @@ -328,7 +339,7 @@ def get_path_to_find(target_path, _check_only_file_mode): return path_to_find, file_to_check_list, _check_only_file_mode -def set_exclude_list(path_to_find, exclude_path): +def set_exclude_list(path_to_find: str, exclude_path: list[str]): global user_exclude_list for path in exclude_path: @@ -336,7 +347,14 @@ def set_exclude_list(path_to_find, exclude_path): user_exclude_list.append(os.path.abspath(os.path.join(path_to_find, path))) -def run_lint(target_path, disable, output_file_name, format='', need_log_file=True, exclude_path=[]): +def run_lint( + target_path: str, + disable: bool, + output_file_name: str, + format='', + need_log_file=True, + exclude_path=[] +) -> None: global _turn_on_exclude_config, _check_only_file_mode, _start_time file_to_check_list = [] diff --git a/src/fosslight_prechecker/_result_html.py b/src/fosslight_prechecker/_result_html.py index fb3b5ec..177cdc3 100644 --- a/src/fosslight_prechecker/_result_html.py +++ b/src/fosslight_prechecker/_result_html.py @@ -7,9 +7,10 @@ from reuse.project import Project from fosslight_prechecker._constant import HTML_FORMAT_PREFIX, HTML_CELL_PREFIX, HTML_FORMAT_SUFFIX, HTML_EXPAND_PREFIX,\ HTML_COMPLIANCE_SUFFIX, HTML_RESULT_PRINT_LIMIT, HTML_RESULT_EXPAND_LIMIT, HTML_CELL_HEAD_ROW +from fosslight_prechecker._result import ResultItem -def check_length_of_print_list(input_list: list, list_len: int): +def check_length_of_print_list(input_list: list, list_len: int) -> str: print_cnt = 0 print_str = "" if not input_list: @@ -30,7 +31,7 @@ def check_length_of_print_list(input_list: list, list_len: int): return print_str -def get_html_summary(result_item): +def get_html_summary(result_item: ResultItem) -> str: pkg_file_str = check_length_of_print_list(result_item._oss_pkg_files, len(result_item._oss_pkg_files)) detected_lic_str = check_length_of_print_list(result_item._detected_licenses, len(result_item._detected_licenses)) @@ -44,21 +45,29 @@ def get_html_summary(result_item): return html_lint_str -def get_html_compliance(result_item): +def get_html_compliance(result_item: ResultItem) -> str: return f"Compliant: {result_item.compliant_result}{HTML_COMPLIANCE_SUFFIX}" -def get_num_of_not_compliant(result_item): +def get_num_of_not_compliant(result_item: ResultItem) -> int: return int(result_item._count_without_lic) + int(result_item._count_without_cop) -def get_file_report(project, path_to_find, file): +def get_file_report( + project: Project, + path_to_find: str, + file: str +) -> str: file_abs_path = os.path.abspath(os.path.join(path_to_find, file)) file_rep = report.FileReport.generate(project, file_abs_path) return file_rep -def get_html_cell(result_item, project: Project, path_to_find): +def get_html_cell( + result_item: ResultItem, + project: Project, + path_to_find: str +) -> str: cell_str = "" for both_file in result_item._files_without_both: cell_str += f""" @@ -87,7 +96,11 @@ def get_html_cell(result_item, project: Project, path_to_find): return cell_str -def result_for_html(result_item, project: Project, path_to_find): +def result_for_html( + result_item: ResultItem, + project: Project, + path_to_find: str +) -> str: compliance_str = get_html_compliance(result_item) summary_str = get_html_summary(result_item) cell_contents_str = "" diff --git a/src/fosslight_prechecker/cli.py b/src/fosslight_prechecker/cli.py index 2426ad5..e79efbb 100644 --- a/src/fosslight_prechecker/cli.py +++ b/src/fosslight_prechecker/cli.py @@ -13,7 +13,7 @@ from fosslight_prechecker._precheck import run_lint -def run_main(mode, path, output, format, no_log, disable, copyright, license, dl_url, parser, exclude_path): +def run_main(mode : str, path, output, format, no_log, disable, copyright, license, dl_url, parser, exclude_path): if mode != "add" and (copyright != "" or license != ""): parser.print_help() sys.exit(1)