From 53e2f00473a6c5a22bb206c75296e5c4e78145d6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 22 Sep 2025 12:28:48 +0530 Subject: [PATCH] A fancier download_with_retry function --- .github/workflows/ci.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.py b/.github/workflows/ci.py index fca261f2f..af1bf1985 100644 --- a/.github/workflows/ci.py +++ b/.github/workflows/ci.py @@ -12,8 +12,7 @@ import sys import tarfile import time -from urllib.error import URLError -from urllib.request import Request, urlopen +from urllib.request import Request BUNDLE_URL = 'https://download.calibre-ebook.com/ci/kitty/{}-64.tar.xz' FONTS_URL = 'https://download.calibre-ebook.com/ci/fonts.tar.xz' @@ -64,16 +63,19 @@ def run(*a: str, print_crash_reports: bool = False) -> None: raise SystemExit(f'The following process failed with exit code: {ret}:\n{cmd}') -def download_with_retry(url_or_rq: str | Request) -> bytes: - ans: bytes = b'' - try: - with urlopen(url_or_rq) as f: - ans = f.read() - except URLError: - time.sleep(1) - with urlopen(url_or_rq) as f: - ans = f.read() - return ans +def download_with_retry(url: str | Request, count: int = 5) -> bytes: + from urllib.request import urlopen + for i in range(count): + try: + print('Downloading', url, flush=True) + ans: bytes = urlopen(url).read() + return ans + except Exception as err: + if i >= count - 1: + raise + print(f'Download failed with error {err} retrying...', file=sys.stderr) + time.sleep(1) + return b'' def install_fonts() -> None: