77import os
88import warnings
99import logging
10- import copy
1110from datetime import datetime
1211import fosslight_util .constant as constant
1312from fosslight_util .set_log import init_log
2120from .run_scanoss import get_scanoss_extra_info
2221import yaml
2322import argparse
24-
25- SCANOSS_SHEET_NAME = 'SRC_FL_Source'
26- SCANOSS_HEADER = {SCANOSS_SHEET_NAME : ['ID' , 'Source Name or Path' , 'OSS Name' ,
27- 'OSS Version' , 'License' , 'Download Location' ,
28- 'Homepage' , 'Copyright Text' , 'Exclude' ,
29- 'Comment' ]}
30- MERGED_HEADER = {SCANOSS_SHEET_NAME : ['ID' , 'Source Name or Path' , 'OSS Name' ,
31- 'OSS Version' , 'License' , 'Download Location' ,
32- 'Homepage' , 'Copyright Text' , 'Exclude' ,
33- 'Comment' , 'license_reference' ]}
23+ from .run_spdx_extractor import get_spdx_downloads
24+ from ._scan_item import ScanItem
25+
26+ SRC_SHEET_NAME = 'SRC_FL_Source'
27+ SCANOSS_HEADER = {SRC_SHEET_NAME : ['ID' , 'Source Name or Path' , 'OSS Name' ,
28+ 'OSS Version' , 'License' , 'Download Location' ,
29+ 'Homepage' , 'Copyright Text' , 'Exclude' , 'Comment' ]}
30+ MERGED_HEADER = {SRC_SHEET_NAME : ['ID' , 'Source Name or Path' , 'OSS Name' ,
31+ 'OSS Version' , 'License' , 'Download Location' ,
32+ 'Homepage' , 'Copyright Text' , 'Exclude' , 'Comment' , 'license_reference' ]}
33+ SCANNER_TYPE = ['scancode' , 'scanoss' , 'all' , '' ]
3434
3535logger = logging .getLogger (constant .LOGGER_NAME )
3636warnings .filterwarnings ("ignore" , category = FutureWarning )
@@ -50,7 +50,6 @@ def main():
5050 selected_scanner = ""
5151 correct_mode = True
5252
53- scanned_result = []
5453 license_list = []
5554 scanoss_result = []
5655 time_out = 120
@@ -114,21 +113,28 @@ def main():
114113 True , logging .INFO , logging .DEBUG , _PKG_NAME , path_to_scan )
115114
116115 if os .path .isdir (path_to_scan ):
117- if selected_scanner == 'scancode' :
118- success , _result_log ["Scan Result" ], scanned_result , license_list = run_scan (path_to_scan , output_file_name ,
119- write_json_file , core , True ,
120- print_matched_text , format , True ,
121- time_out , correct_mode , correct_filepath )
122- elif selected_scanner == 'scanoss' :
123- scanned_result = run_scanoss_py (path_to_scan , output_file_name , format , True , write_json_file )
124- elif selected_scanner == 'all' or selected_scanner == '' :
125- success , _result_log ["Scan Result" ], scanned_result , license_list , scanoss_result = run_all_scanners (
126- path_to_scan , output_file_name , write_json_file , core , print_matched_text , format , True , time_out )
127- else :
116+ scancode_result = []
117+ scanoss_result = []
118+ merged_result = []
119+ spdx_downloads = {}
120+ success = True
121+
122+ if selected_scanner == 'scancode' or selected_scanner == 'all' or selected_scanner == '' :
123+ success , _result_log ["Scan Result" ], scancode_result , license_list = run_scan (path_to_scan , output_file_name ,
124+ write_json_file , core , True ,
125+ print_matched_text , format , True ,
126+ time_out , correct_mode ,
127+ correct_filepath )
128+ if selected_scanner == 'scanoss' or selected_scanner == 'all' or selected_scanner == '' :
129+ scanoss_result = run_scanoss_py (path_to_scan , output_file_name , format , True , write_json_file )
130+ if selected_scanner not in SCANNER_TYPE :
128131 print_help_msg_source_scanner ()
129132 sys .exit (1 )
130- create_report_file (_start_time , scanned_result , license_list , scanoss_result , selected_scanner , print_matched_text ,
133+ spdx_downloads = get_spdx_downloads (path_to_scan )
134+ merged_result = merge_results (scancode_result , scanoss_result , spdx_downloads )
135+ create_report_file (_start_time , merged_result , license_list , scanoss_result , selected_scanner , print_matched_text ,
131136 output_path , output_file , output_extension , correct_mode , correct_filepath , path_to_scan )
137+
132138 try :
133139 logger .info (yaml .safe_dump (_result_log , allow_unicode = True , sort_keys = True ))
134140 except Exception as ex :
@@ -138,7 +144,7 @@ def main():
138144 sys .exit (1 )
139145
140146
141- def create_report_file (_start_time , scanned_result , license_list , scanoss_result , selected_scanner , need_license = False ,
147+ def create_report_file (_start_time , merged_result , license_list , scanoss_result , selected_scanner , need_license = False ,
142148 output_path = "" , output_file = "" , output_extension = "" , correct_mode = True , correct_filepath = "" ,
143149 path_to_scan = "" ):
144150 """
@@ -167,25 +173,25 @@ def create_report_file(_start_time, scanned_result, license_list, scanoss_result
167173 else :
168174 output_file = f"fosslight_report_src_{ _start_time } "
169175
170- if scanned_result :
176+ if merged_result :
171177 if selected_scanner == 'scancode' or output_extension == _json_ext :
172- sheet_list [SCANOSS_SHEET_NAME ] = []
173- for scan_item in scanned_result :
178+ sheet_list [SRC_SHEET_NAME ] = []
179+ for scan_item in merged_result :
174180 for row in scan_item .get_row_to_print ():
175- sheet_list [SCANOSS_SHEET_NAME ].append (row )
181+ sheet_list [SRC_SHEET_NAME ].append (row )
176182
177183 elif selected_scanner == 'scanoss' :
178- sheet_list [SCANOSS_SHEET_NAME ] = []
179- for scan_item in scanned_result :
180- for row in scan_item .get_row_to_print_for_scanoss ():
181- sheet_list [SCANOSS_SHEET_NAME ].append (row )
184+ sheet_list [SRC_SHEET_NAME ] = []
185+ for scan_item in merged_result :
186+ for row in scan_item .get_row_to_print ():
187+ sheet_list [SRC_SHEET_NAME ].append (row )
182188 extended_header = SCANOSS_HEADER
183189
184190 else :
185- sheet_list [SCANOSS_SHEET_NAME ] = []
186- for scan_item in scanned_result :
187- for row in scan_item .get_row_to_print_for_all_scanner ():
188- sheet_list [SCANOSS_SHEET_NAME ].append (row )
191+ sheet_list [SRC_SHEET_NAME ] = []
192+ for scan_item in merged_result :
193+ for row in scan_item .get_row_to_print ():
194+ sheet_list [SRC_SHEET_NAME ].append (row )
189195 extended_header = MERGED_HEADER
190196
191197 if need_license :
@@ -217,47 +223,30 @@ def create_report_file(_start_time, scanned_result, license_list, scanoss_result
217223 logger .error (f"Fail to generate result file. msg:({ writing_msg } )" )
218224
219225
220- def run_all_scanners (path_to_scan , output_file_name = "" , _write_json_file = False , num_cores = - 1 ,
221- need_license = False , format = "" , called_by_cli = True , time_out = 120 ):
226+ def merge_results (scancode_result = [], scanoss_result = [], spdx_downloads = {}):
222227 """
223- Run Scancode and scanoss.py for the given path.
224-
225- :param path_to_scan: path of sourcecode to scan.
226- :param output_file_name: path or file name (with path) for the output.
227- :param _write_json_file: if requested, keep the raw files.
228- :param num_cores: number of cores used for scancode scanning.
229- :param need_license: if requested, output matched text (only for scancode).
230- :param format: output format (excel, csv, opossum).
231- :param called_by_cli: if not called by cli, initialize logger.
232- :return success: success or failure of scancode.
233- :return _result_log["Scan Result"]:
234- :return merged_result: merged scan result of scancode and scanoss.
235- :return license_list: matched text.(only for scancode)
228+ Merge scanner results and spdx parsing result.
229+ :param scancode_result: list of scancode results in ScanItem.
230+ :param scanoss_result: list of scanoss results in ScanItem.
231+ :param spdx_downloads: dictionary of spdx parsed results.
232+ :return merged_result: list of merged result in ScanItem.
236233 """
237- scancode_result = []
238- scanoss_result = []
239- merged_result = []
240- _result_log = {}
241- success = True
242234
243- success , _result_log ["Scan Result" ], scancode_result , license_list = run_scan (path_to_scan , output_file_name ,
244- _write_json_file , num_cores ,
245- True , need_license ,
246- format , called_by_cli , time_out ,
247- False , "" )
248- scanoss_result = run_scanoss_py (path_to_scan , output_file_name , format , called_by_cli , _write_json_file )
249-
250- scanoss_result_for_merging = copy .deepcopy (scanoss_result )
251- for file_in_scancode_result in scancode_result :
252- per_file_result = copy .deepcopy (file_in_scancode_result )
253- if per_file_result in scanoss_result_for_merging : # Remove SCANOSS result if Scancode result exist
254- scanoss_result_for_merging .pop (scanoss_result_for_merging .index (file_in_scancode_result ))
255- merged_result .append (per_file_result )
256- if scanoss_result_for_merging :
257- for file_left_in_scanoss_result in scanoss_result_for_merging :
258- merged_result .append (file_left_in_scanoss_result )
259-
260- return success , _result_log ["Scan Result" ], merged_result , license_list , scanoss_result
235+ # If anything that is found at SCANOSS only exist, add it to result.
236+ scancode_result .extend ([item for item in scanoss_result if item not in scancode_result ])
237+
238+ # If download loc. in SPDX form found, overwrite the scanner result.
239+ # If scanner result doesn't exist, create a new row.
240+ if spdx_downloads :
241+ for file_name , download_location in spdx_downloads .items ():
242+ if file_name in scancode_result :
243+ merged_result_item = scancode_result [scancode_result .index (file_name )]
244+ merged_result_item .download_location = download_location
245+ else :
246+ new_result_item = ScanItem (file_name )
247+ new_result_item .download_location = download_location
248+ scancode_result .append (new_result_item )
249+ return scancode_result
261250
262251
263252if __name__ == '__main__' :
0 commit comments