Skip to content

Commit 77f11b9

Browse files
committed
Add retries to get_external.py downloads as well
1 parent ac539e7 commit 77f11b9

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

PCbuild/get_external.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
11
#!/usr/bin/env python3
22

33
import argparse
4+
import html
45
import os
56
import pathlib
7+
import random
68
import sys
79
import time
810
import zipfile
911
from urllib.request import urlretrieve
1012

1113

14+
def retrieve_with_retries(download_location,
15+
output_path,
16+
reporthook,
17+
max_retries = 7,
18+
base_delay = 2.25,
19+
max_jitter = 1.0):
20+
"""Download a file with exponential backoff retry and save to disk."""
21+
for attempt in range(max_retries):
22+
try:
23+
resp = urlretrieve(
24+
download_location,
25+
output_path,
26+
reporthook=reporthook,
27+
)
28+
except ConnectionError as ex:
29+
if attempt == max_retries:
30+
msg = f"Download from {download_location} failed."
31+
raise OSError(msg) from ex
32+
time.sleep(base_delay**attempt + random.uniform(0, max_jitter))
33+
else:
34+
return resp
35+
36+
1237
def fetch_zip(commit_hash, zip_dir, *, org='python', binary=False, verbose):
1338
repo = f'cpython-{"bin" if binary else "source"}-deps'
1439
url = f'https://github.com/{org}/{repo}/archive/{commit_hash}.zip'
1540
reporthook = None
1641
if verbose:
1742
reporthook = print
1843
zip_dir.mkdir(parents=True, exist_ok=True)
19-
filename, headers = urlretrieve(
44+
filename, _headers = retrieve_with_retries(
2045
url,
2146
zip_dir / f'{commit_hash}.zip',
22-
reporthook=reporthook,
47+
reporthook
2348
)
2449
return filename
2550

0 commit comments

Comments
 (0)