mirror of
https://github.com/python/cpython.git
synced 2025-10-02 13:22:19 +00:00
[3.14] gh-134262: Add retries to downloads in PCbuild\get_external.py (GH-134865)
(cherry picked from commite9d845b41d
) Includes fix for off-by-one error from GH-134867 (cherry-picked from commite64395e8eb
) Co-authored-by: Emma Smith <emma@emmatyping.dev>
This commit is contained in:
parent
52bae918c3
commit
34b451532b
1 changed files with 21 additions and 2 deletions
|
@ -9,6 +9,25 @@ import zipfile
|
|||
from urllib.request import urlretrieve
|
||||
|
||||
|
||||
def retrieve_with_retries(download_location, output_path, reporthook,
|
||||
max_retries=7):
|
||||
"""Download a file with exponential backoff retry and save to disk."""
|
||||
for attempt in range(max_retries + 1):
|
||||
try:
|
||||
resp = urlretrieve(
|
||||
download_location,
|
||||
output_path,
|
||||
reporthook=reporthook,
|
||||
)
|
||||
except ConnectionError as ex:
|
||||
if attempt == max_retries:
|
||||
msg = f"Download from {download_location} failed."
|
||||
raise OSError(msg) from ex
|
||||
time.sleep(2.25**attempt)
|
||||
else:
|
||||
return resp
|
||||
|
||||
|
||||
def fetch_zip(commit_hash, zip_dir, *, org='python', binary=False, verbose):
|
||||
repo = f'cpython-{"bin" if binary else "source"}-deps'
|
||||
url = f'https://github.com/{org}/{repo}/archive/{commit_hash}.zip'
|
||||
|
@ -16,10 +35,10 @@ def fetch_zip(commit_hash, zip_dir, *, org='python', binary=False, verbose):
|
|||
if verbose:
|
||||
reporthook = print
|
||||
zip_dir.mkdir(parents=True, exist_ok=True)
|
||||
filename, headers = urlretrieve(
|
||||
filename, _headers = retrieve_with_retries(
|
||||
url,
|
||||
zip_dir / f'{commit_hash}.zip',
|
||||
reporthook=reporthook,
|
||||
reporthook
|
||||
)
|
||||
return filename
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue