18
18
parser = argparse .ArgumentParser ("A program to generate the notices file for a given project-version" )
19
19
parser .add_argument ("project_name" )
20
20
parser .add_argument ("version_name" )
21
-
22
- # TODO: Add the copyright checkbox option
23
-
24
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" )
25
22
parser .add_argument ('-r' , '--report_format' , default = 'TEXT' , choices = ["JSON" , "TEXT" ], help = "Report format - choices are TEXT or HTML" )
26
23
parser .add_argument ('-c' , '--include_copyright_info' , action = 'store_true' , help = "Set this option to have additional copyright information from the Black Duck KB included in the notices file report." )
32
29
logging .basicConfig (format = '%(asctime)s:%(levelname)s:%(message)s' , stream = sys .stderr , level = logging .DEBUG )
33
30
34
31
class FailedReportDownload (Exception ):
35
- pass
32
+ pass
33
+
34
+ DOWNLOAD_ERROR_CODES = ['{report.main.read.unfinished.report}' , '{report.main.download.unfinished.report}' ]
36
35
37
36
def download_report (location , file_name_base , retries = 10 ):
38
- report_id = location .split ("/" )[- 1 ]
39
-
40
- if retries :
41
- logging .debug ("Retrieving generated report from {}" .format (location ))
42
- # response = hub.download_report(report_id)
43
- response , report_format = hub .download_notification_report (location )
44
- if response .status_code == 200 :
45
- if report_format == "TEXT" :
46
- filename = file_name_base + ".zip"
47
- with open (filename , "wb" ) as f :
48
- f .write (response .content )
49
- else :
50
- # JSON format
51
- filename = file_name_base + ".json"
52
- with open (filename , "w" ) as f :
53
- json .dump (response .json (), f , indent = 3 )
54
- logging .info ("Successfully downloaded json file to {} for report {}" .format (
55
- filename , report_id ))
56
- else :
57
- logging .warning ("Failed to retrieve report {}" .format (report_id ))
58
- logging .warning ("Probably not ready yet, waiting 5 seconds then retrying (remaining retries={}" .format (retries ))
59
- time .sleep (5 )
60
- retries -= 1
61
- download_report (location , file_name_base , retries )
62
- else :
63
- raise FailedReportDownload ("Failed to retrieve report {} after multiple retries" .format (report_id ))
37
+ report_id = location .split ("/" )[- 1 ]
38
+
39
+ if retries :
40
+ logging .debug ("Retrieving generated report from {}" .format (location ))
41
+ # response = hub.download_report(report_id)
42
+ response , report_format = hub .download_notification_report (location )
43
+
44
+ if response .status_code == 200 :
45
+ if report_format == "TEXT" :
46
+ filename = file_name_base + ".zip"
47
+ with open (filename , "wb" ) as f :
48
+ f .write (response .content )
49
+ else :
50
+ # JSON format
51
+ filename = file_name_base + ".json"
52
+ with open (filename , "w" ) as f :
53
+ json .dump (response .json (), f , indent = 3 )
54
+ logging .info ("Successfully downloaded json file to {} for report {}" .format (
55
+ filename , report_id ))
56
+ elif response .status_code == 412 and response .json ()['errorCode' ] in DOWNLOAD_ERROR_CODES :
57
+ # failed to download, and report generation still in progress, wait and try again infinitely
58
+ # TODO: is it possible for things to get stuck in this forever?
59
+ logging .warning (f"Failed to retrieve report { report_id } for reason { response .json ()['errorCode' ]} . Waiting 5 seconds then trying infinitely" )
60
+ time .sleep (5 )
61
+ download_report (location , file_name_base , retries )
62
+ else :
63
+ logging .warning (f"Failed to retrieve report, status code { response .status_code } " )
64
+ logging .warning ("Probably not ready yet, waiting 5 seconds then retrying (remaining retries={}" .format (retries ))
65
+ time .sleep (5 )
66
+ retries -= 1
67
+ download_report (location , file_name_base , retries )
68
+ else :
69
+ raise FailedReportDownload ("Failed to retrieve report {} after multiple retries" .format (report_id ))
64
70
65
71
project = hub .get_project_by_name (args .project_name )
66
72
67
73
if project :
68
- version = hub .get_version_by_name (project , args .version_name )
74
+ version = hub .get_version_by_name (project , args .version_name )
69
75
70
- response = hub .create_version_notices_report (version , args .report_format , include_copyright_info = args .include_copyright_info )
76
+ response = hub .create_version_notices_report (version , args .report_format , include_copyright_info = args .include_copyright_info )
71
77
72
- if response .status_code == 201 :
73
- logging .info ("Successfully created notices report in {} format for project {} and version {}" .format (
74
- args .report_format , args .project_name , args .version_name ))
75
- location = response .headers ['Location' ]
76
- download_report (location , args .file_name_base )
78
+ if response .status_code == 201 :
79
+ logging .info ("Successfully created notices report in {} format for project {} and version {}" .format (
80
+ args .report_format , args .project_name , args .version_name ))
81
+ location = response .headers ['Location' ]
82
+ download_report (location , args .file_name_base )
77
83
78
84
79
- # Showing how you can interact with the downloaded zip and where to find the
80
- # output content. Uncomment the lines below to see how it works.
85
+ # Showing how you can interact with the downloaded zip and where to find the
86
+ # output content. Uncomment the lines below to see how it works.
81
87
82
- # with zipfile.ZipFile(zip_file_name_base, 'r') as zipf:
83
- # with zipf.open("{}/{}/version-license.txt".format(args.project_name, args.version_name), "r") as license_file:
84
- # print(license_file.read())
85
- else :
86
- logging .error ("Failed to create reports for project {} version {}, status code returned {}" .format (
87
- args .project_name , args .version_name , response .status_code ))
88
+ # with zipfile.ZipFile(zip_file_name_base, 'r') as zipf:
89
+ # with zipf.open("{}/{}/version-license.txt".format(args.project_name, args.version_name), "r") as license_file:
90
+ # print(license_file.read())
91
+ else :
92
+ logging .error ("Failed to create reports for project {} version {}, status code returned {}" .format (
93
+ args .project_name , args .version_name , response .status_code ))
88
94
else :
89
- logging .warning ("Did not find project with name {}" .format (args .project_name ))
95
+ logging .warning ("Did not find project with name {}" .format (args .project_name ))
0 commit comments