Skip to content

Commit 53e2f00

Browse files
committed
A fancier download_with_retry function
1 parent a05a56e commit 53e2f00

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

.github/workflows/ci.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
import sys
1313
import tarfile
1414
import time
15-
from urllib.error import URLError
16-
from urllib.request import Request, urlopen
15+
from urllib.request import Request
1716

1817
BUNDLE_URL = 'https://download.calibre-ebook.com/ci/kitty/{}-64.tar.xz'
1918
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:
6463
raise SystemExit(f'The following process failed with exit code: {ret}:\n{cmd}')
6564

6665

67-
def download_with_retry(url_or_rq: str | Request) -> bytes:
68-
ans: bytes = b''
69-
try:
70-
with urlopen(url_or_rq) as f:
71-
ans = f.read()
72-
except URLError:
73-
time.sleep(1)
74-
with urlopen(url_or_rq) as f:
75-
ans = f.read()
76-
return ans
66+
def download_with_retry(url: str | Request, count: int = 5) -> bytes:
67+
from urllib.request import urlopen
68+
for i in range(count):
69+
try:
70+
print('Downloading', url, flush=True)
71+
ans: bytes = urlopen(url).read()
72+
return ans
73+
except Exception as err:
74+
if i >= count - 1:
75+
raise
76+
print(f'Download failed with error {err} retrying...', file=sys.stderr)
77+
time.sleep(1)
78+
return b''
7779

7880

7981
def install_fonts() -> None:

0 commit comments

Comments
 (0)