Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions src/fosslight_util/correct.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list):
correct_list = {}
correct_yaml = ""
if correct_filepath == "":
correct_filepath = os.getcwd()
correct_filepath = path_to_scan

path_to_scan = os.path.normpath(path_to_scan)
correct_filepath = os.path.normpath(correct_filepath)
for filename in os.listdir(correct_filepath):
if re.search(SBOM_INFO_YAML, filename, re.IGNORECASE):
correct_yaml = os.path.join(correct_filepath, filename)
Expand All @@ -34,10 +36,15 @@ 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)

find_match = False
matched_yaml = []
for yitem in yaml_oss_list:
matched_yaml.append([0]*len(yitem.source_name_or_path))

for sheet_name, sheet_contents in scanner_oss_list.items():
if sheet_name not in constant.supported_sheet_and_scanner.keys():
continue
Expand All @@ -50,37 +57,58 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list):
oss_item.set_sheet_item(oss_raw_item)

matched_yi = []
oss_rel_path = os.path.join(rel_path, oss_item.source_name_or_path[0])
for yi in yaml_oss_list:
filtered_exact = next(filter(lambda ys: os.path.normpath(ys) == os.path.normpath(oss_rel_path),
yi.source_name_or_path), None)
if filtered_exact:
find_match = True
matched_yi.append(yi)
oss_rel_path = os.path.normpath(os.path.join(rel_path, oss_item.source_name_or_path[0]))
for y_idx, yi in enumerate(yaml_oss_list):
if not yi.source_name_or_path:
continue
filtered_contain = next(filter(lambda ys:
os.path.normpath(oss_rel_path).startswith(os.path.normpath(ys.rstrip('*'))),
yi.source_name_or_path), None)
if filtered_contain:
find_match = True
matched_yi.append(yi)
for ys_idx, yi_path in enumerate(yi.source_name_or_path):
yi_item = copy.deepcopy(yi)
if ((os.path.normpath(yi_path) == os.path.normpath(oss_rel_path))
or ((os.path.normpath(oss_rel_path).startswith(os.path.normpath(yi_path.rstrip('*')))))):
find_match = True
yi_item.source_name_or_path = []
yi_item.source_name_or_path = oss_item.source_name_or_path[0]
matched_yi.append(yi_item)
matched_yaml[y_idx][ys_idx] = 1
if len(matched_yi) > 0:
for mi in matched_yi:
matched_oss_item = copy.deepcopy(mi)
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'
matched_oss_item.source_name_or_path = []
matched_oss_item.source_name_or_path = oss_item.source_name_or_path[0]
matched_oss_array = matched_oss_item.get_print_array()[0]
correct_contents.append(matched_oss_array)
oss_item.exclude = True
if oss_item.comment:
oss_item.comment += '/'
oss_item.comment += 'Excluded by sbom-info.yaml'
correct_contents[idx] = oss_item.get_print_array()[0]

if sheet_name == 'SRC_FL_Source':
for n_idx, ni in enumerate(matched_yaml):
y_item = copy.deepcopy(yaml_oss_list[n_idx])
if sum(ni) != 0:
not_matched_path = []
for idx, id in enumerate(ni):
if not id:
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 y_item.comment:
y_item.comment += '/'
y_item.comment += 'Added by sbom-info.yaml'
if not y_item.source_name_or_path:
correct_contents.append(y_item.get_print_array()[0])
continue
for y_path in y_item.source_name_or_path:
y_item_i = copy.deepcopy(y_item)
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)
correct_contents.append(y_item_i.get_print_array()[0])
correct_list[sheet_name] = correct_contents

if not find_match:
Expand Down