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 e24ba47
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions bazelisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,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 @@ -339,21 +338,30 @@ def download_bazel_into_directory(version, is_commit, directory):
return destination_path


def download(url, destination_path):
sys.stderr.write("Downloading {}...\n".format(url))
request = Request(url)
if "BAZELISK_BASE_URL" in os.environ:
parts = urlparse(url)
creds = None
def download(url, destination_path, retries=5, wait_seconds=5):
while retries > 0:
retries -= 1
try:
creds = netrc.netrc().hosts.get(parts.netloc)
sys.stderr.write("Downloading {}...\n".format(url))
request = Request(url)
if "BAZELISK_BASE_URL" in os.environ:
parts = urlparse(url)
creds = None
try:
creds = netrc.netrc().hosts.get(parts.netloc)
except Exception:
pass
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)
return
except Exception:
pass
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)
if retries <= 0:
raise
time.sleep(wait_seconds)


def get_bazelisk_directory():
Expand Down

0 comments on commit e24ba47

Please sign in to comment.