Skip to content

Commit dc0455a

Browse files
committed
Modify run_scanners to return
1 parent bbb26f0 commit dc0455a

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/fosslight_source/cli.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
logger = logging.getLogger(constant.LOGGER_NAME)
3636
warnings.filterwarnings("ignore", category=FutureWarning)
3737
_PKG_NAME = "fosslight_source"
38+
RESULT_KEY = "Scan Result"
3839

3940

4041
def main():
@@ -218,60 +219,63 @@ def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
218219
return scancode_result
219220

220221

221-
def run_scanners(path_to_scan, output_file_name="", _write_json_file=False, num_cores=-1, called_by_cli=True,
222+
def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_cores=-1, called_by_cli=True,
222223
print_matched_text=False, format="", time_out=120, correct_mode=True, correct_filepath="",
223224
selected_scanner='all'):
224225
"""
225226
Run Scancode and scanoss.py for the given path.
226227
227228
:param path_to_scan: path of sourcecode to scan.
228229
:param output_file_name: path or file name (with path) for the output.
229-
:param _write_json_file: if requested, keep the raw files.
230+
:param write_json_file: if requested, keep the raw files.
230231
:param num_cores: number of cores used for scancode scanning.
231232
:param called_by_cli: if not called by cli, initialize logger.
232233
:param print_matched_text: if requested, output matched text (only for scancode).
233234
:param format: output format (excel, csv, opossum).
234235
:return success: success or failure of scancode.
235-
:return _result_log["Scan Result"]:
236+
:return result_log["Scan Result"]:
236237
:return merged_result: merged scan result of scancode and scanoss.
237238
:return license_list: matched text.(only for scancode)
238239
"""
239240
global logger
240241

241-
_start_time = datetime.now().strftime('%y%m%d_%H%M')
242+
start_time = datetime.now().strftime('%y%m%d_%H%M')
242243
scancode_result = []
243244
scanoss_result = []
244245
merged_result = []
245246
spdx_downloads = {}
247+
result_log = {}
246248

247249
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
248-
if output_extension != '.xlsx' and output_extension != "" and print_matched_text:
250+
logger, result_log = init_log(os.path.join(output_path, f"fosslight_log_src_{start_time}.txt"),
251+
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan)
252+
if output_extension != '.xlsx' and output_extension and print_matched_text:
249253
logger.warning("-m option is only available for excel.")
250254
print_matched_text = False
251-
if not success:
252-
logger.error(f"Format error. {msg}")
253-
sys.exit(1)
254-
logger, _result_log = init_log(os.path.join(output_path, f"fosslight_log_src_{_start_time}.txt"),
255-
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan)
256-
257-
if selected_scanner == 'scancode' or selected_scanner == 'all' or selected_scanner == '':
258-
success, _result_log["Scan Result"], scancode_result, license_list = run_scan(path_to_scan, output_file_name,
259-
_write_json_file, num_cores, True,
255+
if success:
256+
if selected_scanner == 'scancode' or selected_scanner == 'all' or selected_scanner == '':
257+
success, result_log[RESULT_KEY], scancode_result, license_list = run_scan(path_to_scan, output_file_name,
258+
write_json_file, num_cores, True,
260259
print_matched_text, format, called_by_cli,
261260
time_out, correct_mode, correct_filepath)
262-
if selected_scanner == 'scanoss' or selected_scanner == 'all' or selected_scanner == '':
263-
scanoss_result = run_scanoss_py(path_to_scan, output_file_name, format, True, _write_json_file)
264-
if selected_scanner not in SCANNER_TYPE:
265-
print_help_msg_source_scanner()
266-
sys.exit(1)
267-
268-
spdx_downloads = get_spdx_downloads(path_to_scan)
269-
merged_result = merge_results(scancode_result, scanoss_result, spdx_downloads)
270-
271-
create_report_file(_start_time, merged_result, license_list, scanoss_result, selected_scanner, print_matched_text,
272-
output_path, output_file, output_extension, correct_mode, correct_filepath, path_to_scan)
261+
if selected_scanner == 'scanoss' or selected_scanner == 'all' or selected_scanner == '':
262+
scanoss_result = run_scanoss_py(path_to_scan, output_file_name, format, True, write_json_file)
263+
if selected_scanner in SCANNER_TYPE:
264+
spdx_downloads = get_spdx_downloads(path_to_scan)
265+
merged_result = merge_results(scancode_result, scanoss_result, spdx_downloads)
266+
267+
create_report_file(start_time, merged_result, license_list, scanoss_result, selected_scanner, print_matched_text,
268+
output_path, output_file, output_extension, correct_mode, correct_filepath, path_to_scan)
269+
else:
270+
print_help_msg_source_scanner()
271+
result_log[RESULT_KEY] = "Unsupported scanner"
272+
success = False
273+
else:
274+
result_log[RESULT_KEY] = f"Format error. {msg}"
275+
logger.error(f"Format error. {msg}")
276+
success = False
273277

274-
return success, _result_log["Scan Result"], merged_result, license_list, scanoss_result
278+
return success, result_log.get(RESULT_KEY, ""), merged_result, license_list, scanoss_result
275279

276280

277281
if __name__ == '__main__':

0 commit comments

Comments
 (0)