Skip to content

Commit 7762de2

Browse files
[3.13] gh-134262: Add retries to generate_sbom.py (GH-134263) (#137468)
(cherry picked from commit 0c5a8b0) Co-authored-by: Semyon Moroz <donbarbos@proton.me>
1 parent b772427 commit 7762de2

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Tools/build/generate_sbom.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from pathlib import Path, PurePosixPath, PureWindowsPath
88
import subprocess
99
import sys
10+
import time
11+
import typing
12+
import urllib.error
1013
import urllib.request
1114
import typing
1215

@@ -163,6 +166,21 @@ def get_externals() -> list[str]:
163166
return externals
164167

165168

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

@@ -177,7 +195,7 @@ def check_sbom_packages(sbom_data: dict[str, typing.Any]) -> None:
177195
# and that the download URL is valid.
178196
if "checksums" not in package or "CI" in os.environ:
179197
download_location = package["downloadLocation"]
180-
resp = urllib.request.urlopen(download_location)
198+
resp = download_with_retries(download_location)
181199
error_if(resp.status != 200, f"Couldn't access URL: {download_location}'")
182200

183201
package["checksums"] = [{

0 commit comments

Comments
 (0)