fosslight-util 1.4.34__py3-none-any.whl → 2.1.28__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fosslight_util/correct.py CHANGED
@@ -7,19 +7,16 @@ import logging
7
7
  import os
8
8
  import copy
9
9
  import re
10
- from fosslight_util.constant import LOGGER_NAME
10
+ from fosslight_util.constant import LOGGER_NAME, FOSSLIGHT_SOURCE
11
11
  from fosslight_util.parsing_yaml import parsing_yml
12
- import fosslight_util.constant as constant
13
- from fosslight_util.oss_item import OssItem
14
12
 
15
13
  logger = logging.getLogger(LOGGER_NAME)
16
14
  SBOM_INFO_YAML = r"sbom(-|_)info[\s\S]*.ya?ml"
17
15
 
18
16
 
19
- def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list):
17
+ def correct_with_yaml(correct_filepath, path_to_scan, scan_item):
20
18
  success = True
21
19
  msg = ""
22
- correct_list = {}
23
20
  correct_yaml = ""
24
21
  if correct_filepath == "":
25
22
  correct_filepath = path_to_scan
@@ -33,87 +30,58 @@ def correct_with_yaml(correct_filepath, path_to_scan, scanner_oss_list):
33
30
  if not correct_yaml:
34
31
  msg = f"Cannot find sbom-info.yaml in {correct_filepath}."
35
32
  success = False
36
- return success, msg, correct_list
33
+ return success, msg, scan_item
37
34
 
38
35
  rel_path = os.path.relpath(path_to_scan, correct_filepath)
39
36
 
40
- yaml_oss_list, _, err_msg = parsing_yml(correct_yaml, os.path.dirname(correct_yaml), print_log=True)
41
-
37
+ yaml_file_list, _, err_msg = parsing_yml(correct_yaml, os.path.dirname(correct_yaml), print_log=True)
42
38
  find_match = False
43
- matched_yaml = []
44
- for yitem in yaml_oss_list:
45
- matched_yaml.append([0]*len(yitem.source_name_or_path))
46
-
47
- for sheet_name, sheet_contents in scanner_oss_list.items():
48
- if sheet_name not in constant.supported_sheet_and_scanner.keys():
49
- continue
50
- correct_contents = copy.deepcopy(sheet_contents)
51
- for idx, oss_raw_item in enumerate(sheet_contents):
52
- if len(oss_raw_item) < 9:
53
- logger.warning(f"sheet list is too short ({len(oss_raw_item)}): {oss_raw_item}")
39
+ for scanner_name, _ in scan_item.file_items.items():
40
+ correct_fileitems = []
41
+ exclude_fileitems = []
42
+ for yaml_file_item in yaml_file_list:
43
+ yaml_path_exists = False
44
+ if yaml_file_item.source_name_or_path == '':
45
+ if scanner_name == FOSSLIGHT_SOURCE:
46
+ correct_item = copy.deepcopy(yaml_file_item)
47
+ correct_item.comment = 'Added by sbom-info.yaml'
48
+ correct_fileitems.append(correct_item)
54
49
  continue
55
- oss_item = OssItem('')
56
- oss_item.set_sheet_item(oss_raw_item)
57
-
58
- matched_yi = []
59
- oss_rel_path = os.path.normpath(os.path.join(rel_path, oss_item.source_name_or_path[0]))
60
- for y_idx, yi in enumerate(yaml_oss_list):
61
- if not yi.source_name_or_path:
62
- continue
63
- for ys_idx, yi_path in enumerate(yi.source_name_or_path):
64
- yi_item = copy.deepcopy(yi)
65
- if ((os.path.normpath(yi_path) == os.path.normpath(oss_rel_path))
66
- or ((os.path.normpath(oss_rel_path).startswith(os.path.normpath(yi_path.rstrip('*')))))):
67
- find_match = True
68
- yi_item.source_name_or_path = []
69
- yi_item.source_name_or_path = oss_item.source_name_or_path[0]
70
- matched_yi.append(yi_item)
71
- matched_yaml[y_idx][ys_idx] = 1
72
- if len(matched_yi) > 0:
73
- for matched_yi_item in matched_yi:
74
- matched_oss_item = copy.deepcopy(matched_yi_item)
75
- if matched_oss_item.comment:
76
- matched_oss_item.comment += '/'
77
- matched_oss_item.comment += 'Loaded from sbom-info.yaml'
78
- matched_oss_array = matched_oss_item.get_print_array()[0]
79
- correct_contents.append(matched_oss_array)
80
- oss_item.exclude = True
81
- if oss_item.comment:
82
- oss_item.comment += '/'
83
- oss_item.comment += 'Excluded by sbom-info.yaml'
84
- correct_contents[idx] = oss_item.get_print_array()[0]
50
+ for idx, scan_file_item in enumerate(scan_item.file_items[scanner_name]):
51
+ oss_rel_path = os.path.normpath(os.path.join(rel_path, scan_file_item.source_name_or_path))
52
+ yi_path = yaml_file_item.source_name_or_path
53
+ if ((os.path.normpath(yi_path) == os.path.normpath(oss_rel_path)) or
54
+ ((os.path.normpath(oss_rel_path).startswith(os.path.normpath(yi_path.rstrip('*')))))):
55
+ correct_item = copy.deepcopy(scan_file_item)
56
+ correct_item.exclude = yaml_file_item.exclude
57
+ correct_item.oss_items = copy.deepcopy(yaml_file_item.oss_items)
58
+ correct_item.comment = ''
59
+ correct_item.comment = 'Loaded from sbom-info.yaml'
60
+ correct_fileitems.append(correct_item)
85
61
 
