55#
66import http .client
77from sys import api_version
8+ import sys
89import csv
910import datetime
1011from blackduck import Client
@@ -26,6 +27,10 @@ def RepDebug(level, msg):
2627 return True
2728 return False
2829
30+ def RepWarning (msg ):
31+ print ("WARNING: " + msg )
32+ return True
33+
2934
3035# Parse command line arguments
3136parser = argparse .ArgumentParser ("Refresh copyrights for project/version components" )
@@ -46,14 +51,23 @@ def RepDebug(level, msg):
4651parser .add_argument ("--debug" , dest = 'debug' , type = int , default = 0 , help = "Debug verbosity (0=none)" )
4752
4853parser .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+
4957args = parser .parse_args ()
5058
5159# open the access token file
5260with open (args .token_file , 'r' ) as tf :
5361 access_token = tf .readline ().strip ()
5462
5563# 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+ )
5771
5872# initialise
5973all_my_comp_data = []
@@ -102,6 +116,9 @@ def RepDebug(level, msg):
102116my_statistics ['_cntVersions' ] = 0
103117my_statistics ['_cntComponents' ] = 0
104118my_statistics ['_cntRefresh' ] = 0
119+ my_statistics ['_cntNoOrigins' ] = 0
120+ my_statistics ['_cntNoIDs' ] = 0
121+
105122
106123# record any control values
107124if args .project_name :
@@ -187,25 +204,48 @@ def RepDebug(level, msg):
187204 break
188205
189206 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 )
195216
196- response = bd .session .put (url , data = None , ** refresh_kwargs )
197- RepDebug (5 ,'Refresh response %s' % response )
198217
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'
201241
202- my_statistics ['_cntRefresh' ] += 1
203242
204243 # if recording the data - perhaps outputting to a CSV file
205244 if args .dump_data :
206245 my_data = {}
207246 my_data ['componentName' ] = this_comp_data ['componentName' ]
208247 my_data ['componentVersion' ] = this_comp_data ['componentVersionName' ]
248+ my_data ['url' ] = url
209249
210250 if hasattr (args , 'debug' ) and 5 <= args .debug :
211251 pprint (my_data )
@@ -234,7 +274,8 @@ def RepDebug(level, msg):
234274 with open (args .csv_file , 'w' ) as csv_f :
235275 field_names = [
236276 'Component' ,
237- 'Component Version'
277+ 'Component Version' ,
278+ 'Url'
238279 ]
239280
240281 writer = csv .DictWriter (csv_f , fieldnames = field_names )
@@ -243,7 +284,8 @@ def RepDebug(level, msg):
243284 for my_comp_data in all_my_comp_data :
244285 row_data = {
245286 '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' ]
247289 }
248290 writer .writerow (row_data )
249291 else :
0 commit comments