1414from fosslight_util .set_log import init_log
1515import fosslight_util .constant as constant
1616from fosslight_util .output_format import check_output_formats , write_output_file
17- from ._binary_dao import get_oss_info_from_db
1817from ._binary import BinaryItem , TLSH_CHECKSUM_NULL
18+ from ._binary_dao import get_oss_info_from_db
1919from ._jar_analysis import analyze_jar_file , merge_binary_list
20+ from ._simple_mode import print_simple_mode , filter_binary , init_simple
2021from fosslight_util .correct import correct_with_yaml
2122from fosslight_util .oss_item import ScannerItem
2223import hashlib
4041_REMOVE_DIR = [os .path .sep + dir_name + os .path .sep for dir_name in _REMOVE_DIR ]
4142_error_logs = []
4243_root_path = ""
43- _start_time = ""
44+ start_time = ""
4445windows = False
4546BYTES = 2048
4647BIN_EXT_HEADER = {'BIN_FL_Binary' : ['ID' , 'Binary Path' , 'OSS Name' ,
@@ -71,18 +72,14 @@ def get_checksum_and_tlsh(bin_with_path):
7172
7273
7374def init (path_to_find_bin , output_file_name , formats , path_to_exclude = []):
74- global _root_path , logger , _start_time
75+ global logger
7576
7677 _json_ext = ".json"
77- _start_time = datetime .now ().strftime ('%y%m%d_%H%M' )
7878 _result_log = {
79- "Tool Info" : PKG_NAME
79+ "Tool Info" : PKG_NAME ,
80+ "Mode" : "Normal Mode"
8081 }
8182
82- _root_path = path_to_find_bin
83- if not path_to_find_bin .endswith (os .path .sep ):
84- _root_path += os .path .sep
85-
8683 success , msg , output_path , output_files , output_extensions = check_output_formats (output_file_name , formats )
8784
8885 if success :
@@ -96,16 +93,16 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
9693 for i , output_extension in enumerate (output_extensions ):
9794 if output_files [i ] is None or output_files [i ] == "" :
9895 if output_extension == _json_ext :
99- output_files [i ] = f"fosslight_opossum_bin_{ _start_time } "
96+ output_files [i ] = f"fosslight_opossum_bin_{ start_time } "
10097 else :
101- output_files [i ] = f"fosslight_report_bin_{ _start_time } "
98+ output_files [i ] = f"fosslight_report_bin_{ start_time } "
10299
103100 combined_paths_and_files = [os .path .join (output_path , file ) for file in output_files ]
104101 else :
105102 logger .error (f"Format error - { msg } " )
106103 sys .exit (1 )
107104
108- log_file = os .path .join (output_path , f"fosslight_log_bin_{ _start_time } .txt" )
105+ log_file = os .path .join (output_path , f"fosslight_log_bin_{ start_time } .txt" )
109106 logger , _result_log = init_log (log_file , True , logging .INFO , logging .DEBUG ,
110107 PKG_NAME , path_to_find_bin , path_to_exclude )
111108
@@ -161,9 +158,19 @@ def get_file_list(path_to_find, abs_path_to_exclude):
161158
162159def find_binaries (path_to_find_bin , output_dir , formats , dburl = "" , simple_mode = False ,
163160 correct_mode = True , correct_filepath = "" , path_to_exclude = []):
161+ global start_time , _root_path
164162
165- _result_log , result_reports , output_extensions = init (
166- path_to_find_bin , output_dir , formats , path_to_exclude )
163+ start_time = datetime .now ().strftime ('%y%m%d_%H%M' )
164+
165+ _root_path = path_to_find_bin
166+ if not path_to_find_bin .endswith (os .path .sep ):
167+ _root_path += os .path .sep
168+
169+ if simple_mode :
170+ _result_log , compressed_list_txt , simple_bin_list_txt = init_simple (output_dir , PKG_NAME , start_time )
171+ else :
172+ _result_log , result_reports , output_extensions = init (
173+ path_to_find_bin , output_dir , formats , path_to_exclude )
167174
168175 total_bin_cnt = 0
169176 total_file_cnt = 0
@@ -191,9 +198,15 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
191198 exit = True )
192199 total_bin_cnt = len (return_list )
193200 if simple_mode :
194- bin_list = [bin .bin_name_with_path for bin in return_list ]
201+ try :
202+ compressed_list , bin_list = filter_binary (return_list )
203+ success = print_simple_mode (compressed_list_txt , simple_bin_list_txt , compressed_list , bin_list )
204+ except Exception as ex :
205+ error_occured (error_msg = f"Failed to run simple mode: { ex } " ,
206+ result_log = _result_log ,
207+ exit = True )
195208 else :
196- scan_item = ScannerItem (PKG_NAME , _start_time )
209+ scan_item = ScannerItem (PKG_NAME , start_time )
197210 scan_item .set_cover_pathinfo (path_to_find_bin , path_to_exclude )
198211 try :
199212 # Run OWASP Dependency-check
@@ -307,7 +320,7 @@ def print_result_log(success=True, result_log={}, file_cnt="", bin_file_cnt="",
307320 if "Running time" in result_log :
308321 start_time = result_log ["Running time" ]
309322 else :
310- start_time = _start_time
323+ start_time = start_time
311324 result_log ["Running time" ] = start_time + " ~ " + \
312325 datetime .now ().strftime ('%Y%m%d_%H%M%S' )
313326 result_log ["Execution result" ] = 'Success' if success else 'Error occurred'
0 commit comments