86
- if sheet_name == 'SRC_FL_Source':
87
- for n_idx, ni in enumerate(matched_yaml):
88
- y_item = copy.deepcopy(yaml_oss_list[n_idx])
89
- all_matched = False
90
- if sum(ni) != 0:
91
- not_matched_path = []
92
- for idx, id in enumerate(ni):
93
- if not id:
94
- not_matched_path.append(y_item.source_name_or_path[idx])
95
- y_item.source_name_or_path = []
96
- y_item.source_name_or_path = not_matched_path
97
- if len(not_matched_path) == 0:
98
- all_matched = True
99
- if y_item.comment:
100
- y_item.comment += '/'
101
- y_item.comment += 'Added by sbom-info.yaml'
102
- if not (y_item.source_name_or_path or all_matched):
103
- correct_contents.append(y_item.get_print_array()[0])
104
- continue
105
- for y_path in y_item.source_name_or_path:
106
- y_item_i = copy.deepcopy(y_item)
107
- if not os.path.exists(os.path.normpath(os.path.join(correct_filepath, y_path))):
108
- y_item_i.exclude = True
109
- y_item_i.source_name_or_path = []
110
- y_item_i.source_name_or_path = y_path
111
- correct_contents.append(y_item_i.get_print_array()[0])
112
- correct_list[sheet_name] = correct_contents
62
+ yaml_path_exists = True
63
+ exclude_fileitems.append(idx)
64
+ if scanner_name == FOSSLIGHT_SOURCE and not yaml_path_exists:
65
+ correct_item = copy.deepcopy(yaml_file_item)
66
+ if os.path.exists(os.path.normpath(yaml_file_item.source_name_or_path)):
67
+ correct_item.comment = 'Loaded from sbom-info.yaml'
68
+ correct_fileitems.append(correct_item)
69
+ else:
70
+ correct_item.exclude = True
71
+ correct_item.comment = 'Added by sbom-info.yaml'
72
+ correct_fileitems.append(correct_item)
73
+ if correct_fileitems:
74
+ scan_item.append_file_items(correct_fileitems, scanner_name)
75
+ find_match = True
76
+ if exclude_fileitems:
77
+ exclude_fileitems = list(set(exclude_fileitems))
78
+ for e_idx in exclude_fileitems:
79
+ scan_item.file_items[scanner_name][e_idx].exclude = True
80
+ scan_item.file_items[scanner_name][e_idx].comment = 'Excluded by sbom-info.yaml'
113
81
 
114
82
  if not find_match:
115
83
  success = False
116
84
  err_msg = 'No match items in sbom-info.yaml'
117
- return success, err_msg, yaml_oss_list
85
+ return success, err_msg, scan_item
118
86
 
119
- return success, msg, correct_list
87
+ return success, msg, scan_item
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ # Copyright (c) 2024 LG Electronics Inc.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ import os
7
+ import sys
8
+ from fosslight_util.help import print_package_version
9
+
10
+
11
+ class CoverItem:
12
+ tool_name_key = "Tool information"
13
+ start_time_key = "Start time"
14
+ python_ver_key = "Python version"
15
+ analyzed_path_key = "Analyzed path"
16
+ excluded_path_key = "Excluded path"
17
+ comment_key = "Comment"
18
+
19
+ PKG_NAMES = [
20
+ "fosslight_scanner",
21
+ "fosslight_source",
22
+ "fosslight_dependency",
23
+ "fosslight_binary"
24
+ ]
25
+
26
+ def __init__(self, tool_name="", start_time="", input_path="", comment="", exclude_path=[], simple_mode=True):
27
+ if simple_mode:
28
+ self.tool_name = f'{tool_name} v{print_package_version(tool_name, "", False)}'
29
+ else:
30
+ first_pkg = f'{self.PKG_NAMES[0]} v{print_package_version(self.PKG_NAMES[0], "", False)}'
31
+ remaining_pkgs = ", ".join([
32
+ f'{pkg_name} v{print_package_version(pkg_name, "", False)}'
33
+ for pkg_name in self.PKG_NAMES[1:]
34
+ ])
35
+ self.tool_name = f'{first_pkg} ({remaining_pkgs})'
36
+
37
+ if start_time:
38
+ date, time = start_time.split('_')
39
+ self.start_time = f'{date}, {time[0:2]}:{time[2:4]}'
40
+ else:
41
+ self.start_time = ""
42
+ self.input_path = os.path.abspath(input_path)
43
+ self.exclude_path = ", ".join(exclude_path)
44
+ self.comment = comment
45
+
46
+ self.python_version = f'{sys.version_info.major}.{sys.version_info.minor}'
47
+
48
+ def __del__(self):
49
+ pass
50
+
51
+ def get_print_json(self):
52
+ json_item = {}
53
+ json_item[self.tool_name_key] = self.tool_name
54
+ json_item[self.start_time_key] = self.start_time
55
+ json_item[self.python_ver_key] = self.python_version
56
+ json_item[self.analyzed_path_key] = self.input_path
57
+ json_item[self.excluded_path_key] = self.exclude_path
58
+ json_item[self.comment_key] = self.comment
59
+
60
+ return json_item