10
10
11
11
import argparse
12
12
import json
13
+ import logging
14
+ import sys
13
15
import time
14
16
import zipfile
15
17
16
18
parser = argparse .ArgumentParser ("A program to generate the notices file for a given project-version" )
17
19
parser .add_argument ("project_name" )
18
20
parser .add_argument ("version_name" )
19
- parser .add_argument ("--zip_file_name" , default = "notices_report.zip" )
20
- parser .add_argument ("--reports" ,
21
- default = "version,scans,components,vulnerabilities,source" ,
22
- help = "Comma separated list (no spaces) of the reports to generate - version, scans, components, vulnerabilities, source, and cryptography reports (default: all, except cryptography" )
23
- parser .add_argument ('--format' , default = 'TEXT' , choices = ["HTML" , "TEXT" ], help = "Report format - choices are TEXT or HTML" )
21
+ parser .add_argument ('-f' , "--file_name_base" , default = "notices_report" , help = "Base file name to write the report data into. If the report format is TEXT a .zip file will be created, otherwise a .json file" )
22
+ parser .add_argument ('-r' , '--report_format' , default = 'TEXT' , choices = ["JSON" , "TEXT" ], help = "Report format - choices are TEXT or HTML" )
24
23
25
24
args = parser .parse_args ()
26
25
27
26
hub = HubInstance ()
28
27
29
- # TODO: Promote this to the API?
28
+ logging .basicConfig (format = '%(asctime)s:%(levelname)s:%(message)s' , stream = sys .stderr , level = logging .DEBUG )
29
+
30
30
class FailedReportDownload (Exception ):
31
31
pass
32
32
33
- def download_report (location , filename , retries = 4 ):
33
+ def download_report (location , file_name_base , retries = 10 ):
34
34
report_id = location .split ("/" )[- 1 ]
35
35
36
36
if retries :
37
- print ("Retrieving generated report from {}" .format (location ))
38
- response = hub .download_report (report_id )
37
+ logging .debug ("Retrieving generated report from {}" .format (location ))
38
+ # response = hub.download_report(report_id)
39
+ response , report_format = hub .download_notification_report (location )
39
40
if response .status_code == 200 :
40
- with open (filename , "wb" ) as f :
41
- f .write (response .content )
42
- print ("Successfully downloaded zip file to {} for report {}" .format (filename , report_id ))
41
+ if report_format == "TEXT" :
42
+ filename = file_name_base + ".zip"
43
+ with open (filename , "wb" ) as f :
44
+ f .write (response .content )
45
+ else :
46
+ # JSON format
47
+ filename = file_name_base + ".json"
48
+ with open (filename , "w" ) as f :
49
+ json .dump (response .json (), f , indent = 3 )
50
+ logging .info ("Successfully downloaded json file to {} for report {}" .format (
51
+ filename , report_id ))
43
52
else :
44
- print ("Failed to retrieve report {}" .format (report_id ))
45
- print ("Probably not ready yet, waiting 5 seconds then retrying..." )
53
+ logging . warning ("Failed to retrieve report {}" .format (report_id ))
54
+ logging . warning ("Probably not ready yet, waiting 5 seconds then retrying (remaining retries={}" . format ( retries ) )
46
55
time .sleep (5 )
47
56
retries -= 1
48
- download_report (location , filename , retries )
57
+ download_report (location , file_name_base , retries )
49
58
else :
50
59
raise FailedReportDownload ("Failed to retrieve report {} after multiple retries" .format (report_id ))
51
60
@@ -54,22 +63,23 @@ def download_report(location, filename, retries=4):
54
63
if project :
55
64
version = hub .get_version_by_name (project , args .version_name )
56
65
57
- response = hub .create_version_notices_report (version , args .format )
66
+ response = hub .create_version_notices_report (version , args .report_format )
58
67
59
68
if response .status_code == 201 :
60
- print ("Successfully created reports ({}) for project {} and version {}" .format (
61
- args .reports , args .project_name , args .version_name ))
69
+ logging . info ("Successfully created notices report in {} format for project {} and version {}" .format (
70
+ args .report_format , args .project_name , args .version_name ))
62
71
location = response .headers ['Location' ]
63
- download_report (location , args .zip_file_name )
72
+ download_report (location , args .file_name_base )
73
+
64
74
65
75
# Showing how you can interact with the downloaded zip and where to find the
66
76
# output content. Uncomment the lines below to see how it works.
67
77
68
- # with zipfile.ZipFile(zip_file_name , 'r') as zipf:
78
+ # with zipfile.ZipFile(zip_file_name_base , 'r') as zipf:
69
79
# with zipf.open("{}/{}/version-license.txt".format(args.project_name, args.version_name), "r") as license_file:
70
80
# print(license_file.read())
71
81
else :
72
- print ("Failed to create reports for project {} version {}, status code returned {}" .format (
82
+ logging . error ("Failed to create reports for project {} version {}, status code returned {}" .format (
73
83
args .project_name , args .version_name , response .status_code ))
74
84
else :
75
- print ("Did not find project with name {}" .format (args .project_name ))
85
+ logging . warning ("Did not find project with name {}" .format (args .project_name ))
0 commit comments