diff --git a/setup/unix-ci.py b/setup/unix-ci.py index cad269bb77..e1799bcbb3 100644 --- a/setup/unix-ci.py +++ b/setup/unix-ci.py @@ -12,7 +12,7 @@ import sys import tarfile import time from tempfile import NamedTemporaryFile -from urllib.request import Request +from urllib.request import Request, urlopen _plat = sys.platform.lower() ismacos = 'darwin' in _plat @@ -23,19 +23,21 @@ def setenv(key, val): os.environ[key] = os.path.expandvars(val) -def download_with_retry(url, count=5): - from urllib.request import urlopen - while count > 0: - count -= 1 +def download_with_retry(url: str | Request, count: int = 5) -> bytes: + for i in range(count): try: - print('Downloading', url, flush=True) + print('Downloading', getattr(url, 'full_url', url), flush=True) with urlopen(url) as f: - return f.read() - except Exception: - if count <= 0: + ans: bytes = f.read() + return ans + except Exception as err: + if getattr(err, 'code', -1) == 403: raise - print('Download failed retrying...') + if i >= count - 1: + raise + print(f'Download failed with error {err} retrying...', file=sys.stderr) time.sleep(1) + return b'' if ismacos: diff --git a/setup/win-ci.py b/setup/win-ci.py index 89bf7fbe29..f1ad59ff66 100644 --- a/setup/win-ci.py +++ b/setup/win-ci.py @@ -8,6 +8,7 @@ import subprocess import sys import tarfile import time +from urllib.request import Request, urlopen def printf(*args, **kw): @@ -15,19 +16,21 @@ def printf(*args, **kw): sys.stdout.flush() -def download_with_retry(url, count=5): - from urllib.request import urlopen - while count > 0: - count -= 1 +def download_with_retry(url: str | Request, count: int = 5) -> bytes: + for i in range(count): try: - printf('Downloading', url) + print('Downloading', getattr(url, 'full_url', url), flush=True) with urlopen(url) as f: - return f.read() - except Exception: - if count <= 0: + ans: bytes = f.read() + return ans + except Exception as err: + if getattr(err, 'code', -1) == 403: raise - print('Download failed retrying...') + if i >= count - 1: + raise + print(f'Download failed with error {err} retrying...', file=sys.stderr) time.sleep(1) + return b'' def sw():