1818from fosslight_util .timer_thread import TimerThread
1919from ._parsing_scancode_file_item import parsing_file_item
2020from ._parsing_scancode_file_item import get_error_from_header
21- from fosslight_util .write_excel import write_excel_and_csv
2221from ._help import print_help_msg_source
2322from ._license_matched import get_license_list_to_print
23+ from fosslight_util .output_format import check_output_format , write_output_file
2424
2525logger = logging .getLogger (constant .LOGGER_NAME )
2626warnings .filterwarnings ("ignore" , category = FutureWarning )
@@ -33,9 +33,10 @@ def main():
3333 write_json_file = False
3434 output_file = ""
3535 print_matched_text = False
36+ format = ""
3637
3738 try :
38- opts , args = getopt .getopt (argv , 'hmjp:o:' )
39+ opts , args = getopt .getopt (argv , 'hmjp:o:f: ' )
3940 for opt , arg in opts :
4041 if opt == "-h" :
4142 print_help_msg_source ()
@@ -47,108 +48,124 @@ def main():
4748 output_file = arg
4849 elif opt == "-m" :
4950 print_matched_text = True
51+ elif opt == "-f" :
52+ format = arg
5053 except Exception :
5154 print_help_msg_source ()
5255
5356 timer = TimerThread ()
5457 timer .setDaemon (True )
5558 timer .start ()
56- run_scan (path_to_scan , output_file , write_json_file , - 1 , False , print_matched_text )
59+
60+ run_scan (path_to_scan , output_file , write_json_file , - 1 , False , print_matched_text , format )
5761
5862
5963def run_scan (path_to_scan , output_file_name = "" ,
60- _write_json_file = False , num_cores = - 1 , return_results = False , need_license = False ):
64+ _write_json_file = False , num_cores = - 1 , return_results = False , need_license = False , format = "" ):
6165 global logger
6266
6367 success = True
6468 msg = ""
6569 _str_final_result_log = ""
6670 _result_log = {}
6771 result_list = []
72+ _json_ext = ".json"
6873
6974 _windows = platform .system () == "Windows"
7075 start_time = datetime .now ().strftime ('%Y%m%d_%H%M%S' )
7176
72- if output_file_name == "" :
73- output_file = "FOSSLight-Report_" + start_time
74- output_json_file = "scancode_" + start_time
75- output_dir = os .getcwd ()
76- else :
77- output_file = output_file_name
78- output_json_file = output_file_name
79- output_dir = os .path .dirname (os .path .abspath (output_file_name ))
80-
81- logger , _result_log = init_log (os .path .join (output_dir , "fosslight_src_log_" + start_time + ".txt" ),
82- True , logging .INFO , logging .DEBUG , _PKG_NAME , path_to_scan )
83-
84- if path_to_scan == "" :
85- if _windows :
86- path_to_scan = os .getcwd ()
77+ success , msg , output_path , output_file , output_extension = check_output_format (output_file_name , format )
78+ if success :
79+ if output_path == "" :
80+ output_path = os .getcwd ()
8781 else :
88- print_help_msg_source ( )
82+ output_path = os . path . abspath ( output_path )
8983
90- num_cores = multiprocessing .cpu_count () - 1 if num_cores < 0 else num_cores
84+ if output_file == "" :
85+ if output_extension == _json_ext :
86+ output_file = "Opossum_input_" + start_time
87+ else :
88+ output_file = "FOSSLight-Report_" + start_time
9189
92- if os . path . isdir ( path_to_scan ) :
93- try :
94- output_json_file = output_json_file + ".json" if _write_json_file \
95- else ""
90+ if _write_json_file :
91+ output_json_file = os . path . join ( output_path , "scancode_raw_result.json" )
92+ else :
93+ output_json_file = ""
9694
97- rc , results = cli .run_scan (path_to_scan , max_depth = 100 ,
98- strip_root = True , license = True ,
99- copyright = True , return_results = True ,
100- processes = num_cores ,
101- output_json_pp = output_json_file ,
102- only_findings = True , license_text = True )
95+ logger , _result_log = init_log (os .path .join (output_path , "fosslight_src_log_" + start_time + ".txt" ),
96+ True , logging .INFO , logging .DEBUG , _PKG_NAME , path_to_scan )
10397
104- if not rc :
105- msg = "Source code analysis failed."
106- success = False
98+ if path_to_scan == "" :
99+ if _windows :
100+ path_to_scan = os .getcwd ()
101+ else :
102+ print_help_msg_source ()
107103
108- if results :
109- sheet_list = {}
110- has_error = False
111- if "headers" in results :
112- has_error , error_msg = get_error_from_header (results ["headers" ])
113- if has_error :
114- _result_log ["Error_files" ] = error_msg
115- msg = "Failed to analyze :" + error_msg
116- if "files" in results :
117- rc , result_list , parsing_msg , license_list = parsing_file_item (results ["files" ], has_error , need_license )
118- _result_log ["Parsing Log" ] = parsing_msg
119- if rc :
120- if not success :
121- success = True
122- result_list = sorted (
123- result_list , key = lambda row : ('' .join (row .licenses )))
124- sheet_list ["SRC" ] = [scan_item .get_row_to_print () for scan_item in result_list ]
125- if need_license :
126- sheet_list ["matched_text" ] = get_license_list_to_print (license_list )
127-
128- success_to_write , writing_msg = write_excel_and_csv (
129- output_file , sheet_list )
130- logger .info ("Writing excel :" + str (success_to_write ) + " " + writing_msg )
131- if success_to_write :
132- _result_log ["FOSSLight Report" ] = output_file + ".xlsx"
133- except Exception as ex :
104+ num_cores = multiprocessing .cpu_count () - 1 if num_cores < 0 else num_cores
105+
106+ if os .path .isdir (path_to_scan ):
107+ try :
108+ rc , results = cli .run_scan (path_to_scan , max_depth = 100 ,
109+ strip_root = True , license = True ,
110+ copyright = True , return_results = True ,
111+ processes = num_cores ,
112+ output_json_pp = output_json_file ,
113+ only_findings = True , license_text = True )
114+
115+ if not rc :
116+ msg = "Source code analysis failed."
117+ success = False
118+
119+ if results :
120+ sheet_list = {}
121+ has_error = False
122+ if "headers" in results :
123+ has_error , error_msg = get_error_from_header (results ["headers" ])
124+ if has_error :
125+ _result_log ["Error_files" ] = error_msg
126+ msg = "Failed to analyze :" + error_msg
127+ if "files" in results :
128+ rc , result_list , parsing_msg , license_list = parsing_file_item (results ["files" ], has_error , need_license )
129+ _result_log ["Parsing Log" ] = parsing_msg
130+ if rc :
131+ if not success :
132+ success = True
133+ result_list = sorted (
134+ result_list , key = lambda row : ('' .join (row .licenses )))
135+ sheet_list ["SRC_FL_Source" ] = [scan_item .get_row_to_print () for scan_item in result_list ]
136+ if need_license :
137+ sheet_list ["matched_text" ] = get_license_list_to_print (license_list )
138+
139+ output_file_without_ext = os .path .join (output_path , output_file )
140+ success_to_write , writing_msg = write_output_file (output_file_without_ext , output_extension ,
141+ sheet_list )
142+ logger .info ("Writing Output file(" + output_file + output_extension + "):" + str (success_to_write )
143+ + " " + writing_msg )
144+ if success_to_write :
145+ _result_log ["Output file" ] = output_file_without_ext + output_extension
146+
147+ except Exception as ex :
148+ success = False
149+ msg = str (ex )
150+ logger .error ("Analyze " + path_to_scan + ":" + msg )
151+ else :
134152 success = False
135- msg = str (ex )
136- logger .error ("Analyze " + path_to_scan + ":" + msg )
137- else :
138- success = False
139- msg = "Check the path to scan. :" + path_to_scan
153+ msg = "Check the path to scan. :" + path_to_scan
140154
141- if not return_results :
142- result_list = []
155+ if not return_results :
156+ result_list = []
143157
144- scan_result_msg = str (success ) if msg == "" else str (success ) + "," + msg
158+ scan_result_msg = str (success ) if msg == "" else str (success ) + ", " + msg
145159 _result_log ["Scan Result" ] = scan_result_msg
146- _result_log ["Output Directory" ] = output_dir
160+ _result_log ["Output Directory" ] = output_path
147161 try :
148162 _str_final_result_log = yaml .safe_dump (_result_log , allow_unicode = True , sort_keys = True )
149163 logger .info (_str_final_result_log )
150164 except Exception as ex :
151165 logger .warning ("Failed to print result log. " + str (ex ))
166+
167+ if not success :
168+ logger .error ("Failed to run:" + str (scan_result_msg ))
152169 return success , _result_log ["Scan Result" ], result_list
153170
154171
0 commit comments