|
16 | 16 | import logging
|
17 | 17 | import sys
|
18 | 18 | import time
|
| 19 | +from pprint import pprint |
19 | 20 |
|
20 | 21 | logging.basicConfig(
|
21 | 22 | level=logging.DEBUG,
|
@@ -52,34 +53,39 @@ class FailedReportDownload(Exception):
|
52 | 53 | default=",".join(all_reports),
|
53 | 54 | help=f"Comma separated list (no spaces) of the reports to generate - {list(version_name_map.keys())}. Default is all reports.")
|
54 | 55 | parser.add_argument('--format', default='CSV', choices=["CSV"], help="Report format - only CSV available for now")
|
55 |
| -parser.add_argument('-t', '--tries', default=4, type=int, help="How many times to retry downloading the report, i.e. wait for the report to be generated") |
56 |
| -parser.add_argument('-s', '--sleep_time', default=5, type=int, help="The amount of time to sleep in-between (re-)tries to download the report") |
| 56 | +parser.add_argument('-t', '--tries', default=30, type=int, help="How many times to retry downloading the report, i.e. wait for the report to be generated") |
| 57 | +parser.add_argument('-s', '--sleep_time', default=10, type=int, help="The amount of time to sleep in-between (re-)tries to download the report") |
57 | 58 | parser.add_argument('--no-verify', dest='verify', action='store_false', help="disable TLS certificate verification")
|
58 | 59 |
|
59 | 60 | args = parser.parse_args()
|
60 | 61 |
|
61 | 62 | def download_report(bd_client, location, filename, retries=args.tries):
|
62 | 63 | report_id = location.split("/")[-1]
|
63 |
| - download_url = f"{bd_client.base_url}api/reports/{report_id}" |
| 64 | + base_url = bd_client.base_url if bd_client.base_url.endswith("/") else bd_client.base_url + "/" |
| 65 | + download_url = f"{base_url}api/reports/{report_id}" |
| 66 | + |
| 67 | + logging.info(f"Retrieving report list for {location}") |
64 | 68 |
|
65 | 69 | if retries:
|
66 |
| - print("Retrieving generated report from {}".format(location)) |
67 |
| - response = bd.session.get(download_url, headers={'Content-Type': 'application/zip', 'Accept':'application/zip'}) |
68 |
| - if response.status_code == 200: |
69 |
| - with open(filename, "wb") as f: |
70 |
| - f.write(response.content) |
71 |
| - print(f"Successfully downloaded zip file to {filename} for report {report_id}") |
72 |
| - else: |
73 |
| - print(f"Failed to retrieve report {report_id}") |
74 |
| - print("Probably not ready yet, waiting 5 seconds then retrying...") |
75 |
| - time.sleep(args.sleep_time) |
| 70 | + response = bd_client.session.get(location) |
| 71 | + report_status = response.json().get('status', 'Not Ready') |
| 72 | + if response.status_code == 200 and report_status == 'COMPLETED': |
| 73 | + response = bd.session.get(download_url, headers={'Content-Type': 'application/zip', 'Accept':'application/zip'}) |
| 74 | + if response.status_code == 200: |
| 75 | + with open(filename, "wb") as f: |
| 76 | + f.write(response.content) |
| 77 | + logging.info(f"Successfully downloaded zip file to {filename} for report {report_id}") |
| 78 | + else: |
| 79 | + logging.error(f"Failed to download report") |
| 80 | + else: |
76 | 81 | retries -= 1
|
77 |
| - download_report(location, filename, retries) |
| 82 | + logging.debug(f"Failed to retrieve report {report_id}, report status: {report_status}") |
| 83 | + logging.debug(f"Will retry {retries} more times. Sleeping for {args.sleep_time} second(s)") |
| 84 | + time.sleep(args.sleep_time) |
| 85 | + download_report(bd_client, location, filename, retries) |
78 | 86 | else:
|
79 | 87 | raise FailedReportDownload(f"Failed to retrieve report {report_id} after multiple retries")
|
80 | 88 |
|
81 |
| -# hub = HubInstance() |
82 |
| - |
83 | 89 | with open(args.token_file, 'r') as tf:
|
84 | 90 | access_token = tf.readline().strip()
|
85 | 91 |
|
|
0 commit comments