]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-134262: Add retries to downloads in PCbuild\get_external.py (GH-134820)
authorEmma Smith <emma@emmatyping.dev>
Wed, 28 May 2025 21:18:34 +0000 (14:18 -0700)
committerGitHub <noreply@github.com>
Wed, 28 May 2025 21:18:34 +0000 (22:18 +0100)
PCbuild/get_external.py

index 4ecc8925349c930e10a13aac142e2143bba4a60d..99aff63882f5baabf4ddca566b3fd4ee0f70961b 100755 (executable)
@@ -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):
+        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