1818from ._binary_dao import get_oss_info_from_db
1919from ._binary import BinaryItem , TLSH_CHECKSUM_NULL
2020from ._jar_analysis import analyze_jar_file , merge_binary_list
21+ from ._simple_mode import print_simple_mode , filter_binary , init_simple
2122from fosslight_util .correct import correct_with_yaml
2223from fosslight_util .oss_item import ScannerItem
2324import hashlib
4142_REMOVE_DIR = [os .path .sep + dir_name + os .path .sep for dir_name in _REMOVE_DIR ]
4243_error_logs = []
4344_root_path = ""
44- _start_time = ""
45+ start_time = ""
4546windows = False
4647BYTES = 2048
4748BIN_EXT_HEADER = {'BIN_FL_Binary' : ['ID' , 'Binary Path' , 'OSS Name' ,
@@ -72,18 +73,9 @@ def get_checksum_and_tlsh(bin_with_path):
7273
7374
7475def init (path_to_find_bin , output_file_name , formats , path_to_exclude = []):
75- global _root_path , logger , _start_time
76+ global logger , _result_log
7677
7778 _json_ext = ".json"
78- _start_time = datetime .now ().strftime ('%y%m%d_%H%M' )
79- _result_log = {
80- "Tool Info" : PKG_NAME
81- }
82-
83- _root_path = path_to_find_bin
84- if not path_to_find_bin .endswith (os .path .sep ):
85- _root_path += os .path .sep
86-
8779 success , msg , output_path , output_files , output_extensions , formats = check_output_formats_v2 (output_file_name , formats )
8880
8981 if success :
@@ -100,20 +92,20 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
10092 if formats :
10193 if formats [i ].startswith ('spdx' ):
10294 if platform .system () != 'Windows' :
103- output_files [i ] = f"fosslight_spdx_bin_{ _start_time } "
95+ output_files [i ] = f"fosslight_spdx_bin_{ start_time } "
10496 else :
10597 logger .warning ('spdx format is not supported on Windows. Please remove spdx from format.' )
10698 to_remove .append (i )
10799 else :
108100 if output_extension == _json_ext :
109- output_files [i ] = f"fosslight_opossum_bin_{ _start_time } "
101+ output_files [i ] = f"fosslight_opossum_bin_{ start_time } "
110102 else :
111- output_files [i ] = f"fosslight_report_bin_{ _start_time } "
103+ output_files [i ] = f"fosslight_report_bin_{ start_time } "
112104 else :
113105 if output_extension == _json_ext :
114- output_files [i ] = f"fosslight_opossum_bin_{ _start_time } "
106+ output_files [i ] = f"fosslight_opossum_bin_{ start_time } "
115107 else :
116- output_files [i ] = f"fosslight_report_bin_{ _start_time } "
108+ output_files [i ] = f"fosslight_report_bin_{ start_time } "
117109 for index in sorted (to_remove , reverse = True ):
118110 # remove elements of spdx format on windows
119111 del output_files [index ]
@@ -127,7 +119,7 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
127119 logger .error (f"Format error - { msg } " )
128120 sys .exit (1 )
129121
130- log_file = os .path .join (output_path , f"fosslight_log_bin_{ _start_time } .txt" )
122+ log_file = os .path .join (output_path , f"fosslight_log_bin_{ start_time } .txt" )
131123 logger , _result_log = init_log (log_file , True , logging .INFO , logging .DEBUG ,
132124 PKG_NAME , path_to_find_bin , path_to_exclude )
133125
@@ -183,9 +175,21 @@ def get_file_list(path_to_find, abs_path_to_exclude):
183175
184176def find_binaries (path_to_find_bin , output_dir , formats , dburl = "" , simple_mode = False ,
185177 correct_mode = True , correct_filepath = "" , path_to_exclude = []):
178+ global start_time , _root_path , _result_log
179+
180+ mode = "Normal Mode"
181+ start_time = datetime .now ().strftime ('%y%m%d_%H%M' )
182+
183+ _root_path = path_to_find_bin
184+ if not path_to_find_bin .endswith (os .path .sep ):
185+ _root_path += os .path .sep
186186
187- _result_log , result_reports , output_extensions = init (
188- path_to_find_bin , output_dir , formats , path_to_exclude )
187+ if simple_mode :
188+ mode = "Simple Mode"
189+ _result_log , compressed_list_txt , simple_bin_list_txt = init_simple (output_dir , PKG_NAME , start_time )
190+ else :
191+ _result_log , result_reports , output_extensions = init (
192+ path_to_find_bin , output_dir , formats , path_to_exclude )
189193
190194 total_bin_cnt = 0
191195 total_file_cnt = 0
@@ -201,7 +205,8 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
201205 if not os .path .isdir (path_to_find_bin ):
202206 error_occured (error_msg = f"Can't find the directory : { path_to_find_bin } " ,
203207 result_log = _result_log ,
204- exit = True )
208+ exit = True ,
209+ mode = mode )
205210 if not correct_filepath :
206211 correct_filepath = path_to_find_bin
207212 try :
@@ -210,12 +215,20 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
210215 except Exception as ex :
211216 error_occured (error_msg = f"Failed to check whether it is binary or not : { ex } " ,
212217 result_log = _result_log ,
213- exit = True )
218+ exit = True ,
219+ mode = mode )
214220 total_bin_cnt = len (return_list )
215221 if simple_mode :
216- bin_list = [bin .bin_name_with_path for bin in return_list ]
222+ try :
223+ compressed_list , bin_list = filter_binary (return_list )
224+ results = print_simple_mode (compressed_list_txt , simple_bin_list_txt , compressed_list , bin_list )
225+ except Exception as ex :
226+ error_occured (error_msg = f"Failed to run simple mode: { ex } " ,
227+ result_log = _result_log ,
228+ exit = True ,
229+ mode = "Simple mode" )
217230 else :
218- scan_item = ScannerItem (PKG_NAME , _start_time )
231+ scan_item = ScannerItem (PKG_NAME , start_time )
219232 scan_item .set_cover_pathinfo (path_to_find_bin , path_to_exclude )
220233 try :
221234 # Run OWASP Dependency-check
@@ -262,12 +275,12 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
262275 logger .error (f"Fail to generate result file.:{ writing_msg } " )
263276
264277 try :
265- print_result_log (success = True , result_log = _result_log ,
278+ print_result_log (mode = mode , success = True , result_log = _result_log ,
266279 file_cnt = str (total_file_cnt ),
267280 bin_file_cnt = str (total_bin_cnt ),
268281 auto_bin_cnt = str (db_loaded_cnt ), bin_list = bin_list )
269282 except Exception as ex :
270- error_occured (error_msg = f"Print log : { ex } " , exit = False )
283+ error_occured (mode , error_msg = f"Print log : { ex } " , exit = False )
271284
272285 return success_to_write , scan_item
273286
@@ -279,7 +292,7 @@ def return_bin_only(file_list, need_checksum_tlsh=True):
279292 if need_checksum_tlsh :
280293 file_item .checksum , file_item .tlsh , error_msg = get_checksum_and_tlsh (file_item .bin_name_with_path )
281294 if error_msg :
282- error_occured (error_msg = error_msg , exit = False )
295+ error_occured (modeerror_msg = error_msg , exit = False )
283296 yield file_item
284297 except Exception as ex :
285298 logger .debug (f"Exception in get_file_list: { ex } " )
@@ -317,21 +330,22 @@ def check_binary(file_with_path):
317330 return is_bin_confirmed
318331
319332
320- def error_occured (error_msg , exit = False , result_log = {}):
333+ def error_occured (error_msg , exit = False , result_log = {}, mode = "Normal mode" ):
321334 global _error_logs
322335 _error_logs .append (error_msg )
323336 if exit :
324- print_result_log (success = False , result_log = result_log )
337+ print_result_log (mode , success = False , result_log = result_log )
325338 sys .exit ()
326339
327340
328- def print_result_log (success = True , result_log = {}, file_cnt = "" , bin_file_cnt = "" , auto_bin_cnt = "" , bin_list = []):
341+ def print_result_log (mode = "Normal Mode" , success = True , result_log = {}, file_cnt = "" , bin_file_cnt = "" , auto_bin_cnt = "" , bin_list = []):
329342
330343 if "Running time" in result_log :
331- start_time = result_log ["Running time" ]
344+ starttime = result_log ["Running time" ]
332345 else :
333- start_time = _start_time
334- result_log ["Running time" ] = start_time + " ~ " + \
346+ starttime = start_time
347+ result_log ["Mode" ] = mode
348+ result_log ["Running time" ] = starttime + " ~ " + \
335349 datetime .now ().strftime ('%Y%m%d_%H%M%S' )
336350 result_log ["Execution result" ] = 'Success' if success else 'Error occurred'
337351 result_log ["Binaries / Scanned files" ] = f"{ bin_file_cnt } /{ file_cnt } "
0 commit comments