Skip to content

Commit 5977e56

Browse files
author
Glenn Snyder
committed
inserting a list of error codes since the download error code on my v2021.2 server was different than the one in the PR
1 parent cf5243c commit 5977e56

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

examples/generate_notices_report_for_project_version.py

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,64 +29,67 @@
2929
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
3030

3131
class FailedReportDownload(Exception):
32-
pass
32+
pass
33+
34+
DOWNLOAD_ERROR_CODES = ['{report.main.read.unfinished.report}', '{report.main.download.unfinished.report}']
3335

3436
def download_report(location, file_name_base, retries=10):
35-
report_id = location.split("/")[-1]
36-
37-
if retries:
38-
logging.debug("Retrieving generated report from {}".format(location))
39-
# response = hub.download_report(report_id)
40-
response, report_format = hub.download_notification_report(location)
41-
if response.status_code == 200:
42-
if report_format == "TEXT":
43-
filename = file_name_base + ".zip"
44-
with open(filename, "wb") as f:
45-
f.write(response.content)
46-
else:
47-
# JSON format
48-
filename = file_name_base + ".json"
49-
with open(filename, "w") as f:
50-
json.dump(response.json(), f, indent=3)
51-
logging.info("Successfully downloaded json file to {} for report {}".format(
52-
filename, report_id))
53-
elif response.status_code == 412 and response.json()['errorCode'] == '{report.main.read.unfinished.report.contents}':
54-
# failed to download, and report generation still in progress, wait and try again infinitely
55-
# TODO: is it possible for things to get stuck in this forever?
56-
logging.warning(f"Failed to retrieve report {report_id} for reason {response.json()['errorCode']}. Waiting 5 seconds then trying infinitely")
57-
time.sleep(5)
58-
download_report(location, file_name_base, retries)
59-
else:
60-
logging.warning("Failed to retrieve report {}".format(report_id))
61-
logging.warning("Probably not ready yet, waiting 5 seconds then retrying (remaining retries={}".format(retries))
62-
time.sleep(5)
63-
retries -= 1
64-
download_report(location, file_name_base, retries)
65-
else:
66-
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))
6770

6871
project = hub.get_project_by_name(args.project_name)
6972

7073
if project:
71-
version = hub.get_version_by_name(project, args.version_name)
74+
version = hub.get_version_by_name(project, args.version_name)
7275

73-
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)
7477

75-
if response.status_code == 201:
76-
logging.info("Successfully created notices report in {} format for project {} and version {}".format(
77-
args.report_format, args.project_name, args.version_name))
78-
location = response.headers['Location']
79-
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)
8083

8184

82-
# Showing how you can interact with the downloaded zip and where to find the
83-
# 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.
8487

85-
# with zipfile.ZipFile(zip_file_name_base, 'r') as zipf:
86-
# with zipf.open("{}/{}/version-license.txt".format(args.project_name, args.version_name), "r") as license_file:
87-
# print(license_file.read())
88-
else:
89-
logging.error("Failed to create reports for project {} version {}, status code returned {}".format(
90-
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))
9194
else:
92-
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

Comments
 (0)