1414from bs4 import BeautifulSoup
1515import fosslight_util .constant as constant
1616from fosslight_util .compare_yaml import compare_yaml
17+ from fosslight_util .convert_excel_to_yaml import convert_excel_to_yaml
1718
1819logger = logging .getLogger (constant .LOGGER_NAME )
1920ADD = "add"
2021DELETE = "delete"
2122CHANGE = "change"
2223COMP_STATUS = [ADD , DELETE , CHANGE ]
2324
25+ JSON_EXT = '.json'
26+ YAML_EXT = '.yaml'
27+ HTML_EXT = '.html'
28+ XLSX_EXT = '.xlsx'
29+
2430
2531def write_result_json_yaml (output_file , compared_result , file_ext ):
2632 ret = True
2733 try :
2834 with open (output_file , 'w' ) as f :
29- if file_ext == '.json' :
35+ if file_ext == JSON_EXT :
3036 json .dump (compared_result , f , indent = 4 , sort_keys = True )
31- elif file_ext == '.yaml' :
37+ elif file_ext == YAML_EXT :
3238 yaml .dump (compared_result , f , sort_keys = True )
3339 except Exception :
3440 ret = False
@@ -65,7 +71,7 @@ def parse_result_for_table(oi, status):
6571
6672def get_sample_html ():
6773 RESOURCES_DIR = 'resources'
68- SAMPLE_HTML = 'bom_compare.html '
74+ SAMPLE_HTML = f 'bom_compare{ HTML_EXT } '
6975 html_file = os .path .join (RESOURCES_DIR , SAMPLE_HTML )
7076 html_f = ''
7177
@@ -83,14 +89,14 @@ def get_sample_html():
8389 return html_f
8490
8591
86- def write_result_html (output_file , compared_result , before_yaml , after_yaml ):
92+ def write_result_html (output_file , compared_result , before_f , after_f ):
8793 ret = True
8894 html_f = get_sample_html ()
8995 if html_f != '' :
9096 try :
9197 f = BeautifulSoup (html_f .read (), 'html.parser' )
92- f .find ("li" , {"class" : "before_f" }).append (before_yaml )
93- f .find ("li" , {"class" : "after_f" }).append (after_yaml )
98+ f .find ("li" , {"class" : "before_f" }).append (before_f )
99+ f .find ("li" , {"class" : "after_f" }).append (after_f )
94100
95101 table_html = f .find ("table" , {"id" : "comp_result" })
96102
@@ -170,22 +176,22 @@ def write_result_xlsx(output_file, compared_result):
170176 return ret
171177
172178
173- def write_compared_result (output_file , compared_result , file_ext , before_yaml = '' , after_yaml = '' ):
179+ def write_compared_result (output_file , compared_result , file_ext , before_f = '' , after_f = '' ):
174180 success = False
175- if file_ext == "" or file_ext == ".xlsx" :
181+ if file_ext == "" or file_ext == XLSX_EXT :
176182 success = write_result_xlsx (output_file , compared_result )
177- elif file_ext == ".html" :
178- output_xlsx_file = os .path .splitext (output_file )[0 ] + ".xlsx"
183+ elif file_ext == HTML_EXT :
184+ output_xlsx_file = f' { os .path .splitext (output_file )[0 ]} { XLSX_EXT } '
179185 success_xlsx = write_result_xlsx (output_xlsx_file , compared_result )
180- success = write_result_html (output_file , compared_result , before_yaml , after_yaml )
186+ success = write_result_html (output_file , compared_result , before_f , after_f )
181187 if not success_xlsx :
182188 logger .error ("Fail to write comparison excel file." )
183189 else :
184190 logger .info (f"In html format, { output_xlsx_file } is generated by default." )
185191 output_file = f"{ output_xlsx_file } , { output_file } "
186- elif file_ext == ".json" :
192+ elif file_ext == JSON_EXT :
187193 success = write_result_json_yaml (output_file , compared_result , file_ext )
188- elif file_ext == ".yaml" :
194+ elif file_ext == YAML_EXT :
189195 success = write_result_json_yaml (output_file , compared_result , file_ext )
190196 else :
191197 logger .info ("Not supported file extension" )
@@ -198,14 +204,14 @@ def get_comparison_result_filename(output_path, output_file, output_extension, _
198204 if output_file != "" :
199205 result_file = f"{ output_file } { output_extension } "
200206 else :
201- if output_extension == '.xlsx' or output_extension == "" :
202- result_file = f"FOSSLight_Compare_{ _start_time } .xlsx "
203- elif output_extension == '.html' :
204- result_file = f"FOSSLight_Compare_{ _start_time } .html "
205- elif output_extension == '.yaml' :
206- result_file = f"FOSSLight_Compare_{ _start_time } .yaml "
207- elif output_extension == '.json' :
208- result_file = f"FOSSLight_Compare_{ _start_time } .json "
207+ if output_extension == XLSX_EXT or output_extension == "" :
208+ result_file = f"FOSSLight_Compare_{ _start_time } { XLSX_EXT } "
209+ elif output_extension == HTML_EXT :
210+ result_file = f"FOSSLight_Compare_{ _start_time } { HTML_EXT } "
211+ elif output_extension == YAML_EXT :
212+ result_file = f"FOSSLight_Compare_{ _start_time } { YAML_EXT } "
213+ elif output_extension == JSON_EXT :
214+ result_file = f"FOSSLight_Compare_{ _start_time } { JSON_EXT } "
209215 else :
210216 logger .error ("Not supported file extension" )
211217
@@ -224,13 +230,29 @@ def count_compared_result(compared_result):
224230 logger .info (f"Comparison result: { count_str } " )
225231
226232
227- def run_compare (before_yaml , after_yaml , output_path , output_file , file_ext , _start_time ):
233+ def run_compare (before_f , after_f , output_path , output_file , file_ext , _start_time ):
228234 ret = False
229235 logger .info ("Start compare mode" )
230- logger .info (f"before file: { before_yaml } " )
231- logger .info (f"after file: { after_yaml } " )
236+ logger .info (f"before file: { before_f } " )
237+ logger .info (f"after file: { after_f } " )
238+
239+ before_ext = f'.{ os .path .basename (before_f ).split ("." )[- 1 ]} '
240+ after_ext = f'.{ os .path .basename (after_f ).split ("." )[- 1 ]} '
241+ if before_ext != after_ext :
242+ logger .error ("Please enter the two FOSSLight report with the same file extension." )
243+ return False
244+ if before_ext not in [YAML_EXT , XLSX_EXT ]:
245+ logger .error (f"Compare mode only supports 'yaml' or 'xlsx' extension. (input extension:{ before_ext } )" )
246+ return False
247+ else :
248+ before_yaml = before_f if before_ext == YAML_EXT else f'{ before_f .rstrip (XLSX_EXT )} { YAML_EXT } '
249+ after_yaml = after_f if after_ext == YAML_EXT else f'{ after_f .rstrip (XLSX_EXT )} { YAML_EXT } '
232250
233251 result_file = get_comparison_result_filename (output_path , output_file , file_ext , _start_time )
252+
253+ if before_ext == XLSX_EXT :
254+ convert_excel_to_yaml (before_f , before_yaml )
255+ convert_excel_to_yaml (after_f , after_yaml )
234256 compared_result = compare_yaml (before_yaml , after_yaml )
235257 if compared_result != '' :
236258 count_compared_result (compared_result )
0 commit comments