Skip to content

Commit

Permalink
stupid simple retry logic for download
Browse files Browse the repository at this point in the history
  • Loading branch information
celestialorb committed Sep 8, 2023
1 parent 3f0897a commit 7020954
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions bazelisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@

BAZEL_UPSTREAM = "bazelbuild"

BAZEL_DOWNLOAD_RETRIES = 5
BAZEL_DOWNLOAD_RETRY_WAIT_SECS = 5


def decide_which_bazel_version_to_use():
# Check in this order:
Expand Down Expand Up @@ -180,8 +183,7 @@ def get_version_history(bazelisk_directory):
),
# This only handles versions with numeric components, but that is fine
# since prerelease versions have been excluded.
key=lambda version: tuple(int(component)
for component in version.split('.')),
key=lambda version: tuple(int(component) for component in version.split(".")),
reverse=True,
)

Expand Down Expand Up @@ -352,8 +354,16 @@ def download(url, destination_path):
if creds is not None:
auth = base64.b64encode(("%s:%s" % (creds[0], creds[2])).encode("ascii"))
request.add_header("Authorization", "Basic %s" % auth.decode("utf-8"))
with closing(urlopen(request)) as response, open(destination_path, "wb") as file:
shutil.copyfileobj(response, file)

tries = BAZEL_DOWNLOAD_RETRIES
while tries > 0:
try:
with closing(urlopen(request)) as response, open(destination_path, "wb") as file:
shutil.copyfileobj(response, file)
break
except Exception:
tries -= 1
time.sleep(BAZEL_DOWNLOAD_RETRY_WAIT_SECS)


def get_bazelisk_directory():
Expand Down

0 comments on commit 7020954

Please sign in to comment.