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+ response = hub .execute_get (codelocation_url )
22+ if response .status_code == 200 :
23+ # codelocation and scan are synonymous
24+ codelocation_info = response .json ().get ('items' , [])
25+ if codelocation_info :
26+ most_recent_scan = max ([cl ['updatedAt' ] for cl in codelocation_info ])
27+ oldest_scan = min ([cl ['createdAt' ] for cl in codelocation_info ])
28+ number_scans = len (codelocation_info )
29+ else :
30+ number_scans = 0
31+ oldest_scan = most_recent_scan = "Not applicable"
32+
33+ resultString = json .dumps (
34+ {
35+ 'scans' : codelocation_info ,
36+ 'number_scans' : number_scans ,
37+ 'most_recent_scan' : most_recent_scan ,
38+ 'oldest_scan' : oldest_scan
39+ })
40+ resultArray = json .loads (resultString )
41+ print (f"Number of scan performed { resultArray .get ('number_scans' )} " )
42+ scansArray = resultArray .get ('scans' )
43+ sizeTrack = 0
44+ for i , scan in enumerate (scansArray ):
45+ print (f"Scan { i + 1 } name:{ scan ['name' ]} " )
46+ sizeinbytes = scan ['scanSize' ]
47+ size = sizeinbytes / 1024 / 1024
48+ sizeTrack = sizeTrack + sizeinbytes
49+ print (f" size:{ '{0:.2f}' .format (size )} MB" )
50+
51+ totalsize = sizeTrack / 1024 / 1024
52+ print (f"Project Total size:{ '{0:.2f}' .format (totalsize )} MB" )
53+
54+
55+ # print(json.dumps(
56+ # {
57+ # 'scans': codelocation_info,
58+ # 'number_scans': number_scans,
59+ # 'most_recent_scan': most_recent_scan,
60+ # 'oldest_scan': oldest_scan
61+ # }))
62+ else :
63+ print ("Failed to retrieve the codelocation (aka scan) info, response code was {}" .format (response .status_code ))
64+ else :
65+ print ("Could not find the version {} in project {}" .format (args .version_name , args .project_name ))
66+ else :
67+ print ("Could not find the project {}" .format (args .project_name ))
0 commit comments