Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to each function #159

Merged
merged 10 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion src/fosslight_oss_pkg/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: bool = True
) -> None:
oss_yaml_files = []
file_option_on = False
output_report = ""
Expand Down
9 changes: 7 additions & 2 deletions src/fosslight_oss_pkg/_parsing_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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 = {}

Expand Down
70 changes: 51 additions & 19 deletions src/fosslight_prechecker/_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 ")
Expand All @@ -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, <year> <name>): ")
if input_copyright == 'Quit' or input_copyright == 'quit' or input_copyright == 'Q':
Expand All @@ -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':
Expand All @@ -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]
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 = ""

Expand Down Expand Up @@ -218,15 +243,15 @@ 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)
except Exception as ex:
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}')
Expand All @@ -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('-', '_')
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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 = []
Expand Down Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion src/fosslight_prechecker/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
-u <dl_location>\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)
46 changes: 32 additions & 14 deletions src/fosslight_prechecker/_precheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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())
Expand All @@ -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()
Expand All @@ -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")
Expand Down Expand Up @@ -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: \
Expand Down Expand Up @@ -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))
Expand All @@ -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 = []
Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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)
Expand All @@ -281,15 +286,21 @@ 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:
logger.error(error_msg)
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: bool = True,
exclude_path: list = []
) -> None:
global logger, _result_log

if file_list:
Expand All @@ -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 = []
Expand Down Expand Up @@ -328,15 +339,22 @@ 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:
if path.strip != "":
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: str = '',
need_log_file: bool = True,
exclude_path: list = []
) -> None:
global _turn_on_exclude_config, _check_only_file_mode, _start_time

file_to_check_list = []
Expand Down
Loading