14
14
from pprint import pprint
15
15
import array as arr
16
16
17
+ from urllib3 .exceptions import ReadTimeoutError
18
+
17
19
http .client ._MAXHEADERS = 1000
18
20
19
21
logging .basicConfig (
@@ -44,15 +46,18 @@ def RepWarning(msg):
44
46
parser .add_argument ("--project" , dest = 'project_name' , help = "Project name" )
45
47
parser .add_argument ("--version" , dest = 'version_name' , help = "Version name" )
46
48
47
- parser .add_argument ("--max-projects" , dest = 'max_projects' , type = int , help = "Maximum projects to inspect else all" )
49
+ parser .add_argument ("--max-projects" , dest = 'max_projects' , type = int , help = "Maximum number of projects to inspect else all" )
48
50
parser .add_argument ("--max-versions-per-project" , dest = 'max_versions_per_project' , type = int , help = "Maximum versions per project to inspect else all" )
49
51
parser .add_argument ("--max-components" , dest = 'max_components' , type = int , help = "Maximum components to inspect in total else all" )
50
52
51
- parser .add_argument ("--debug" , dest = 'debug' , type = int , default = 0 , help = "Debug verbosity (0=none)" )
53
+ parser .add_argument ("--skip-projects" , dest = 'skip_projects' , type = int , help = "Skip first 'n' projects to inspect" )
54
+
55
+ parser .add_argument ("--debug" , dest = 'debug' , type = int , default = 0 , help = "Debug verbosity (0=none 'n'=level)" )
56
+ parser .add_argument ("--dryrun" , dest = 'dry_run' , type = int , default = 0 , help = "Dry run test (0=no 1=yes)" )
52
57
53
58
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)" )
59
+ parser .add_argument ("-- timeout" , default = 60 , type = int , help = "Adjust the (HTTP) session timeout value (default: 60s )" )
60
+ parser .add_argument ("--retries" , default = 3 , type = int , help = "Adjust the number of retries on failure (default: 3)" )
56
61
57
62
args = parser .parse_args ()
58
63
@@ -63,8 +68,8 @@ def RepWarning(msg):
63
68
# access the Black Duck platform
64
69
bd = Client (
65
70
base_url = args .base_url ,
66
- token = access_token ,
67
71
verify = args .verify ,
72
+ token = access_token ,
68
73
timeout = args .timeout ,
69
74
retries = args .retries ,
70
75
)
@@ -73,6 +78,7 @@ def RepWarning(msg):
73
78
all_my_comp_data = []
74
79
my_statistics = {}
75
80
81
+ str_unknown = "n/a"
76
82
77
83
# version of components API to call
78
84
comp_api_version = 6
@@ -118,7 +124,7 @@ def RepWarning(msg):
118
124
my_statistics ['_cntRefresh' ] = 0
119
125
my_statistics ['_cntNoOrigins' ] = 0
120
126
my_statistics ['_cntNoIDs' ] = 0
121
-
127
+ my_statistics [ '_cntSkippedProjects' ] = 0
122
128
123
129
# record any control values
124
130
if args .project_name :
@@ -149,18 +155,32 @@ def RepWarning(msg):
149
155
# all projects are in scope
150
156
projects = bd .get_resource ('projects' )
151
157
158
+
159
+ cnt_projects = 0
160
+
152
161
# loop through projects list
153
162
for this_project in projects :
154
163
164
+ cnt_projects += 1
165
+
166
+ # check if we are skipping over this project
167
+ if args .skip_projects and cnt_projects <= args .skip_projects :
168
+ my_statistics ['_cntSkippedProjects' ] += 1
169
+ RepDebug (1 , 'Skipping project [%d] [%s]' % (cnt_projects , this_project ['name' ]))
170
+ continue
171
+
155
172
# check if we have hit any limit
156
173
if args .max_components and my_statistics ['_cntComponents' ] >= args .max_components :
174
+ RepDebug (1 , 'Reached component limit [%d]' % args .max_components )
157
175
break
158
176
159
177
if args .max_projects and my_statistics ['_cntProjects' ] >= args .max_projects :
178
+ RepDebug (1 , 'Reached project limit [%d]' % args .max_projects )
160
179
break
161
180
181
+ # process this project
162
182
my_statistics ['_cntProjects' ] += 1
163
- RepDebug (1 , '## Project %d: %s ' % (my_statistics [ '_cntProjects' ] , this_project ['name' ]))
183
+ RepDebug (1 , '## Project: [%d] [%s] ' % (cnt_projects , this_project ['name' ]))
164
184
165
185
if args .version_name :
166
186
# note the specific project version of interest
@@ -181,78 +201,121 @@ def RepWarning(msg):
181
201
182
202
# check if we have hit any limit
183
203
if args .max_components and my_statistics ['_cntComponents' ] >= args .max_components :
184
- # exit component loop - at the limit
204
+ RepDebug ( 1 , 'Reached component limit [%d]' % args . max_components )
185
205
break
186
206
187
207
if args .max_versions_per_project and nVersionsPerProject >= args .max_versions_per_project :
188
- # exit loop - at the version per project limit
208
+ RepDebug ( 1 , 'Reached versions per project limit [%d]' % args . max_versions_per_project )
189
209
break
190
210
191
211
nVersionsPerProject += 1
192
212
my_statistics ['_cntVersions' ] += 1
193
213
194
214
# Announce
195
215
# logging.debug(f"Found {this_project['name']}:{this_version['versionName']}")
196
- RepDebug (3 , ' Version: %s ' % this_version ['versionName' ])
216
+ RepDebug (3 , ' Version: [%s] ' % this_version ['versionName' ])
197
217
198
218
199
219
# iterate through all components for this project version
200
220
for this_comp_data in bd .get_resource ('components' , this_version , ** comp_kwargs ):
201
221
202
222
if args .max_components and my_statistics ['_cntComponents' ] >= args .max_components :
203
- # exit component loop - at the limit
223
+ RepDebug ( 1 , 'Reached component limit [%d]' % args . max_components )
204
224
break
205
225
206
226
my_statistics ['_cntComponents' ] += 1
207
- RepDebug (4 , ' Component: %s (%s)' %
208
- (this_comp_data ['componentName' ], this_comp_data ['componentVersionName' ]))
227
+
228
+ if this_comp_data .get ("componentName" ):
229
+ comp_name = this_comp_data ['componentName' ]
230
+ else :
231
+ comp_name = str_unknown
232
+
233
+ if this_comp_data .get ("componentVersionName" ):
234
+ comp_version_name = this_comp_data ['componentVersionName' ]
235
+ else :
236
+ comp_version_name = str_unknown
237
+
238
+ comp_label = "{} ({})" .format (comp_name , comp_version_name )
239
+
240
+ RepDebug (4 , ' Component: [%s]' % comp_label )
209
241
210
242
if this_comp_data ['inputExternalIds' ].__len__ () > 0 :
211
243
inputExternalIds = this_comp_data ['inputExternalIds' ][0 ]
212
244
else :
213
245
my_statistics ['_cntNoIDs' ] += 1
214
- inputExternalIds = "n/a"
215
- RepDebug (2 , ' ID: %s ' % inputExternalIds )
246
+ inputExternalIds = str_unknown
247
+ RepDebug (2 , ' ID: [%s] ' % inputExternalIds )
216
248
217
249
218
- # refresh the copyrights for this component
250
+ # refresh the copyrights for this component-origin
219
251
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
252
+
253
+ n_origin = 0
254
+
255
+ for this_origin in this_comp_data ['origins' ]:
256
+
257
+ n_origin += 1
258
+ if this_origin .get ('externalId' ):
259
+ origin_id = this_origin ['externalId' ]
260
+ else :
261
+ origin_id = str_unknown
262
+
263
+ url = this_origin ['origin' ]
264
+
265
+ # refresh with end point
266
+ url += "/copyrights-refresh"
267
+
268
+ status = - 1
269
+
270
+ if args .dry_run != 0 :
271
+ RepDebug (2 , "DryRun: origin - no [%d] id [%s] url [%s]" % (n_origin , origin_id , url ))
272
+ else :
273
+ try :
274
+ response = bd .session .put (url , data = None , ** refresh_kwargs )
275
+ RepDebug (5 ,'Refresh response: origin [%s] [%s]' % (this_origin , response ))
276
+ my_statistics ['_cntRefresh' ] += 1
277
+ status = 0
278
+
279
+ except Exception :
280
+ print ('Failed to confirm copyrights refresh' )
281
+ status = 1
282
+
283
+
284
+ # if recording the data - perhaps outputting to a CSV file
285
+ if args .dump_data :
286
+ my_data = {}
287
+ my_data ['componentName' ] = this_comp_data ['componentName' ]
288
+ my_data ['componentVersion' ] = this_comp_data ['componentVersionName' ]
289
+ my_data ['status' ] = status
290
+ my_data ['url' ] = url
291
+
292
+ if hasattr (args , 'debug' ) and 5 <= args .debug :
293
+ pprint (my_data )
294
+
295
+ # add to our list
296
+ all_my_comp_data .append (my_data )
297
+
238
298
else :
299
+ # no origins defined
300
+ RepWarning ('No origin(s) defined for [%s]' % comp_label )
239
301
my_statistics ['_cntNoOrigins' ] += 1
302
+ origin_id = ''
303
+ status = 3
240
304
url = 'n/a'
241
305
306
+ # if recording the data
307
+ if args .dump_data :
308
+ my_data = {}
309
+ my_data ['componentName' ] = comp_name
310
+ my_data ['componentVersion' ] = comp_version_name
311
+ my_data ['status' ] = status
312
+ my_data ['url' ] = url
242
313
243
- # if recording the data - perhaps outputting to a CSV file
244
- if args .dump_data :
245
- my_data = {}
246
- my_data ['componentName' ] = this_comp_data ['componentName' ]
247
- my_data ['componentVersion' ] = this_comp_data ['componentVersionName' ]
248
- my_data ['url' ] = url
249
-
250
- if hasattr (args , 'debug' ) and 5 <= args .debug :
251
- pprint (my_data )
252
-
253
- # add to our list
254
- all_my_comp_data .append (my_data )
314
+ if hasattr (args , 'debug' ) and 5 <= args .debug :
315
+ pprint (my_data )
255
316
317
+ # add to our list
318
+ all_my_comp_data .append (my_data )
256
319
257
320
# end of processing loop
258
321
@@ -275,6 +338,7 @@ def RepWarning(msg):
275
338
field_names = [
276
339
'Component' ,
277
340
'Component Version' ,
341
+ 'Status' ,
278
342
'Url'
279
343
]
280
344
@@ -285,6 +349,7 @@ def RepWarning(msg):
285
349
row_data = {
286
350
'Component' : my_comp_data ['componentName' ],
287
351
'Component Version' : my_comp_data ['componentVersion' ],
352
+ 'Status' : my_comp_data ['status' ],
288
353
'Url' : my_comp_data ['url' ]
289
354
}
290
355
writer .writerow (row_data )
0 commit comments