Skip to content

Commit e24ba47

Browse files
committed
stupid simple retry logic for download
1 parent 3f0897a commit e24ba47

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

bazelisk.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ def get_version_history(bazelisk_directory):
180180
),
181181
# This only handles versions with numeric components, but that is fine
182182
# since prerelease versions have been excluded.
183-
key=lambda version: tuple(int(component)
184-
for component in version.split('.')),
183+
key=lambda version: tuple(int(component) for component in version.split(".")),
185184
reverse=True,
186185
)
187186

@@ -339,21 +338,30 @@ def download_bazel_into_directory(version, is_commit, directory):
339338
return destination_path
340339

341340

342-
def download(url, destination_path):
343-
sys.stderr.write("Downloading {}...\n".format(url))
344-
request = Request(url)
345-
if "BAZELISK_BASE_URL" in os.environ:
346-
parts = urlparse(url)
347-
creds = None
341+
def download(url, destination_path, retries=5, wait_seconds=5):
342+
while retries > 0:
343+
retries -= 1
348344
try:
349-
creds = netrc.netrc().hosts.get(parts.netloc)
345+
sys.stderr.write("Downloading {}...\n".format(url))
346+
request = Request(url)
347+
if "BAZELISK_BASE_URL" in os.environ:
348+
parts = urlparse(url)
349+
creds = None
350+
try:
351+
creds = netrc.netrc().hosts.get(parts.netloc)
352+
except Exception:
353+
pass
354+
if creds is not None:
355+
auth = base64.b64encode(("%s:%s" % (creds[0], creds[2])).encode("ascii"))
356+
request.add_header("Authorization", "Basic %s" % auth.decode("utf-8"))
357+
358+
with closing(urlopen(request)) as response, open(destination_path, "wb") as file:
359+
shutil.copyfileobj(response, file)
360+
return
350361
except Exception:
351-
pass
352-
if creds is not None:
353-
auth = base64.b64encode(("%s:%s" % (creds[0], creds[2])).encode("ascii"))
354-
request.add_header("Authorization", "Basic %s" % auth.decode("utf-8"))
355-
with closing(urlopen(request)) as response, open(destination_path, "wb") as file:
356-
shutil.copyfileobj(response, file)
362+
if retries <= 0:
363+
raise
364+
time.sleep(wait_seconds)
357365

358366

359367
def get_bazelisk_directory():

0 commit comments

Comments
 (0)