Skip to content

Commit 0c5a8b0

Browse files
pythongh-134262: Add retries to generate_sbom.py (python#134263)
Co-authored-by: Semyon Moroz <[email protected]>
1 parent b8998fe commit 0c5a8b0

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Tools/build/generate_sbom.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import re
88
import subprocess
99
import sys
10+
import time
1011
import typing
12+
import urllib.error
1113
import urllib.request
1214
from pathlib import Path, PurePosixPath, PureWindowsPath
1315

@@ -161,6 +163,21 @@ def get_externals() -> list[str]:
161163
return externals
162164

163165

166+
def download_with_retries(download_location: str,
167+
max_retries: int = 5,
168+
base_delay: float = 2.0) -> typing.Any:
169+
"""Download a file with exponential backoff retry."""
170+
for attempt in range(max_retries):
171+
try:
172+
resp = urllib.request.urlopen(download_location)
173+
except urllib.error.URLError as ex:
174+
if attempt == max_retries:
175+
raise ex
176+
time.sleep(base_delay**attempt)
177+
else:
178+
return resp
179+
180+
164181
def check_sbom_packages(sbom_data: dict[str, typing.Any]) -> None:
165182
"""Make a bunch of assertions about the SBOM package data to ensure it's consistent."""
166183

@@ -175,7 +192,7 @@ def check_sbom_packages(sbom_data: dict[str, typing.Any]) -> None:
175192
# and that the download URL is valid.
176193
if "checksums" not in package or "CI" in os.environ:
177194
download_location = package["downloadLocation"]
178-
resp = urllib.request.urlopen(download_location)
195+
resp = download_with_retries(download_location)
179196
error_if(resp.status != 200, f"Couldn't access URL: {download_location}'")
180197

181198
package["checksums"] = [{

0 commit comments

Comments
 (0)