|
22 | 22 | specific language governing permissions and limitations
|
23 | 23 | under the License.
|
24 | 24 |
|
25 |
| -This scrit will override policies for components that are listed in a CSV file |
| 25 | +This scrit will override policies for components that are listed in a CSV or an EXCEL file |
26 | 26 | Cmponents with status IN_VIOLATION will get policy status to IN_VIOLATION_OVERRIDEN
|
27 |
| -identification of a project and cmponent wil be done based n following fields in the CSV file |
| 27 | +
|
| 28 | +Note: Override will happen only when the Override Date field is blank. |
| 29 | + Rows with blank Override Date and Ovveride Rationale will be ignored. |
| 30 | +
|
| 31 | +Note: EXCEL processing is rather simplistic, it will not process milti-sheet workbooks properly. |
| 32 | + It will skip all the header rows, until it finds "Name of Software Component" header. |
| 33 | + Rows after that will be processed according to abovementioned rules. |
| 34 | +
|
| 35 | +identification of a project and cmponent wil be done based n following fields in the input file |
28 | 36 |
|
29 | 37 | component_name = field 0 (Column A in Excel lingo)
|
30 | 38 | component_version = field 1 (Column B in Excel lingo)
|
|
41 | 49 | -h Show help
|
42 | 50 | -u BASE_URL URL of a Blackduck system
|
43 | 51 | -t TOKEN_FILE Authentication token file
|
44 |
| - -i INPUT_FILE Input CSV file |
| 52 | + -i INPUT_FILE Input CSV or EXCEL file |
45 | 53 | -nv Trust TLS certificate
|
46 | 54 |
|
47 | 55 |
|
|
78 | 86 | import json
|
79 | 87 | import logging
|
80 | 88 | import arrow
|
| 89 | +import re |
81 | 90 |
|
82 | 91 | from itertools import islice
|
83 | 92 | from datetime import timedelta
|
@@ -124,28 +133,56 @@ def parse_command_args():
|
124 | 133 |
|
125 | 134 | return parser.parse_args()
|
126 | 135 |
|
127 |
| -def main(): |
128 |
| - args = parse_command_args() |
129 |
| - with open(args.token_file, 'r') as tf: |
130 |
| - access_token = tf.readline().strip() |
131 |
| - global bd |
132 |
| - bd = Client(base_url=args.base_url, token=access_token, verify=args.no_verify, timeout=60.0, retries=4) |
133 |
| - |
| 136 | +def process_csv_file(filename): |
134 | 137 | file = open(args.input_file)
|
135 | 138 | type(file)
|
136 |
| - |
137 | 139 | csvreader = csv.reader(file)
|
138 |
| - |
139 | 140 | for row in csvreader:
|
140 | 141 | component_name = row[0]
|
141 | 142 | component_version = row[1]
|
142 | 143 | policy_violation_status = row[8]
|
| 144 | + override_date = row[10] |
143 | 145 | override_rationale = row[11]
|
144 | 146 | project_name = row[13]
|
145 | 147 | project_version = row[14]
|
146 |
| - if policy_violation_status == 'IN_VIOLATION': |
| 148 | + if policy_violation_status == 'IN_VIOLATION' and override_rationale and not override_date: |
147 | 149 | logging.info(f"Attemting to override policy status for {component_name} {component_version} in {project_name} {project_version} with ''{override_rationale}''")
|
148 | 150 | override_policy_violaton(project_name, project_version, component_name, component_version, override_rationale)
|
149 | 151 |
|
| 152 | +def process_excel_file(filename): |
| 153 | + import openpyxl |
| 154 | + wb = openpyxl.load_workbook(filename) |
| 155 | + ws = wb.active |
| 156 | + process = False |
| 157 | + for row in ws.values: |
| 158 | + if process: |
| 159 | + component_name = row[0] |
| 160 | + component_version = row[1] |
| 161 | + policy_violation_status = row[8] |
| 162 | + override_date = row[10] |
| 163 | + override_rationale = row[11] |
| 164 | + project_name = row[13] |
| 165 | + project_version = row[14] |
| 166 | + if policy_violation_status == 'IN_VIOLATION' and override_rationale and not override_date: |
| 167 | + print ("overriding") |
| 168 | + logging.info(f"Attemting to override policy status for {component_name} {component_version} in {project_name} {project_version} with ''{override_rationale}''") |
| 169 | + override_policy_violaton(project_name, project_version, component_name, component_version, override_rationale) |
| 170 | + if not process: |
| 171 | + process = (row[0] == "Name of Software Component") |
| 172 | + |
| 173 | +def main(): |
| 174 | + args = parse_command_args() |
| 175 | + with open(args.token_file, 'r') as tf: |
| 176 | + access_token = tf.readline().strip() |
| 177 | + global bd |
| 178 | + bd = Client(base_url=args.base_url, token=access_token, verify=args.no_verify, timeout=60.0, retries=4) |
| 179 | + |
| 180 | + if re.match(".+xlsx?$", args.input_file): |
| 181 | + print (f"Processing EXCEL file {args.input_file}") |
| 182 | + process_excel_file(args.input_file) |
| 183 | + else: |
| 184 | + print ("Processing as CSV") |
| 185 | + process_csv_file(args.input_file) |
| 186 | + |
150 | 187 | if __name__ == "__main__":
|
151 | 188 | sys.exit(main())
|
0 commit comments