99import re
1010import yaml
1111import sys
12+ import shutil
1213from pathlib import Path
13- from shutil import rmtree as rmdir
1414from datetime import datetime
1515from fosslight_binary import binary_analysis
1616from fosslight_dependency .run_dependency_scanner import run_dependency_scanner
2323from fosslight_prechecker ._precheck import run_lint as prechecker_lint
2424from .common import (copy_file , call_analysis_api ,
2525 overwrite_excel , extract_name_from_link ,
26- merge_yamls )
26+ merge_yamls , correct_scanner_result )
2727from fosslight_util .write_excel import merge_excels
2828from ._run_compare import run_compare
2929import subprocess
@@ -102,7 +102,8 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
102102 run_src = True , run_bin = True , run_dep = True , run_prechecker = True ,
103103 remove_src_data = True , result_log = {}, output_file = "" ,
104104 output_extension = "" , num_cores = - 1 , db_url = "" ,
105- default_oss_name = "" , url = "" ):
105+ default_oss_name = "" , url = "" ,
106+ correct_mode = True , correct_fpath = "" ):
106107 final_excel_dir = output_path
107108 success = True
108109 temp_output_fiiles = []
@@ -115,6 +116,9 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
115116 if output_extension == "" :
116117 output_extension = ".xlsx"
117118
119+ if not correct_fpath :
120+ correct_fpath = src_path
121+
118122 try :
119123 sheet_list = {}
120124 final_excel_dir = os .path .abspath (final_excel_dir )
@@ -149,7 +153,8 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
149153 sheet_list ["SRC_FL_Source" ] = [scan_item .get_row_to_print () for scan_item in result [2 ]]
150154 need_license = True if output_extension == ".xlsx" else False
151155 create_report_file (0 , result [2 ], result [3 ], 'all' , need_license ,
152- _output_dir , output_files ["SRC" ].split ('.' )[0 ], output_extension )
156+ _output_dir , output_files ["SRC" ].split ('.' )[0 ], output_extension ,
157+ correct_mode , correct_fpath , abs_path )
153158 else : # Run fosslight_source by using docker image
154159 src_output = os .path .join ("output" , output_files ["SRC" ])
155160 output_rel_path = os .path .relpath (abs_path , os .getcwd ())
@@ -166,7 +171,8 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
166171 1 , binary_analysis .find_binaries ,
167172 abs_path ,
168173 os .path .join (_output_dir , output_files ["BIN" ]),
169- "" , db_url )
174+ "" , db_url , False ,
175+ correct_mode , correct_fpath )
170176 if success :
171177 output_binary_txt_raw = f"{ output_files ['BIN' ].split ('.' )[0 ]} .txt"
172178 success_file , copied_file = copy_file (os .path .join (_output_dir , output_binary_txt_raw ),
@@ -186,6 +192,19 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
186192 try :
187193 output_file_without_ext = os .path .join (final_excel_dir , output_file )
188194 final_report = f"{ output_file_without_ext } { output_extension } "
195+ tmp_dir = f"tmp_{ datetime .now ().strftime ('%y%m%d_%H%M' )} "
196+ exist_src = False
197+ exist_bin = False
198+ if correct_mode :
199+ os .makedirs (os .path .join (_output_dir , tmp_dir ), exist_ok = True )
200+ if os .path .exists (os .path .join (_output_dir , output_files ['SRC' ])):
201+ exist_src = True
202+ shutil .copy2 (os .path .join (_output_dir , output_files ['SRC' ]), os .path .join (_output_dir , tmp_dir ))
203+ if os .path .exists (os .path .join (_output_dir , output_files ['BIN' ])):
204+ exist_bin = True
205+ shutil .copy2 (os .path .join (_output_dir , output_files ['BIN' ]), os .path .join (_output_dir , tmp_dir ))
206+ if exist_src or exist_bin :
207+ correct_scanner_result (_output_dir , output_files , exist_src , exist_bin )
189208 if output_extension == ".xlsx" :
190209 if remove_src_data :
191210 overwrite_excel (_output_dir , default_oss_name , "OSS Name" )
@@ -195,7 +214,14 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
195214 merge_yaml_files = [output_files ["SRC" ], output_files ["BIN" ], output_files ["DEP" ]]
196215 success , err_msg = merge_yamls (_output_dir , merge_yaml_files , final_report ,
197216 remove_src_data , default_oss_name , url )
198-
217+ if correct_mode :
218+ if exist_src :
219+ shutil .move (os .path .join (_output_dir , tmp_dir , output_files ['SRC' ]),
220+ os .path .join (_output_dir , output_files ['SRC' ]))
221+ if exist_bin :
222+ shutil .move (os .path .join (_output_dir , tmp_dir , output_files ['BIN' ]),
223+ os .path .join (_output_dir , output_files ['BIN' ]))
224+ shutil .rmtree (os .path .join (_output_dir , tmp_dir ), ignore_errors = True )
199225 if success :
200226 if os .path .isfile (final_report ):
201227 result_log ["Output File" ] = final_report
@@ -212,7 +238,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
212238 try :
213239 if remove_src_data :
214240 logger .debug (f"Remove temporary source: { src_path } " )
215- rmdir (src_path )
241+ shutil . rmtree (src_path )
216242 except Exception as ex :
217243 logger .debug (f"Error to remove temp files:{ ex } " )
218244
@@ -267,7 +293,7 @@ def init(output_path="", make_outdir=True):
267293
268294
269295def run_main (mode , path_arg , dep_arguments , output_file_or_dir , file_format , url_to_analyze , db_url ,
270- hide_progressbar = False , keep_raw_data = False , num_cores = - 1 ):
296+ hide_progressbar = False , keep_raw_data = False , num_cores = - 1 , correct_mode = True , correct_fpath = "" ):
271297 global _executed_path , _start_time
272298
273299 output_file = ""
@@ -348,16 +374,20 @@ def run_main(mode, path_arg, dep_arguments, output_file_or_dir, file_format, url
348374 default_oss_name = extract_name_from_link (url_to_analyze )
349375 success , src_path = download_source (url_to_analyze , output_path )
350376
377+ if not correct_fpath :
378+ correct_fpath = src_path
379+
351380 if src_path != "" :
352381 run_scanner (src_path , dep_arguments , output_path , keep_raw_data ,
353382 run_src , run_bin , run_dep , run_prechecker ,
354383 remove_downloaded_source , {}, output_file ,
355384 output_extension , num_cores , db_url ,
356- default_oss_name , url_to_analyze )
385+ default_oss_name , url_to_analyze ,
386+ correct_mode , correct_fpath )
357387 try :
358388 if not keep_raw_data :
359389 logger .debug (f"Remove temporary files: { _output_dir } " )
360- rmdir (_output_dir )
390+ shutil . rmtree (_output_dir )
361391 except Exception as ex :
362392 logger .debug (f"Error to remove temp files:{ ex } " )
363393 except Exception as ex :
0 commit comments