99import shutil
1010import pandas as pd
1111import yaml
12-
13- import fosslight_util .constant as constant
12+ from fosslight_util .constant import LOGGER_NAME , FOSSLIGHT_SOURCE , FOSSLIGHT_BINARY
1413from fosslight_util .parsing_yaml import parsing_yml
15- from fosslight_util .write_yaml import create_yaml_with_ossitem
1614from fosslight_util .write_scancodejson import write_scancodejson
1715from fosslight_util .read_excel import read_oss_report
18- from fosslight_util .output_format import write_output_file
1916from fosslight_util .oss_item import OssItem
2017
21- logger = logging .getLogger (constant . LOGGER_NAME )
18+ logger = logging .getLogger (LOGGER_NAME )
2219SRC_SHEET = 'SRC_FL_Source'
2320BIN_SHEET = 'BIN_FL_Binary'
2421BIN_EXT_HEADER = {
@@ -93,65 +90,18 @@ def call_analysis_api(path_to_run, str_run_start, return_idx, func, *args, **kwa
9390 return success , result or []
9491
9592
96- def overwrite_excel (excel_file_path , oss_name , column_name = 'OSS Name' ):
97- if oss_name :
98- try :
99- files = os .listdir (excel_file_path )
100- for file in files :
101- if file .endswith (".xlsx" ):
102- file_path = os .path .join (excel_file_path , file )
103- excel_file = pd .ExcelFile (file_path , engine = 'openpyxl' )
104-
105- for sheet_name in excel_file .sheet_names :
106- try :
107- df = pd .read_excel (file_path , sheet_name = sheet_name , engine = 'openpyxl' )
108- if column_name in df .columns :
109- updated = (df [column_name ] == '' ) | (df [column_name ].isnull ())
110- df .loc [updated , column_name ] = oss_name
111- df .to_excel (file_path , sheet_name = sheet_name , index = False )
112- except Exception as ex :
113- logger .debug (f"overwrite_sheet { sheet_name } :{ ex } " )
114- except Exception as ex :
115- logger .debug (f"overwrite_excel:{ ex } " )
116-
117-
118- def merge_yamls (_output_dir , merge_yaml_files , final_report , remove_src_data = False ,
119- default_oss_name = '' , default_oss_version = '' , url = '' ):
120- success = True
121- err_msg = ''
122-
123- oss_total_list = []
124- yaml_dict = {}
125- try :
126- for mf in merge_yaml_files :
127- if os .path .exists (os .path .join (_output_dir , mf )):
128- oss_list , _ , _ = parsing_yml (os .path .join (_output_dir , mf ), _output_dir )
129-
130- if remove_src_data :
131- existed_yaml = {}
132- for oi in oss_list :
133- oi .name = default_oss_name if oi .name == '' else oi .name
134- oi .version = default_oss_version if oi .version == '' else oi .version
135- oi .download_location = url if oi .download_location == '' else oi .download_location
136- create_yaml_with_ossitem (oi , existed_yaml )
137- with open (os .path .join (_output_dir , mf ), 'w' ) as f :
138- yaml .dump (existed_yaml , f , default_flow_style = False , sort_keys = False )
139-
140- oss_total_list .extend (oss_list )
141-
142- if oss_total_list != []:
143- for oti in oss_total_list :
144- create_yaml_with_ossitem (oti , yaml_dict )
145- with open (os .path .join (_output_dir , final_report ), 'w' ) as f :
146- yaml .dump (yaml_dict , f , default_flow_style = False , sort_keys = False )
147- else :
148- success = False
149- err_msg = "Output file is not created as no oss items detected."
150- except Exception as ex :
151- err_msg = ex
152- success = False
153-
154- return success , err_msg
93+ def update_oss_item (scan_item , oss_name , oss_version , download_loc ):
94+ for file_items in scan_item .file_items .values ():
95+ for file_item in file_items :
96+ if file_item .oss_items :
97+ for oi in file_item .oss_items :
98+ if oi .name == '' and oi .version == '' and oi .download_location == '' :
99+ oi .name = oss_name
100+ oi .version = oss_version
101+ oi .download_location = download_loc
102+ else :
103+ file_item .oss_items .append (OssItem (oss_name , oss_version , '' , download_loc ))
104+ return scan_item
155105
156106
157107def create_scancodejson (final_report , output_extension , ui_mode_report , src_path = "" ):
@@ -199,73 +149,48 @@ def create_scancodejson(final_report, output_extension, ui_mode_report, src_path
199149 return success , err_msg
200150
201151
202- def correct_scanner_result (_output_dir , output_files , output_extension , exist_src , exist_bin ):
203- src_oss_list = []
204- bin_oss_list = []
152+ def correct_scanner_result (all_scan_item ):
205153 duplicates = False
206154
207- if exist_src :
208- src_oss_list = check_exclude_dir (get_osslist (_output_dir , output_files ['SRC' ], output_extension , SRC_SHEET ))
209- if exist_bin :
210- bin_oss_list = check_exclude_dir (get_osslist (_output_dir , output_files ['BIN' ], output_extension , BIN_SHEET ))
211-
212- if exist_src and exist_bin :
155+ keys_needed = {FOSSLIGHT_SOURCE , FOSSLIGHT_BINARY }
156+ is_contained = keys_needed .issubset (all_scan_item .file_items .keys ())
157+ if is_contained :
158+ src_fileitems = all_scan_item .file_items [FOSSLIGHT_SOURCE ]
159+ bin_fileitems = all_scan_item .file_items [FOSSLIGHT_BINARY ]
213160 try :
214161 remove_src_idx_list = []
215- for idx_src , src_item in enumerate (src_oss_list ):
162+ for idx_src , src_fileitem in enumerate (src_fileitems ):
163+ src_fileitem .exclude = check_exclude_dir (src_fileitem .source_name_or_path )
216164 dup_flag = False
217- for bin_item in bin_oss_list :
218- if (not src_item .source_name_or_path ):
219- continue
220- if src_item .source_name_or_path [0 ] == bin_item .source_name_or_path [0 ]:
165+ for bin_fileitem in bin_fileitems :
166+ bin_fileitem .exclude = check_exclude_dir (bin_fileitem .source_name_or_path )
167+ if src_fileitem .source_name_or_path == bin_fileitem .source_name_or_path :
221168 dup_flag = True
222- if not bin_item .license and src_item .license :
223- src_item .exclude = bin_item .exclude
224- bin_item .set_sheet_item (src_item .get_print_array (constant .FL_BINARY )[0 ])
225- if bin_item .comment :
226- bin_item .comment += '/'
227- bin_item .comment += 'Loaded from SRC OSS info'
169+ src_all_licenses_non_empty = all (oss_item .license for oss_item in src_fileitem .oss_items )
170+ bin_empty_license_exists = all (not oss_item .license for oss_item in bin_fileitem .oss_items )
171+
172+ if src_all_licenses_non_empty and bin_empty_license_exists :
173+ exclude = bin_fileitem .oss_items [0 ].exclude
174+ bin_fileitem .oss_items = []
175+ for src_oss_item in src_fileitem .oss_items :
176+ src_oss_item .exclude = exclude
177+ bin_fileitem .oss_items .append (src_oss_item )
178+ bin_fileitem .comment = 'Loaded from SRC OSS info'
228179 if dup_flag :
229180 remove_src_idx_list .append (idx_src )
230181 if remove_src_idx_list :
231182 duplicates = True
232183 for i in sorted (remove_src_idx_list , reverse = True ):
233- del src_oss_list [i ]
184+ del src_fileitems [i ]
234185 except Exception as ex :
235186 logger .warning (f"correct the scanner result:{ ex } " )
236187
237188 try :
238- if exist_src :
239- success , err_msg = write_output_with_osslist (src_oss_list , _output_dir , output_files ['SRC' ],
240- output_extension , SRC_SHEET )
241- if not success :
242- logger .warning (err_msg )
243- if exist_bin :
244- success , err_msg = write_output_with_osslist (bin_oss_list , _output_dir , output_files ['BIN' ],
245- output_extension , BIN_SHEET , BIN_EXT_HEADER , BIN_HIDDEN_HEADER )
246- if not success :
247- logger .warning (err_msg )
248189 if duplicates :
249190 logger .info ('Success to correct the src/bin scanner result' )
250191 except Exception as ex :
251192 logger .warning (f"Corrected src/bin scanner result:{ ex } " )
252- return
253-
254-
255- def write_output_with_osslist (oss_list , output_dir , output_file , output_extension , sheetname , extended_hdr = {}, hidden_hdr = {}):
256- new_oss_list = []
257- sheet_list = {}
258- sheet_list [sheetname ] = []
259-
260- for src_item in oss_list :
261- scanner_name = constant .supported_sheet_and_scanner [sheetname ]
262- new_oss_list .append (src_item .get_print_array (scanner_name )[0 ])
263- sheet_list [sheetname ].extend (new_oss_list )
264- if os .path .exists (os .path .join (output_dir , output_file )):
265- os .remove (os .path .join (output_dir , output_file ))
266- success , err_msg , _ = write_output_file (os .path .join (output_dir , output_file ).rstrip (output_extension ),
267- output_extension , sheet_list , extended_hdr , hidden_hdr )
268- return success , err_msg
193+ return all_scan_item
269194
270195
271196def get_osslist (_output_dir , output_file , output_extension , sheet_name = '' ):
@@ -285,14 +210,12 @@ def get_osslist(_output_dir, output_file, output_extension, sheet_name=''):
285210 return oss_list
286211
287212
288- def check_exclude_dir (oss_list ):
213+ def check_exclude_dir (source_name_or_path ):
289214 _exclude_dirs = ["venv" , "node_modules" , "Pods" , "Carthage" ]
215+ exclude = False
290216
291- for oss_item in oss_list :
292- if not oss_item .source_name_or_path :
293- continue
294- for exclude_dir in _exclude_dirs :
295- if exclude_dir in oss_item .source_name_or_path [0 ].split (os .path .sep ):
296- oss_item .exclude = True
297- break
298- return oss_list
217+ for exclude_dir in _exclude_dirs :
218+ if exclude_dir in source_name_or_path .split (os .path .sep ):
219+ exclude = True
220+ break
221+ return exclude
0 commit comments