Skip to content

Commit c5b5428

Browse files
author
Murat Kumykov
committed
updating black override date columns only
1 parent 9e41a86 commit c5b5428

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

examples/batch_policy_override.py

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222
specific language governing permissions and limitations
2323
under the License.
2424
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
2626
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
2836
2937
component_name = field 0 (Column A in Excel lingo)
3038
component_version = field 1 (Column B in Excel lingo)
@@ -41,7 +49,7 @@
4149
-h Show help
4250
-u BASE_URL URL of a Blackduck system
4351
-t TOKEN_FILE Authentication token file
44-
-i INPUT_FILE Input CSV file
52+
-i INPUT_FILE Input CSV or EXCEL file
4553
-nv Trust TLS certificate
4654
4755
@@ -78,6 +86,7 @@
7886
import json
7987
import logging
8088
import arrow
89+
import re
8190

8291
from itertools import islice
8392
from datetime import timedelta
@@ -124,28 +133,56 @@ def parse_command_args():
124133

125134
return parser.parse_args()
126135

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):
134137
file = open(args.input_file)
135138
type(file)
136-
137139
csvreader = csv.reader(file)
138-
139140
for row in csvreader:
140141
component_name = row[0]
141142
component_version = row[1]
142143
policy_violation_status = row[8]
144+
override_date = row[10]
143145
override_rationale = row[11]
144146
project_name = row[13]
145147
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:
147149
logging.info(f"Attemting to override policy status for {component_name} {component_version} in {project_name} {project_version} with ''{override_rationale}''")
148150
override_policy_violaton(project_name, project_version, component_name, component_version, override_rationale)
149151

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+
150187
if __name__ == "__main__":
151188
sys.exit(main())

0 commit comments

Comments
 (0)