From f1288ccb6334182432624d504883ee740fc1d6b8 Mon Sep 17 00:00:00 2001 From: Jiyeong Seok Date: Mon, 15 May 2023 18:13:40 +0900 Subject: [PATCH] Add the option for correction with sbom-info.yaml Signed-off-by: Jiyeong Seok --- src/fosslight_source/_help.py | 4 +++- src/fosslight_source/cli.py | 28 ++++++++++++++++++++++++---- src/fosslight_source/run_scancode.py | 11 +++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/fosslight_source/_help.py b/src/fosslight_source/_help.py index 746b53c..6ac1748 100644 --- a/src/fosslight_source/_help.py +++ b/src/fosslight_source/_help.py @@ -28,7 +28,9 @@ -s \t Select which scanner to be run (scancode, scanoss, all) -j\t\t\t Generate raw result of scanners in json format -t \t\t Stop scancode scanning if scanning takes longer than a timeout in seconds. - -c \t\t Select the number of cores to be scanned with ScanCode.""" + -c \t\t Select the number of cores to be scanned with ScanCode. + --no_correction\t Enter if you don't want to correct OSS information with sbom-info.yaml + --correct_fpath\t Path to the sbom-info.yaml file""" def print_version(pkg_name): diff --git a/src/fosslight_source/cli.py b/src/fosslight_source/cli.py index 48c4a5d..7a91ded 100755 --- a/src/fosslight_source/cli.py +++ b/src/fosslight_source/cli.py @@ -15,6 +15,7 @@ from ._help import print_version, print_help_msg_source_scanner from ._license_matched import get_license_list_to_print from fosslight_util.output_format import check_output_format, write_output_file +from fosslight_util.correct import correct_with_yaml from .run_scancode import run_scan from .run_scanoss import run_scanoss_py from .run_scanoss import get_scanoss_extra_info @@ -47,6 +48,9 @@ def main(): print_matched_text = False format = "" selected_scanner = "" + correct_mode = True + correct_filepath = os.getcwd() + scanned_result = [] license_list = [] @@ -64,6 +68,8 @@ def main(): parser.add_argument('-s', '--scanner', nargs=1, type=str, required=False, default='all') parser.add_argument('-t', '--timeout', type=int, required=False, default=120) parser.add_argument('-c', '--cores', type=int, required=False, default=-1) + parser.add_argument('--no_correction', action='store_true', required=False) + parser.add_argument('--correct_fpath', nargs=1, type=str, required=False) args = parser.parse_args() @@ -84,6 +90,10 @@ def main(): format = ''.join(args.format) if args.scanner: selected_scanner = ''.join(args.scanner) + if args.no_correction: + correct_mode = False + if args.correct_fpath: + correct_filepath = ''.join(args.correct_fpath) time_out = args.timeout core = args.cores @@ -108,7 +118,8 @@ def main(): success, _result_log["Scan Result"], scanned_result, license_list = run_scan(path_to_scan, output_file_name, write_json_file, core, True, print_matched_text, format, True, - time_out) + time_out, correct_mode, + correct_filepath) elif selected_scanner == 'scanoss': scanned_result = run_scanoss_py(path_to_scan, output_file_name, format, True, write_json_file) elif selected_scanner == 'all' or selected_scanner == '': @@ -120,7 +131,7 @@ def main(): print_help_msg_source_scanner() sys.exit(1) create_report_file(_start_time, scanned_result, license_list, selected_scanner, print_matched_text, - output_path, output_file, output_extension) + output_path, output_file, output_extension, correct_mode, correct_filepath) try: logger.info(yaml.safe_dump(_result_log, allow_unicode=True, sort_keys=True)) except Exception as ex: @@ -131,7 +142,7 @@ def main(): def create_report_file(_start_time, scanned_result, license_list, selected_scanner, need_license=False, - output_path="", output_file="", output_extension=""): + output_path="", output_file="", output_extension="", correct_mode=True, correct_filepath=""): """ Create report files for given scanned result. @@ -178,6 +189,14 @@ def create_report_file(_start_time, scanned_result, license_list, selected_scann sheet_list["scancode_reference"] = get_license_list_to_print(license_list) sheet_list["scanoss_reference"] = get_scanoss_extra_info(scanned_result) + if correct_mode: + success, msg_correct, correct_list = correct_with_yaml(correct_filepath, sheet_list) + if not success: + logger.info(f"No correction with yaml: {msg_correct}") + else: + sheet_list = correct_list + logger.info("Success to correct with yaml.") + output_file_without_ext = os.path.join(output_path, output_file) success_to_write, writing_msg, result_file = write_output_file(output_file_without_ext, output_extension, sheet_list, extended_header) @@ -216,7 +235,8 @@ def run_all_scanners(path_to_scan, output_file_name="", _write_json_file=False, success, _result_log["Scan Result"], scancode_result, license_list = run_scan(path_to_scan, output_file_name, _write_json_file, num_cores, True, need_license, - format, called_by_cli, time_out) + format, called_by_cli, time_out, + False, "") scanoss_result = run_scanoss_py(path_to_scan, output_file_name, format, called_by_cli, _write_json_file) for file_in_scancode_result in scancode_result: diff --git a/src/fosslight_source/run_scancode.py b/src/fosslight_source/run_scancode.py index 25c69c5..f6c0667 100755 --- a/src/fosslight_source/run_scancode.py +++ b/src/fosslight_source/run_scancode.py @@ -16,6 +16,7 @@ from ._parsing_scancode_file_item import get_error_from_header from ._license_matched import get_license_list_to_print from fosslight_util.output_format import check_output_format, write_output_file +from fosslight_util.correct import correct_with_yaml logger = logging.getLogger(constant.LOGGER_NAME) warnings.filterwarnings("ignore", category=FutureWarning) @@ -24,7 +25,7 @@ def run_scan(path_to_scan, output_file_name="", _write_json_file=False, num_cores=-1, return_results=False, need_license=False, format="", - called_by_cli=False, time_out=120): + called_by_cli=False, time_out=120, correct_mode=True, correct_filepath=""): if not called_by_cli: global logger @@ -100,13 +101,19 @@ def run_scan(path_to_scan, output_file_name="", output_file_without_ext = os.path.join(output_path, output_file) if not called_by_cli: + if correct_mode: + success, msg_correct, correct_list = correct_with_yaml(correct_filepath, sheet_list) + if not success: + logger.info(f"No correction with yaml: {msg_correct}") + else: + sheet_list = correct_list + logger.info("Success to correct with yaml.") success_to_write, writing_msg, result_file = write_output_file(output_file_without_ext, output_extension, sheet_list) if success_to_write: logger.info(f"Writing Output file({result_file}, success:{success_to_write}") else: logger.error(f"Fail to generate result file. msg:({writing_msg})") - except Exception as ex: success = False msg = str(ex)