|
2 | 2 | import csv |
3 | 3 | import json |
4 | 4 | import string |
| 5 | +import time |
5 | 6 | import urllib.request |
6 | 7 | from multiprocessing import Pool |
7 | 8 | from argparse import ArgumentParser |
8 | 9 |
|
| 10 | +RETRY = 10 |
| 11 | + |
9 | 12 | access_token = os.getenv("C3_TOKEN") |
| 13 | +if not access_token: |
| 14 | + raise SystemExit("C3_TOKEN required") |
10 | 15 |
|
11 | 16 |
|
12 | 17 | def parse_args(): |
13 | 18 | ap = ArgumentParser() |
14 | 19 | ap.add_argument("--filter", choices=("id", "artifact_name"), default="id") |
15 | 20 | ap.add_argument("csvs", nargs="+") |
16 | | - ap.add_argument("test_id") |
| 21 | + ap.add_argument("--test-ids", nargs="+", required=True) |
17 | 22 | return ap.parse_args() |
18 | 23 |
|
19 | 24 |
|
20 | 25 | def get_summary(sub_id): |
21 | 26 | """ |
22 | 27 | Get the validation results of a given list of submissions from the C3 API. |
23 | 28 | """ |
24 | | - api_url = ( |
25 | | - f"https://certification.canonical.com/api/v2/reports/summary/{sub_id}" |
26 | | - ) |
27 | | - headers = { |
28 | | - "Content-Type": "application/json", |
29 | | - "Authorization": f"Bearer {access_token}", |
30 | | - } |
31 | | - # Convert the payload to a JSON string |
32 | | - # Create the request object with the data and headers |
33 | | - req = urllib.request.Request(api_url, headers=headers, method="GET") |
34 | | - # Send the request and get the response |
35 | | - with urllib.request.urlopen(req) as response: |
36 | | - result = json.loads(response.read().decode("utf-8")) |
| 29 | + for i in range(RETRY): |
| 30 | + try: |
| 31 | + api_url = f"https://certification.canonical.com/api/v2/reports/summary/{sub_id}" |
| 32 | + headers = { |
| 33 | + "Content-Type": "application/json", |
| 34 | + "Authorization": f"Bearer {access_token}", |
| 35 | + } |
| 36 | + # Convert the payload to a JSON string |
| 37 | + # Create the request object with the data and headers |
| 38 | + req = urllib.request.Request( |
| 39 | + api_url, headers=headers, method="GET" |
| 40 | + ) |
| 41 | + # Send the request and get the response |
| 42 | + with urllib.request.urlopen(req, timeout=60) as response: |
| 43 | + result = json.loads(response.read().decode("utf-8")) |
37 | 44 |
|
38 | | - return (sub_id, result) |
| 45 | + return (sub_id, result) |
| 46 | + except Exception as e: |
| 47 | + if i == RETRY - 1: |
| 48 | + raise |
| 49 | + print(f"C3 failed with exception: {e}", flush=True) |
| 50 | + time.sleep(min(i * 10, 60)) |
39 | 51 |
|
40 | 52 |
|
41 | 53 | def slugify(_string: str): |
42 | 54 | if not _string: |
43 | 55 | return _string |
| 56 | + _string = _string.replace("com.canonical.certification::", "") |
44 | 57 |
|
45 | 58 | valid_chars = frozenset(f"_{string.ascii_letters}{string.digits}") |
46 | 59 | # Python identifiers cannot start with a digit |
@@ -75,12 +88,11 @@ def _f(x: dict) -> bool: |
75 | 88 | ] |
76 | 89 |
|
77 | 90 |
|
78 | | -def main(): |
79 | | - args = parse_args() |
| 91 | +def do_test_id(filter_arg, csvs, test_id): |
80 | 92 | lines_of_interest = [] |
81 | | - filter_f = get_filter(args.filter, args.test_id) |
| 93 | + filter_f = get_filter(filter_arg, test_id) |
82 | 94 |
|
83 | | - for f_path in args.csvs: |
| 95 | + for f_path in csvs: |
84 | 96 | with open(f_path) as f: |
85 | 97 | reader = csv.DictReader(f, delimiter=",") |
86 | 98 | lines_of_interest += list(filter(filter_f, reader)) |
@@ -109,7 +121,7 @@ def main(): |
109 | 121 | } | relevant_template_id |
110 | 122 | relevant_ids = relevant_ids - {None, ""} |
111 | 123 |
|
112 | | - file_name = slugify(args.test_id) |
| 124 | + file_name = slugify(test_id) |
113 | 125 |
|
114 | 126 | with open(f"{file_name}.url", "w+") as f: |
115 | 127 | f.writelines("\n".join(sub_links)) |
@@ -193,5 +205,13 @@ def main(): |
193 | 205 | writer.writerows(result_rows) |
194 | 206 |
|
195 | 207 |
|
| 208 | +def main(): |
| 209 | + args = parse_args() |
| 210 | + tot = len(args.test_ids) |
| 211 | + for i, test_id in enumerate(args.test_ids, 1): |
| 212 | + print(f"Doing [{i}/{tot}]: {test_id}") |
| 213 | + do_test_id(args.filter, args.csvs, test_id) |
| 214 | + |
| 215 | + |
196 | 216 | if __name__ == "__main__": |
197 | 217 | main() |
0 commit comments