99import sys
1010import fosslight_util .constant as constant
1111import urllib .request
12+ import argparse
1213from yaml import safe_dump
1314from fosslight_util .set_log import init_log
1415from fosslight_util .spdx_licenses import get_spdx_licenses_json , get_license_from_nick
2425from reuse .project import Project
2526from bs4 import BeautifulSoup
2627from pathlib import Path
28+ from typing import List , Optional
2729
2830
2931PKG_NAME = "fosslight_prechecker"
3739logger = logging .getLogger (constant .LOGGER_NAME )
3840
3941
40- def convert_to_spdx_style (input_string ) :
42+ def convert_to_spdx_style (input_string : str ) -> str :
4143 input_string = input_string .replace (" " , "-" )
4244 input_converted = f"LicenseRef-{ input_string } "
4345 return input_converted
4446
4547
46- def check_input_license_format (input_license ) :
48+ def check_input_license_format (input_license : str ) -> str :
4749 for spdx in spdx_licenses :
4850 if input_license .casefold () == spdx .casefold ():
4951 return spdx
@@ -66,7 +68,7 @@ def check_input_license_format(input_license):
6668 return converted_license
6769
6870
69- def check_input_copyright_format (input_copyright ) :
71+ def check_input_copyright_format (input_copyright : str ) -> bool :
7072 regex = re .compile (r'Copyright(\s)+(\(c\)\s)?\s*\d{4}(-\d{4})*(\s)+(\S)+' )
7173 check_ok = True
7274
@@ -77,7 +79,7 @@ def check_input_copyright_format(input_copyright):
7779 return check_ok
7880
7981
80- def input_license_while_running ():
82+ def input_license_while_running () -> str :
8183 input_license = ""
8284
8385 logger .info ("# Select a license to write in the license missing files " )
@@ -95,7 +97,7 @@ def input_license_while_running():
9597 return input_license
9698
9799
98- def input_copyright_while_running ():
100+ def input_copyright_while_running () -> Optional [ str ] :
99101 input_copyright = ""
100102 input_copyright = input ("# Input Copyright to write in the copyright missing files (ex, <year> <name>): " )
101103 if input_copyright == 'Quit' or input_copyright == 'quit' or input_copyright == 'Q' :
@@ -104,7 +106,7 @@ def input_copyright_while_running():
104106 return input_copyright
105107
106108
107- def input_dl_url_while_running ():
109+ def input_dl_url_while_running () -> Optional [ str ] :
108110 input_dl_url = ""
109111 input_dl_url = input ("# Input Download URL to write to missing files (ex, https://github.com/fosslight/fosslight-prechecker): " )
110112 if input_dl_url == 'Quit' or input_dl_url == 'quit' or input_dl_url == 'Q' :
@@ -113,7 +115,13 @@ def input_dl_url_while_running():
113115 return input_dl_url
114116
115117
116- def add_dl_url_into_file (main_parser , project , path_to_find , input_dl_url , file_list ):
118+ def add_dl_url_into_file (
119+ main_parser : argparse .ArgumentParser ,
120+ project : Project ,
121+ path_to_find : str ,
122+ input_dl_url : str ,
123+ file_list : List [str ]
124+ ) -> None :
117125 logger .info ("\n # Adding Download Location into your files" )
118126 logger .warning (f" * Your input DownloadLocation : { input_dl_url } " )
119127 add_dl_url_list = [os .path .join (path_to_find , file ) for file in file_list ]
@@ -125,7 +133,12 @@ def add_dl_url_into_file(main_parser, project, path_to_find, input_dl_url, file_
125133 dump_error_msg (f"Error_to_add_url : { ex } " )
126134
127135
128- def add_license_into_file (main_parser , project , input_license , file_list ):
136+ def add_license_into_file (
137+ main_parser : argparse .ArgumentParser ,
138+ project : Project ,
139+ input_license : str ,
140+ file_list : List [str ]
141+ ) -> None :
129142 converted_license = check_input_license_format (input_license )
130143 logger .warning (f" * Your input license : { converted_license } " )
131144 parsed_args = main_parser .parse_args (['addheader' , '--license' , str (converted_license )] + file_list )
@@ -135,7 +148,12 @@ def add_license_into_file(main_parser, project, input_license, file_list):
135148 dump_error_msg (f"Error_call_run_in_license : { ex } " )
136149
137150
138- def add_copyright_into_file (main_parser , project , input_copyright , file_list ):
151+ def add_copyright_into_file (
152+ main_parser : argparse .ArgumentParser ,
153+ project : Project ,
154+ input_copyright : str ,
155+ file_list : List [str ]
156+ ) -> None :
139157 input_copyright = f"Copyright { input_copyright } "
140158
141159 input_ok = check_input_copyright_format (input_copyright )
@@ -152,8 +170,16 @@ def add_copyright_into_file(main_parser, project, input_copyright, file_list):
152170 dump_error_msg (f"Error_call_run_in_copyright : { ex } " )
153171
154172
155- def set_missing_license_copyright (missing_license_filtered , missing_copyright_filtered , project ,
156- path_to_find , license , copyright , total_files_excluded , input_dl_url ):
173+ def set_missing_license_copyright (
174+ missing_license_filtered : Optional [List [str ]],
175+ missing_copyright_filtered : Optional [List [str ]],
176+ project : Project ,
177+ path_to_find : str ,
178+ license : str ,
179+ copyright : str ,
180+ total_files_excluded : List [str ],
181+ input_dl_url : str
182+ ) -> None :
157183 input_license = ""
158184 input_copyright = ""
159185
@@ -218,15 +244,15 @@ def get_allfiles_list(path):
218244 dump_error_msg (f"Error - get all files list : { ex } " )
219245
220246
221- def save_result_log ():
247+ def save_result_log () -> None :
222248 try :
223249 _str_final_result_log = safe_dump (_result_log , allow_unicode = True , sort_keys = True )
224250 logger .info (_str_final_result_log )
225251 except Exception as ex :
226252 logger .warning (f"Failed to print add result log. : { ex } " )
227253
228254
229- def copy_to_root (path_to_find , input_license ) :
255+ def copy_to_root (path_to_find : str , input_license : str ) -> None :
230256 lic_file = f"{ input_license } .txt"
231257 try :
232258 source = os .path .join (path_to_find , 'LICENSES' , f'{ lic_file } ' )
@@ -236,7 +262,7 @@ def copy_to_root(path_to_find, input_license):
236262 dump_error_msg (f"Error - Can't copy license file: { ex } " )
237263
238264
239- def lge_lic_download (path_to_find , input_license ) :
265+ def lge_lic_download (path_to_find : str , input_license : str ) -> bool :
240266 success = False
241267
242268 input_license_url = input_license .replace (' ' , '_' ).replace ('/' , '_' ).replace ('LicenseRef-' , '' ).replace ('-' , '_' )
@@ -269,7 +295,7 @@ def lge_lic_download(path_to_find, input_license):
269295 return success
270296
271297
272- def present_license_file (path_to_find , lic ) :
298+ def present_license_file (path_to_find : str , lic : str ) -> bool :
273299 present = False
274300 lic_file_path = os .path .join (os .getcwd (), path_to_find , 'LICENSES' )
275301 file_name = f"{ lic } .txt"
@@ -278,7 +304,7 @@ def present_license_file(path_to_find, lic):
278304 return present
279305
280306
281- def find_representative_license (path_to_find , input_license ) :
307+ def find_representative_license (path_to_find : str , input_license : str ) -> None :
282308 files = []
283309 found_file = []
284310 found_license_file = False
@@ -324,7 +350,7 @@ def find_representative_license(path_to_find, input_license):
324350 dump_error_msg (f"Error - download representative license text: { ex } " )
325351
326352
327- def is_exclude_dir (dir_path ) :
353+ def is_exclude_dir (dir_path : str ) -> Optional [ bool ] :
328354 if dir_path != "" :
329355 dir_path = dir_path .lower ()
330356 dir_path = dir_path if dir_path .endswith (
@@ -335,7 +361,7 @@ def is_exclude_dir(dir_path):
335361 return
336362
337363
338- def download_oss_info_license (base_path , input_license = "" ):
364+ def download_oss_info_license (base_path : str , input_license : str = "" ) -> None :
339365 license_list = []
340366 converted_lic_list = []
341367 oss_yaml_files = []
@@ -370,7 +396,14 @@ def download_oss_info_license(base_path, input_license=""):
370396 logger .info (" # There is no license in the path \n " )
371397
372398
373- def add_content (target_path = "" , input_license = "" , input_copyright = "" , input_dl_url = "" , output_path = "" , need_log_file = True ):
399+ def add_content (
400+ target_path : str = "" ,
401+ input_license : str = "" ,
402+ input_copyright : str = "" ,
403+ input_dl_url : str = "" ,
404+ output_path : str = "" ,
405+ need_log_file : bool = True
406+ ) -> None :
374407 global _result_log , spdx_licenses
375408 _check_only_file_mode = False
376409 file_to_check_list = []
@@ -450,7 +483,8 @@ def add_content(target_path="", input_license="", input_copyright="", input_dl_u
450483 logger .info (f"\n # File list that have both license and copyright : { len (skip_files )} / { len (total_files_excluded )} " )
451484
452485 # Filter by file extension
453- total_files_excluded = [file for file in total_files_excluded if os .path .splitext (file )[1 ].lower () in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE ]
486+ total_files_excluded = [file for file in total_files_excluded
487+ if os .path .splitext (file )[1 ].lower () in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE ]
454488 missing_license = [file for file in missing_license if os .path .splitext (file )[1 ].lower () in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE ]
455489 missing_copyright = [file for file in missing_copyright if os .path .splitext (file )[1 ].lower () in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE ]
456490
0 commit comments