@@ -64,18 +64,28 @@ def run(*a: str, print_crash_reports: bool = False) -> None:
6464 raise SystemExit (f'The following process failed with exit code: { ret } :\n { cmd } ' )
6565
6666
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
77+
78+
6779def install_fonts () -> None :
68- with urlopen (FONTS_URL ) as f :
69- data = f .read ()
80+ data = download_with_retry (FONTS_URL )
7081 fonts_dir = os .path .expanduser ('~/Library/Fonts' if is_macos else '~/.local/share/fonts' )
7182 os .makedirs (fonts_dir , exist_ok = True )
7283 with tarfile .open (fileobj = io .BytesIO (data ), mode = 'r:xz' ) as tf :
7384 try :
7485 tf .extractall (fonts_dir , filter = 'fully_trusted' )
7586 except TypeError :
7687 tf .extractall (fonts_dir )
77- with urlopen (NERD_URL ) as f :
78- data = f .read ()
88+ data = download_with_retry (NERD_URL )
7989 with tarfile .open (fileobj = io .BytesIO (data ), mode = 'r:xz' ) as tf :
8090 try :
8191 tf .extractall (fonts_dir , filter = 'fully_trusted' )
@@ -164,8 +174,7 @@ def install_bundle(dest: str = '', which: str = '') -> None:
164174 os .makedirs (dest , exist_ok = True )
165175 os .chdir (dest )
166176 which = which or ('macos' if is_macos else 'linux' )
167- with urlopen (BUNDLE_URL .format (which )) as f :
168- data = f .read ()
177+ data = download_with_retry (BUNDLE_URL .format (which ))
169178 with tarfile .open (fileobj = io .BytesIO (data ), mode = 'r:xz' ) as tf :
170179 try :
171180 tf .extractall (filter = 'fully_trusted' )
@@ -188,22 +197,15 @@ def install_grype() -> str:
188197 rq = Request ('https://api.github.com/repos/anchore/grype/releases/latest' , headers = {
189198 'Accept' : 'application/vnd.github.v3+json' ,
190199 })
191- try :
192- with urlopen (rq ) as f :
193- m = json .loads (f .read ())
194- except URLError :
195- time .sleep (1 )
196- with urlopen (rq ) as f :
197- m = json .loads (f .read ())
200+ m = json .loads (download_with_retry (rq ))
198201 for asset in m ['assets' ]:
199202 if asset ['name' ].endswith ('_linux_amd64.tar.gz' ):
200203 url = asset ['browser_download_url' ]
201204 break
202205 else :
203206 raise ValueError ('Could not find linux binary for grype' )
204207 os .makedirs (dest , exist_ok = True )
205- with urlopen (url ) as f :
206- data = f .read ()
208+ data = download_with_retry (url )
207209 with tarfile .open (fileobj = io .BytesIO (data ), mode = 'r' ) as tf :
208210 tf .extract ('grype' , path = dest , filter = 'fully_trusted' )
209211 return os .path .join (dest , 'grype' )
0 commit comments