1010from datetime import datetime
1111from pathlib import Path
1212from yaml import safe_dump
13- from fosslight_prechecker ._help import print_help_msg
1413from fosslight_util .constant import LOGGER_NAME
1514from fosslight_util .set_log import init_log
1615from fosslight_util .output_format import check_output_format
16+ from fosslight_util .parsing_yaml import find_sbom_yaml_files
1717from ._parsing_excel import convert_excel_to_yaml , convert_yml_to_excel
1818
1919CUSTOMIZED_FORMAT_FOR_PRECHECKER = {'yaml' : '.yaml' , 'excel' : '.xlsx' }
@@ -40,12 +40,19 @@ def find_report_file(path_to_find):
4040 return ""
4141
4242
43+ def check_extension_and_format (file , format ):
44+ if (file .endswith ((".yaml" , ".yml" )) and format == "yaml" ) or \
45+ (file .endswith (".xlsx" ) and format == "excel" ):
46+ logger .error (f"File extension is not matched with input format({ format } )" )
47+ sys .exit (1 )
48+
49+
4350def convert_report (base_path , output_name , format , need_log_file = True , sheet_names = "" ):
44- oss_pkg_files = ["oss-pkg-info.yml" , "oss-pkg-info.yaml" ]
51+ oss_yaml_files = []
52+ oss_report_files = "" # TODO: Change to list type for multiple Report files
4553 file_option_on = False
4654 convert_yml_mode = False
4755 convert_excel_mode = False
48- report_to_read = ""
4956 output_report = ""
5057 output_yaml = ""
5158 now = datetime .now ().strftime ('%Y%m%d_%H-%M-%S' )
@@ -68,54 +75,50 @@ def convert_report(base_path, output_name, format, need_log_file=True, sheet_nam
6875 output_yaml = os .path .join (output_path , output_name )
6976 else :
7077 output_report = os .path .join (os .path .abspath (output_path ), f"FOSSLight-Report_{ now } " )
71- output_yaml = os .path .join (os .path .abspath (output_path ), f"oss-pkg -info_{ now } " )
78+ output_yaml = os .path .join (os .path .abspath (output_path ), f"fosslight-sbom -info_{ now } " )
7279 else :
7380 logger .error (f"Format error - { msg } " )
7481 sys .exit (1 )
7582
83+ if base_path == "" :
84+ base_path = output_path
85+
7686 if os .path .isdir (base_path ):
77- if output_extension == ".yaml" :
78- logger .error ("Format error - can make only .xlsx file" )
79- sys .exit (1 )
80- convert_yml_mode = True
87+ oss_yaml_files = find_sbom_yaml_files (base_path )
88+ if oss_yaml_files :
89+ convert_yml_mode = True
8190 else :
8291 if base_path != "" :
83- if base_path .endswith (".xlsx" ):
84- if output_extension == '.xlsx' :
85- logger .error ("Format error - can make only .yaml file" )
86- sys .exit (1 )
87- p = re .compile (r"[\s\S]*OSS[\s\S]*-Report[\s\S]*.xlsx" , re .I )
88- if p .search (base_path ):
92+ files_to_convert = base_path .split ("," )
93+ for file in files_to_convert :
94+ check_extension_and_format (file , format )
95+ if file .endswith (".xlsx" ):
8996 convert_excel_mode = True
90- report_to_read = base_path
91- elif base_path .endswith ((".yaml" , ".yml" )):
92- if output_extension == '.yaml' :
93- logger .error ("Format error - can make only .xlsx file" )
94- sys .exit (1 )
95- p = re .compile (r"oss-pkg-info[\s\S]*.ya?ml" , re .I )
96- if p .search (base_path ):
97- oss_pkg_files = base_path .split (',' )
97+ oss_report_files = file
98+ elif file .endswith ((".yaml" , ".yml" )):
9899 convert_yml_mode = True
99100 file_option_on = True
100- else :
101- logger .error ("Not support file name or extension - only support for FOSSLight-Report*.xlsx or oss-pkg-info*.yaml file" )
102- sys .exit (1 )
101+ oss_yaml_files .append (file )
102+ else :
103+ logger .error ("Not support file name or extension" )
104+ sys .exit (1 )
103105
104106 if not convert_yml_mode and not convert_excel_mode :
105107 if is_window :
106108 convert_yml_mode = True
107109 base_path = os .getcwd ()
108- report_to_read = find_report_file (base_path )
109- if report_to_read != "" :
110+ oss_report_files = find_report_file (base_path )
111+ if oss_report_files != "" :
110112 convert_excel_mode = True
111113 else :
112- print_help_msg ()
114+ logger .info ("fosslight_prechecker: can't convert anything" )
115+ logger .info ("Try 'fosslight_prechecker -h for more information" )
113116
114117 if convert_yml_mode :
115- convert_yml_to_excel (oss_pkg_files , output_report , file_option_on , base_path , is_window )
118+ convert_yml_to_excel (oss_yaml_files , output_report , file_option_on , base_path )
116119
117120 if convert_excel_mode :
118- convert_excel_to_yaml (report_to_read , output_yaml , sheet_names )
121+ convert_excel_to_yaml (oss_report_files , output_yaml , sheet_names )
119122
120123 try :
121124 _str_final_result_log = safe_dump (_result_log , allow_unicode = True , sort_keys = True )
0 commit comments