5
5
#
6
6
import http .client
7
7
from sys import api_version
8
+ import sys
8
9
import csv
9
10
import datetime
10
11
from blackduck import Client
@@ -26,6 +27,10 @@ def RepDebug(level, msg):
26
27
return True
27
28
return False
28
29
30
+ def RepWarning (msg ):
31
+ print ("WARNING: " + msg )
32
+ return True
33
+
29
34
30
35
# Parse command line arguments
31
36
parser = argparse .ArgumentParser ("Refresh copyrights for project/version components" )
@@ -46,14 +51,23 @@ def RepDebug(level, msg):
46
51
parser .add_argument ("--debug" , dest = 'debug' , type = int , default = 0 , help = "Debug verbosity (0=none)" )
47
52
48
53
parser .add_argument ("--no-verify" , dest = 'verify' , action = 'store_false' , help = "Disable TLS certificate verification" )
54
+ parser .add_argument ("-t" , "--timeout" , default = 15 , type = int , help = "Adjust the (HTTP) session timeout value (default: 15s)" )
55
+ parser .add_argument ("-r" , "--retries" , default = 3 , type = int , help = "Adjust the number of retries on failure (default: 3)" )
56
+
49
57
args = parser .parse_args ()
50
58
51
59
# open the access token file
52
60
with open (args .token_file , 'r' ) as tf :
53
61
access_token = tf .readline ().strip ()
54
62
55
63
# access the Black Duck platform
56
- bd = Client (base_url = args .base_url , token = access_token , verify = args .verify )
64
+ bd = Client (
65
+ base_url = args .base_url ,
66
+ token = access_token ,
67
+ verify = args .verify ,
68
+ timeout = args .timeout ,
69
+ retries = args .retries ,
70
+ )
57
71
58
72
# initialise
59
73
all_my_comp_data = []
@@ -102,6 +116,9 @@ def RepDebug(level, msg):
102
116
my_statistics ['_cntVersions' ] = 0
103
117
my_statistics ['_cntComponents' ] = 0
104
118
my_statistics ['_cntRefresh' ] = 0
119
+ my_statistics ['_cntNoOrigins' ] = 0
120
+ my_statistics ['_cntNoIDs' ] = 0
121
+
105
122
106
123
# record any control values
107
124
if args .project_name :
@@ -187,25 +204,48 @@ def RepDebug(level, msg):
187
204
break
188
205
189
206
my_statistics ['_cntComponents' ] += 1
190
- RepDebug (4 , ' Component: %s' % this_comp_data ['componentName' ])
191
-
192
- # refresh the copyrights for this component
193
- url = this_comp_data ['origins' ][0 ]['origin' ]
194
- url += "/copyrights-refresh"
207
+ RepDebug (4 , ' Component: %s (%s)' %
208
+ (this_comp_data ['componentName' ], this_comp_data ['componentVersionName' ]))
209
+
210
+ if this_comp_data ['inputExternalIds' ].__len__ () > 0 :
211
+ inputExternalIds = this_comp_data ['inputExternalIds' ][0 ]
212
+ else :
213
+ my_statistics ['_cntNoIDs' ] += 1
214
+ inputExternalIds = "n/a"
215
+ RepDebug (2 , ' ID: %s' % inputExternalIds )
195
216
196
- response = bd .session .put (url , data = None , ** refresh_kwargs )
197
- RepDebug (5 ,'Refresh response %s' % response )
198
217
199
- inputExternalIds = this_comp_data ['inputExternalIds' ][0 ]
200
- RepDebug (2 , ' ID: %s' % inputExternalIds )
218
+ # refresh the copyrights for this component
219
+ if this_comp_data ['origins' ].__len__ () > 0 :
220
+ url = this_comp_data ['origins' ][0 ]['origin' ]
221
+ else :
222
+ # no origins
223
+ RepWarning ('No origin defined for [%s]' % this_comp_data ['componentVersion' ])
224
+ # url = this_comp_data['componentVersion']
225
+ url = ''
226
+
227
+ if len (url ) > 0 :
228
+ # refresh end point
229
+ url += "/copyrights-refresh"
230
+
231
+ try :
232
+ response = bd .session .put (url , data = None , ** refresh_kwargs )
233
+ RepDebug (5 ,'Refresh response %s' % response )
234
+ except urllib3 .exceptions .ReadTimeoutError :
235
+ print ('Failed to confirm copyrights refresh' )
236
+
237
+ my_statistics ['_cntRefresh' ] += 1
238
+ else :
239
+ my_statistics ['_cntNoOrigins' ] += 1
240
+ url = 'n/a'
201
241
202
- my_statistics ['_cntRefresh' ] += 1
203
242
204
243
# if recording the data - perhaps outputting to a CSV file
205
244
if args .dump_data :
206
245
my_data = {}
207
246
my_data ['componentName' ] = this_comp_data ['componentName' ]
208
247
my_data ['componentVersion' ] = this_comp_data ['componentVersionName' ]
248
+ my_data ['url' ] = url
209
249
210
250
if hasattr (args , 'debug' ) and 5 <= args .debug :
211
251
pprint (my_data )
@@ -234,7 +274,8 @@ def RepDebug(level, msg):
234
274
with open (args .csv_file , 'w' ) as csv_f :
235
275
field_names = [
236
276
'Component' ,
237
- 'Component Version'
277
+ 'Component Version' ,
278
+ 'Url'
238
279
]
239
280
240
281
writer = csv .DictWriter (csv_f , fieldnames = field_names )
@@ -243,7 +284,8 @@ def RepDebug(level, msg):
243
284
for my_comp_data in all_my_comp_data :
244
285
row_data = {
245
286
'Component' : my_comp_data ['componentName' ],
246
- 'Component Version' : my_comp_data ['componentVersion' ]
287
+ 'Component Version' : my_comp_data ['componentVersion' ],
288
+ 'Url' : my_comp_data ['url' ]
247
289
}
248
290
writer .writerow (row_data )
249
291
else :
0 commit comments