Skip to content

Commit aea5f58

Browse files
Update refresh_project_copyrights.py
Iterate through each origin per component and refresh each, increase timeout to 60 seconds, added dry run option
1 parent 053a647 commit aea5f58

File tree

1 file changed

+66
-38
lines changed

1 file changed

+66
-38
lines changed

examples/client/refresh_project_copyrights.py

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from pprint import pprint
1515
import array as arr
1616

17+
from urllib3.exceptions import ReadTimeoutError
18+
1719
http.client._MAXHEADERS = 1000
1820

1921
logging.basicConfig(
@@ -52,8 +54,8 @@ def RepWarning(msg):
5254
parser.add_argument("--dryrun", dest='dry_run', type=int, default=0, help="Dry run test (0=no 1=yes)")
5355

5456
parser.add_argument("--no-verify", dest='verify', action='store_false', help="Disable TLS certificate verification")
55-
parser.add_argument("-t", "--timeout", default=15, type=int, help="Adjust the (HTTP) session timeout value (default: 15s)")
56-
parser.add_argument("-r", "--retries", default=3, type=int, help="Adjust the number of retries on failure (default: 3)")
57+
parser.add_argument("--timeout", default=60, type=int, help="Adjust the (HTTP) session timeout value (default: 60s)")
58+
parser.add_argument("--retries", default=3, type=int, help="Adjust the number of retries on failure (default: 3)")
5759

5860
args = parser.parse_args()
5961

@@ -64,8 +66,8 @@ def RepWarning(msg):
6466
# access the Black Duck platform
6567
bd = Client(
6668
base_url=args.base_url,
67-
token=access_token,
6869
verify=args.verify,
70+
token=access_token,
6971
timeout=args.timeout,
7072
retries=args.retries,
7173
)
@@ -205,8 +207,9 @@ def RepWarning(msg):
205207
break
206208

207209
my_statistics['_cntComponents'] += 1
208-
RepDebug(4, ' Component: %s (%s)' %
209-
(this_comp_data['componentName'], this_comp_data['componentVersionName']))
210+
comp_label = "{} ({})".format(this_comp_data['componentName'], this_comp_data['componentVersionName'])
211+
212+
RepDebug(4, ' Component: %s' % comp_label)
210213

211214
if this_comp_data['inputExternalIds'].__len__() > 0:
212215
inputExternalIds = this_comp_data['inputExternalIds'][0]
@@ -218,46 +221,69 @@ def RepWarning(msg):
218221

219222
# refresh the copyrights for this component
220223
if this_comp_data['origins'].__len__() > 0:
221-
url = this_comp_data['origins'][0]['origin']
222-
else:
223-
# no origins
224-
RepWarning('No origin defined for [%s]' % this_comp_data['componentVersion'])
225-
# url = this_comp_data['componentVersion']
226-
url = ''
227-
228-
if len(url) > 0:
229-
# refresh end point
230-
url += "/copyrights-refresh"
231-
232-
if args.dry_run != 0:
233-
RepDebug(1, "DryRun: %s" % url)
234-
else:
235-
try:
236-
response = bd.session.put(url, data=None, **refresh_kwargs)
237-
RepDebug(5,'Refresh response %s' % response)
238-
except ReadTimeoutError:
239-
print('Failed to confirm copyrights refresh')
240-
241-
my_statistics['_cntRefresh'] += 1
224+
225+
n_origin = 0
226+
227+
for this_origin in this_comp_data['origins']:
228+
229+
n_origin += 1
230+
origin_id = this_origin['externalId']
231+
url = this_origin['origin']
232+
233+
# refresh with end point
234+
url += "/copyrights-refresh"
235+
236+
status = -1
237+
238+
if args.dry_run != 0:
239+
RepDebug(1, "DryRun: no=%d origin=%s url=%s" % (n_origin, origin_id, url))
240+
else:
241+
try:
242+
response = bd.session.put(url, data=None, **refresh_kwargs)
243+
RepDebug(5,'Refresh response: origin [%s] [%s]' % (this_origin, response))
244+
my_statistics['_cntRefresh'] += 1
245+
status= 0
246+
247+
except Exception:
248+
print('Failed to confirm copyrights refresh')
249+
status = 1
250+
251+
252+
# if recording the data - perhaps outputting to a CSV file
253+
if args.dump_data:
254+
my_data = {}
255+
my_data['componentName'] = this_comp_data['componentName']
256+
my_data['componentVersion'] = this_comp_data['componentVersionName']
257+
my_data['status'] = status
258+
my_data['url'] = url
259+
260+
if hasattr(args, 'debug') and 5 <= args.debug:
261+
pprint(my_data)
262+
263+
# add to our list
264+
all_my_comp_data.append(my_data)
242265

243266
else:
267+
# no origins defined
268+
RepWarning('No origin(s) defined for [%s]' % comp_label)
244269
my_statistics['_cntNoOrigins'] += 1
270+
origin_id = ''
271+
status = 3
245272
url = 'n/a'
246273

274+
# if recording the data
275+
if args.dump_data:
276+
my_data = {}
277+
my_data['componentName'] = this_comp_data['componentName']
278+
my_data['componentVersion'] = this_comp_data['componentVersionName']
279+
my_data['status'] = status
280+
my_data['url'] = url
247281

248-
# if recording the data - perhaps outputting to a CSV file
249-
if args.dump_data:
250-
my_data = {}
251-
my_data['componentName'] = this_comp_data['componentName']
252-
my_data['componentVersion'] = this_comp_data['componentVersionName']
253-
my_data['url'] = url
254-
255-
if hasattr(args, 'debug') and 5 <= args.debug:
256-
pprint(my_data)
257-
258-
# add to our list
259-
all_my_comp_data.append(my_data)
282+
if hasattr(args, 'debug') and 5 <= args.debug:
283+
pprint(my_data)
260284

285+
# add to our list
286+
all_my_comp_data.append(my_data)
261287

262288
# end of processing loop
263289

@@ -280,6 +306,7 @@ def RepWarning(msg):
280306
field_names = [
281307
'Component',
282308
'Component Version',
309+
'Status',
283310
'Url'
284311
]
285312

@@ -290,6 +317,7 @@ def RepWarning(msg):
290317
row_data = {
291318
'Component': my_comp_data['componentName'],
292319
'Component Version': my_comp_data['componentVersion'],
320+
'Status': my_comp_data['status'],
293321
'Url': my_comp_data['url']
294322
}
295323
writer.writerow(row_data)

0 commit comments

Comments
 (0)