1616from fosslight_util .set_log import init_log
1717import fosslight_util .constant as constant
1818from fosslight_util .write_txt import write_txt_file
19- from fosslight_util .write_excel import write_excel_and_csv
19+ from fosslight_util .output_format import check_output_format , write_output_file
2020from ._binary_dao import get_oss_info_from_db
2121from ._binary import BinaryItem
2222from ._help import print_help_msg
3838_root_path = ""
3939
4040
41- def init (path_to_find_bin , output_dir , output_file_name ):
41+ def init (path_to_find_bin , output_file_name , format ):
4242 global _root_path , logger
43+
44+ _json_ext = ".json"
45+ _start_time = datetime .now ().strftime ('%Y%m%d_%H%M%S' )
4346 _result_log = {
4447 "Tool Info" : _PKG_NAME
4548 }
49+
4650 _root_path = path_to_find_bin
4751 if not path_to_find_bin .endswith (os .path .sep ):
4852 _root_path += os .path .sep
49- output_dir = os .path .abspath (output_dir )
50- _start_time = datetime .now ().strftime ('%Y%m%d_%H%M%S' )
5153
52- if output_file_name != "" :
53- result_report = output_file_name
54- log_file = output_file_name + "_log.txt"
55- bin_txt_file = output_file_name + ".txt"
56- else :
57- result_report = "FOSSLight-Report_" + _start_time
58- log_file = "fosslight_bin_log_" + _start_time + ".txt"
59- bin_txt_file = "binary_" + _start_time + ".txt"
54+ success , msg , output_path , output_file , output_extension = check_output_format (output_file_name , format )
55+ if success :
56+ if output_path == "" :
57+ output_path = os .getcwd ()
58+ else :
59+ output_path = os .path .abspath (output_path )
6060
61- result_report = os .path .join (output_dir , result_report )
62- binary_txt_file = os .path .join (output_dir , bin_txt_file )
63- log_file = os .path .join (output_dir , log_file )
61+ if output_file != "" :
62+ result_report = output_file
63+ bin_txt_file = output_file + ".txt"
64+ else :
65+ if output_extension == _json_ext :
66+ result_report = "Opossum_input_" + _start_time
67+ else :
68+ result_report = "FOSSLight-Report_" + _start_time
69+ bin_txt_file = "binary_" + _start_time + ".txt"
70+
71+ result_report = os .path .join (output_path , result_report )
72+ binary_txt_file = os .path .join (output_path , bin_txt_file )
73+ else :
74+ output_path = os .getcwd ()
6475
76+ log_file = os .path .join (output_path , "fosslight_bin_log_" + _start_time + ".txt" )
6577 logger , _result_log = init_log (log_file , True , logging .INFO , logging .DEBUG , _PKG_NAME , path_to_find_bin )
6678
67- return _result_log , result_report , binary_txt_file
79+ if not success :
80+ error_occured (error_msg = msg ,
81+ result_log = _result_log ,
82+ exit = True )
83+ return _result_log , result_report , binary_txt_file , output_extension
6884
6985
7086def get_file_list (path_to_find ):
@@ -96,10 +112,10 @@ def get_file_list(path_to_find):
96112 return file_cnt , bin_list
97113
98114
99- def find_binaries (path_to_find_bin , output_dir , output_file_name , _include_file_command , dburl = "" ):
115+ def find_binaries (path_to_find_bin , output_dir , format , _include_file_command , dburl = "" ):
100116
101- _result_log , result_report , binary_txt_file = init (
102- path_to_find_bin , output_dir , output_file_name )
117+ _result_log , result_report , binary_txt_file , output_extension = init (
118+ path_to_find_bin , output_dir , format )
103119
104120 total_bin_cnt = 0
105121 total_file_cnt = 0
@@ -130,12 +146,14 @@ def find_binaries(path_to_find_bin, output_dir, output_file_name, _include_file_
130146 content_list = []
131147 for scan_item in return_list :
132148 content_list .extend (scan_item .get_print_oss_report ())
133- sheet_list ["BIN " ] = content_list
149+ sheet_list ["BIN_FL_Binary " ] = content_list
134150
135- success_to_write , writing_msg = write_excel_and_csv (result_report , sheet_list )
136- logger .info ("Writing excel :" + str (success_to_write ) + " " + writing_msg )
151+ success_to_write , writing_msg = write_output_file (result_report , output_extension ,
152+ sheet_list )
153+ logger .info ("Writing Output file(" + os .path .basename (result_report ) + output_extension
154+ + "):" + str (success_to_write ) + " " + writing_msg )
137155 if success_to_write :
138- _result_log ["FOSSLight Report " ] = result_report + ".xlsx"
156+ _result_log ["Output file " ] = result_report + output_extension
139157
140158 except Exception as ex :
141159 error_occured (error_msg = str (ex ), exit = False )
@@ -213,9 +231,9 @@ def print_result_log(success=True, result_log={}, file_cnt="", bin_file_cnt="",
213231def main ():
214232
215233 argv = sys .argv [1 :]
216- output_dir = os . getcwd ()
234+ output_dir = ""
217235 path_to_find_bin = ""
218- output_file_name = ""
236+ format = ""
219237 _include_file_command = ""
220238 db_url = ""
221239
@@ -230,7 +248,7 @@ def main():
230248 elif opt == "-o" :
231249 output_dir = arg
232250 elif opt == "-f" :
233- output_file_name = arg
251+ format = arg
234252 elif opt == "-d" :
235253 db_url = arg
236254
@@ -241,8 +259,8 @@ def main():
241259 else :
242260 print_help_msg ()
243261
244- find_binaries (path_to_find_bin , output_dir ,
245- output_file_name , _include_file_command , db_url )
262+ find_binaries (path_to_find_bin , output_dir , format ,
263+ _include_file_command , db_url )
246264
247265
248266if __name__ == '__main__' :
0 commit comments