Skip to content

Commit 4b24cde

Browse files
committed
new project format spec file added
1 parent 11b2721 commit 4b24cde

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

examples/client/multi-image/manage_project_structure.py

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -204,31 +204,53 @@ def add_component_to_version_bom(child_version, version):
204204
data = { 'component': child_version['_meta']['href']}
205205
return bd.session.post(url, json=data)
206206

207+
def process_excel_spec_file(wb):
208+
ws = wb.active
209+
project_list = []
210+
row_number = 0
211+
for row in ws.values:
212+
row_number += 1
213+
if (row_number == 1 and
214+
row[0] == 'Container Name' and
215+
row[1] == 'Image ID' and
216+
row[2] == 'Version' and
217+
row[3] == 'Project Name'):
218+
logging.info(f"File Format checks out (kind of)")
219+
continue
220+
elif row_number > 1:
221+
project_list.append(f"{row[3]}:{row[0]}:{row[2]}")
222+
else:
223+
logging.error(f"Could not parse input file {args.subproject_spec_file}")
224+
sys.exit(1)
225+
return (project_list)
226+
227+
def process_text_spec_file(args):
228+
project_list = []
229+
prefix = args.string_to_put_in_front_of_subproject_name
230+
if not prefix:
231+
prefix = args.project_name
232+
with open(args.subproject_spec_file, "r") as f:
233+
lines = f.read().splitlines()
234+
for line in lines:
235+
image_name = line.split('/')[-1].split(':')[0] # Don't look at me, you wrote it!
236+
sub_project_name = "_".join((prefix, image_name))
237+
spec_line = ":".join((sub_project_name, line))
238+
if "ciena.com" in spec_line:
239+
project_list.append(spec_line)
240+
return (project_list)
241+
207242
def get_child_spec_list(args):
208243
if args.subproject_list:
209244
return args.subproject_list.split(',')
210245
else:
246+
# Excel and plaintext
211247
logging.info(f"Processing excel file {args.subproject_spec_file}")
212248
import openpyxl
213-
wb = openpyxl.load_workbook(args.subproject_spec_file)
214-
ws = wb.active
215-
project_list = []
216-
row_number = 0
217-
for row in ws.values:
218-
row_number += 1
219-
if (row_number == 1 and
220-
row[0] == 'Container Name' and
221-
row[1] == 'Image ID' and
222-
row[2] == 'Version' and
223-
row[3] == 'Project Name'):
224-
logging.info(f"File Format checks out (kind of)")
225-
continue
226-
elif row_number > 1:
227-
project_list.append(f"{row[3]}:{row[0]}:{row[2]}")
228-
else:
229-
logging.error(f"Could not parse input file {args.subproject_spec_file}")
230-
sys.exit(1)
231-
return (project_list)
249+
try:
250+
wb = openpyxl.load_workbook(args.subproject_spec_file)
251+
return process_excel_spec_file(wb)
252+
except Exception:
253+
return process_text_spec_file(args)
232254

233255
def create_and_add_child_projects(version, args):
234256
version_url = version['_meta']['href'] + '/components'
@@ -321,16 +343,20 @@ def scan_container_images(scan_params, hub):
321343
project_group = params.get('project_group', None)
322344
if project_group:
323345
detect_options += f" --detect.project.group.name=\"{project_group}\""
324-
scan_container_image(
325-
params['image'],
326-
None,
327-
None,
328-
None,
329-
params['project'],
330-
params['version'],
331-
detect_options,
332-
hub=hub
333-
)
346+
try:
347+
scan_container_image(
348+
params['image'],
349+
None,
350+
None,
351+
None,
352+
params['project'],
353+
params['version'],
354+
detect_options,
355+
hub=hub
356+
)
357+
except Exception:
358+
logging.error(f"Scanning of {params['image']} failed, skipping")
359+
skipped_scans.append(params)
334360

335361

336362
def parse_command_args():
@@ -343,20 +369,22 @@ def parse_command_args():
343369
parser.add_argument("-pv", "--version-name", required=True, help="Project Version Name")
344370
group = parser.add_mutually_exclusive_group()
345371
group.add_argument("-sp", "--subproject-list", required=False, help="List of subprojects to generate with subproject:container:tag")
346-
group.add_argument("-ssf", "--subproject-spec-file", required=False, help="Excel file containing subproject specification")
372+
group.add_argument("-ssf", "--subproject-spec-file", required=False, help="Excel or txt file containing subproject specification")
347373
parser.add_argument("-nv", "--no-verify", action='store_false', help="Disable TLS certificate verification")
348374
parser.add_argument("-rm", "--remove", action='store_true', required=False, help="Remove project structure with all subprojects (DANGEROUS!)")
349375
parser.add_argument("--clone-from", required=False, help="Main project version to use as template for cloning")
350376
parser.add_argument("--dry-run", action='store_true', required=False, help="Create structure only, do not execute scans")
377+
parser.add_argument("-str", "--string-to-put-in-front-of-subproject-name", required=False, help="Prefix string for subproject names" )
351378
return parser.parse_args()
352379

353380
def main():
354381
args = parse_command_args()
355382
with open(args.token_file, 'r') as tf:
356383
access_token = tf.readline().strip()
357384
global bd
358-
global scan_params
385+
global scan_params, skipped_scans
359386
scan_params = []
387+
skipped_scans = []
360388
bd = Client(base_url=args.base_url, token=access_token, verify=args.no_verify, timeout=60.0, retries=4)
361389
logging.info(f"{args}")
362390

@@ -367,10 +395,14 @@ def main():
367395
if args.dry_run:
368396
logging.info(f"{pformat(scan_params)}")
369397
else:
370-
logging.info("Now execution scans")
398+
logging.info("Now executing scans")
371399
from blackduck.HubRestApi import HubInstance
372400
hub = HubInstance(args.base_url, api_token=access_token, insecure=True, debug=False)
373401
scan_container_images(scan_params, hub)
402+
if len(skipped_scans) > 0:
403+
logging.info(f"The following images were not scanned")
404+
logging.info(f"{pformat(skipped_scans)}")
405+
374406

375407

376408
if __name__ == "__main__":

0 commit comments

Comments
 (0)