|
31 | 31 | import logging |
32 | 32 | import sys |
33 | 33 | import io |
34 | | -import os |
35 | | -import re |
36 | 34 | import time |
37 | | -import subprocess |
38 | 35 | import json |
39 | 36 | import traceback |
40 | | -import copy |
41 | | -import ijson |
42 | 37 | from blackduck import Client |
43 | 38 | from zipfile import ZipFile |
44 | 39 | from pprint import pprint |
|
49 | 44 |
|
50 | 45 | This script assumes a project version exists and has scans associated with it (i.e. the project is not scanned as part of this process). |
51 | 46 |
|
52 | | -Config file: |
53 | | -API Token and Black Duck URL need to be placed in the .restconfig.json file which must be placed in the same folder where this script resides. |
54 | | - { |
55 | | - "baseurl": "https://hub-hostname", |
56 | | - "api_token": "<API token goes here>", |
57 | | - "insecure": true or false <Default is false>, |
58 | | - "debug": true or false <Default is false> |
59 | | - } |
60 | | -
|
61 | | -Remarks: |
62 | | -This script uses 3rd party PyPI package "ijson". This package must be installed. |
63 | 47 | ''' |
64 | 48 |
|
65 | 49 | # BD report general |
@@ -134,26 +118,24 @@ def create_version_details_report(bd, version): |
134 | 118 | if (r.status_code == 403): |
135 | 119 | logging.debug("Authorization Error - Please ensure the token you are using has write permissions!") |
136 | 120 | r.raise_for_status() |
137 | | - pprint(r.headers) |
138 | 121 | location = r.headers.get('Location') |
139 | 122 | assert location, "Hmm, this does not make sense. If we successfully created a report then there needs to be a location where we can get it from" |
140 | 123 | return location |
141 | 124 |
|
142 | 125 | def download_report(bd, location, retries): |
143 | 126 | report_id = location.split("/")[-1] |
144 | | - print (location) |
| 127 | + logging.debug(f"Report location {location}") |
145 | 128 | url_data = location.split('/') |
146 | 129 | url_data.pop(4) |
147 | 130 | url_data.pop(4) |
148 | 131 | download_link = '/'.join(url_data) |
149 | | - print(download_link) |
| 132 | + logging.debug(f"Report Download link {download_link}") |
150 | 133 | if retries: |
151 | | - logging.debug(f"Retrieving generated report from {location}") |
| 134 | + logging.debug(f"Retrieving generated report for {location} via {download_link}") |
152 | 135 | response = bd.session.get(location) |
153 | 136 | report_status = response.json().get('status', 'Not Ready') |
154 | 137 | if response.status_code == 200 and report_status == 'COMPLETED': |
155 | 138 | response = bd.session.get(download_link, headers={'Content-Type': 'application/zip', 'Accept':'application/zip'}) |
156 | | - pprint(response) |
157 | 139 | if response.status_code == 200: |
158 | 140 | return response.content |
159 | 141 | else: |
@@ -204,17 +186,18 @@ def main(): |
204 | 186 |
|
205 | 187 | project = find_project_by_name(hub_client, args.project_name) |
206 | 188 | version = find_project_version_by_name(hub_client, project, args.project_version_name) |
207 | | - pprint(version) |
208 | 189 | location = create_version_details_report(hub_client, version) |
209 | | - pprint(location) |
210 | 190 | report_zip = download_report(hub_client, location, args.report_retries) |
211 | | - pprint(report_zip) |
212 | 191 | logging.debug(f"Deleting report from Black Duck {hub_client.session.delete(location)}") |
213 | 192 | zip=ZipFile(io.BytesIO(report_zip), "r") |
214 | 193 | pprint(zip.namelist()) |
215 | 194 | report_data = {name: zip.read(name) for name in zip.namelist()} |
216 | 195 | filename = [i for i in report_data.keys() if i.endswith(".json")][0] |
217 | | - pprint(json.loads(report_data[filename])) |
| 196 | + version_report = json.loads(report_data[filename]) |
| 197 | + # TODO items |
| 198 | + # Process file section of report data to identify primary paths |
| 199 | + # Combine component data with selected file data |
| 200 | + # Output result with CSV anf JSON as options. |
218 | 201 |
|
219 | 202 |
|
220 | 203 | except (Exception, BaseException) as err: |
|
0 commit comments