1212import time
1313import pandas as pd
1414from pathlib import Path
15+ import fosslight_util .constant as constant
1516
1617_HEADER = {'SRC' : ['ID' , 'Source Name or Path' , 'OSS Name' ,
1718 'OSS Version' , 'License' , 'Download Location' ,
2122 'License' , 'Download Location' , 'Homepage' ,
2223 'Copyright Text' , 'Exclude' , 'Comment' ]}
2324_OUTPUT_FILE_PREFIX = "OSS-Report_"
25+ _EMPTY_ITEM_MSG = "* There is no item" \
26+ " to print in OSS-Report.\n "
27+ logger = logging .getLogger (constant .LOGGER_NAME )
2428
2529
2630def write_excel_and_csv (filename_without_extension , sheet_list , ignore_os = False ):
@@ -30,22 +34,53 @@ def write_excel_and_csv(filename_without_extension, sheet_list, ignore_os=False)
3034 success_csv = True
3135 error_msg_csv = ""
3236
33- output_dir = os .path .dirname (filename_without_extension )
34- Path (output_dir ).mkdir (parents = True , exist_ok = True )
37+ is_not_null , sheet_list = remove_empty_sheet (sheet_list )
3538
36- success , error_msg = write_result_to_excel (filename_without_extension +
39+ if is_not_null :
40+ output_dir = os .path .dirname (filename_without_extension )
41+ Path (output_dir ).mkdir (parents = True , exist_ok = True )
42+
43+ success , error_msg = write_result_to_excel (filename_without_extension +
3744 ".xlsx" , sheet_list )
38- if ignore_os or platform .system () != "Windows" :
39- success_csv , error_msg_csv = write_result_to_csv (filename_without_extension +
45+ if ignore_os or platform .system () != "Windows" :
46+ success_csv , error_msg_csv = write_result_to_csv (filename_without_extension +
4047 ".csv" , sheet_list )
41- if not success :
42- error_msg = "[Error] Writing excel:" + error_msg
43- if not success_csv :
44- error_msg += "\n [Error] Writing csv:" + error_msg_csv
48+ if not success :
49+ error_msg = "[Error] Writing excel:" + error_msg
50+ if not success_csv :
51+ error_msg += "\n [Error] Writing csv:" + error_msg_csv
52+ else :
53+ success = False
54+ error_msg = _EMPTY_ITEM_MSG
4555
4656 return (success and success_csv ), error_msg
4757
4858
59+ def remove_empty_sheet (sheet_items ):
60+ skip_sheet_name = []
61+ cnt_sheet_to_print = 0
62+ final_sheet_to_print = {}
63+ success = False
64+
65+ try :
66+ if sheet_items :
67+ for sheet_name , sheet_content in sheet_items .items ():
68+ logger .debug ("ITEM COUNT:" + str (len (sheet_content )))
69+ if len (sheet_content ) > 0 :
70+ final_sheet_to_print [sheet_name ] = sheet_content
71+ cnt_sheet_to_print += 1
72+ else :
73+ skip_sheet_name .append (sheet_name )
74+ if cnt_sheet_to_print != 0 :
75+ success = True
76+ if len (skip_sheet_name ) > 0 :
77+ logger .warn ("* Empty sheet(not printed):" + str (skip_sheet_name ))
78+ except Exception as ex :
79+ logger .warn ("* Warning:" + str (ex ))
80+
81+ return success , final_sheet_to_print
82+
83+
4984def write_result_to_csv (output_file , sheet_list ):
5085 success = True
5186 error_msg = ""
0 commit comments