From 45e27632725814f9a837a60bf3dd0921fef70e4c Mon Sep 17 00:00:00 2001 From: Jiyeong Seok <50347670+dd-jy@users.noreply.github.com> Date: Fri, 14 Jul 2023 11:36:24 +0900 Subject: [PATCH] Fix the issue for correct mode (#123) * Fix to maintain exclude value for sbom-info.yaml * Modify to sort the excel, csv result - 1st: exclude - 2nd: source name or path is empty - 3rd: source name or path * Fix the issue about sbom-info.yaml * Fix the magic number --------- Signed-off-by: Jiyeong Seok --- src/fosslight_util/correct.py | 10 +++++----- src/fosslight_util/write_excel.py | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/fosslight_util/correct.py b/src/fosslight_util/correct.py index 5769cf2..bcad220 100644 --- a/src/fosslight_util/correct.py +++ b/src/fosslight_util/correct.py @@ -36,7 +36,6 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list): return success, msg, correct_list rel_path = os.path.relpath(path_to_scan, correct_filepath) - rel_correct_path = os.path.relpath(correct_filepath, path_to_scan) yaml_oss_list, _, err_msg = parsing_yml(correct_yaml, os.path.dirname(correct_yaml), print_log=True) @@ -73,8 +72,6 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list): if len(matched_yi) > 0: for matched_yi_item in matched_yi: matched_oss_item = copy.deepcopy(matched_yi_item) - if oss_item.exclude: - matched_oss_item.exclude = oss_item.exclude if matched_oss_item.comment: matched_oss_item.comment += '/' matched_oss_item.comment += 'Loaded from sbom-info.yaml' @@ -89,6 +86,7 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list): if sheet_name == 'SRC_FL_Source': for n_idx, ni in enumerate(matched_yaml): y_item = copy.deepcopy(yaml_oss_list[n_idx]) + all_matched = False if sum(ni) != 0: not_matched_path = [] for idx, id in enumerate(ni): @@ -96,10 +94,12 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list): not_matched_path.append(y_item.source_name_or_path[idx]) y_item.source_name_or_path = [] y_item.source_name_or_path = not_matched_path + if len(not_matched_path) == 0: + all_matched = True if y_item.comment: y_item.comment += '/' y_item.comment += 'Added by sbom-info.yaml' - if not y_item.source_name_or_path: + if not (y_item.source_name_or_path or all_matched): correct_contents.append(y_item.get_print_array()[0]) continue for y_path in y_item.source_name_or_path: @@ -107,7 +107,7 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list): if not os.path.exists(os.path.normpath(os.path.join(correct_filepath, y_path))): y_item_i.exclude = True y_item_i.source_name_or_path = [] - y_item_i.source_name_or_path = os.path.join(rel_correct_path, y_path) + y_item_i.source_name_or_path = y_path correct_contents.append(y_item_i.get_print_array()[0]) correct_list[sheet_name] = correct_contents diff --git a/src/fosslight_util/write_excel.py b/src/fosslight_util/write_excel.py index 736f053..e24fe69 100755 --- a/src/fosslight_util/write_excel.py +++ b/src/fosslight_util/write_excel.py @@ -28,6 +28,8 @@ _OUTPUT_FILE_PREFIX = "FOSSLight-Report_" _EMPTY_ITEM_MSG = "* There is no item"\ " to print in FOSSLight-Report.\n" +IDX_FILE = 0 +IDX_EXCLUDE = 7 logger = logging.getLogger(constant.LOGGER_NAME) @@ -137,6 +139,8 @@ def write_result_to_csv(output_file, sheet_list_origin, separate_sheet=False, ex else: output_file = separate_output_file + "_" + sheet_name + file_extension + sheet_content_without_header = sorted(sheet_content_without_header, + key=lambda x: (x[IDX_EXCLUDE], x[IDX_FILE] == "", x[IDX_FILE])) with open(output_file, 'w', newline='') as file: writer = csv.writer(file, delimiter='\t') writer.writerow(header_row) @@ -166,6 +170,8 @@ def write_result_to_excel(out_file_name, sheet_list, extended_header={}): workbook = xlsxwriter.Workbook(out_file_name) for sheet_name, sheet_contents in sheet_list.items(): selected_header, sheet_content_without_header = get_header_row(sheet_name, sheet_contents[:], extended_header) + sheet_content_without_header = sorted(sheet_content_without_header, + key=lambda x: (x[IDX_EXCLUDE], x[IDX_FILE] == "", x[IDX_FILE])) worksheet = create_worksheet(workbook, sheet_name, selected_header) write_result_to_sheet(worksheet, sheet_content_without_header) workbook.close()