1
+ #!/usr/bin/env python
2
+
3
+ from blackduck .HubRestApi import HubInstance
4
+
5
+ import argparse
6
+ import json
7
+
8
+ parser = argparse .ArgumentParser ()
9
+ parser .add_argument ("project_name" )
10
+ parser .add_argument ("version_name" )
11
+ args = parser .parse_args ()
12
+
13
+ hub = HubInstance ()
14
+
15
+ project = hub .get_project_by_name (args .project_name )
16
+ if project :
17
+ version = hub .get_version_by_name (project , args .version_name )
18
+
19
+ if version :
20
+ codelocation_url = hub .get_link (version , "codelocations" )
21
+ codelocation_url += "?limit={}" .format (1000000 )
22
+ response = hub .execute_get (codelocation_url )
23
+ if response .status_code == 200 :
24
+ # codelocation and scan are synonymous
25
+ codelocation_info = response .json ().get ('items' , [])
26
+ if codelocation_info :
27
+ most_recent_scan = max ([cl ['updatedAt' ] for cl in codelocation_info ])
28
+ oldest_scan = min ([cl ['createdAt' ] for cl in codelocation_info ])
29
+ number_scans = len (codelocation_info )
30
+ else :
31
+ number_scans = 0
32
+ oldest_scan = most_recent_scan = "Not applicable"
33
+
34
+ resultString = json .dumps (
35
+ {
36
+ 'scans' : codelocation_info ,
37
+ 'number_scans' : number_scans ,
38
+ 'most_recent_scan' : most_recent_scan ,
39
+ 'oldest_scan' : oldest_scan
40
+ })
41
+ resultArray = json .loads (resultString )
42
+ print (f"Number of scan performed { resultArray .get ('number_scans' )} " )
43
+ scansArray = resultArray .get ('scans' )
44
+ sizeTrack = 0
45
+ for i , scan in enumerate (scansArray ):
46
+ print (f"Scan { i + 1 } name:{ scan ['name' ]} " )
47
+ sizeinbytes = scan ['scanSize' ]
48
+ size = sizeinbytes / 1024 / 1024
49
+ sizeTrack = sizeTrack + sizeinbytes
50
+ print (f" size:{ '{0:.2f}' .format (size )} MB" )
51
+
52
+ totalsize = sizeTrack / 1024 / 1024
53
+ print ("===============================" )
54
+ print (f"Project Total size:{ '{0:.2f}' .format (totalsize )} MB" )
55
+ print ("===============================" )
56
+
57
+ # print(json.dumps(
58
+ # {
59
+ # 'scans': codelocation_info,
60
+ # 'number_scans': number_scans,
61
+ # 'most_recent_scan': most_recent_scan,
62
+ # 'oldest_scan': oldest_scan
63
+ # }))
64
+ else :
65
+ print ("Failed to retrieve the codelocation (aka scan) info, response code was {}" .format (response .status_code ))
66
+ else :
67
+ print ("Could not find the version {} in project {}" .format (args .version_name , args .project_name ))
68
+ else :
69
+ print ("Could not find the project {}" .format (args .project_name ))
0 commit comments