Skip to content

Commit 1f27bf2

Browse files
author
Glenn Snyder
authored
Merge pull request #141 from blackducksoftware/shanko07/notices-report-recheck-condition
Update notices report generation to check for 412 unfinished report c…
2 parents 54b0d8f + 5977e56 commit 1f27bf2

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

examples/generate_notices_report_for_project_version.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
parser = argparse.ArgumentParser("A program to generate the notices file for a given project-version")
1919
parser.add_argument("project_name")
2020
parser.add_argument("version_name")
21-
22-
# TODO: Add the copyright checkbox option
23-
2421
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")
2522
parser.add_argument('-r', '--report_format', default='TEXT', choices=["JSON", "TEXT"], help="Report format - choices are TEXT or HTML")
2623
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,58 +29,67 @@
3229
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
3330

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

3736
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))
6470

6571
project = hub.get_project_by_name(args.project_name)
6672

6773
if project:
68-
version = hub.get_version_by_name(project, args.version_name)
74+
version = hub.get_version_by_name(project, args.version_name)
6975

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)
7177

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)
7783

7884

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.
8187

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))
8894
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

Comments
 (0)