diff --git a/bazelisk.py b/bazelisk.py index be93ef96..3788ff0b 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -306,6 +306,8 @@ def download_bazel_into_directory(version, is_commit, directory): sha256_path = destination_path + ".sha256" expected_hash = "" + matcher=re.compile(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?") + matched=matcher.match(version) if not os.path.exists(sha256_path): try: download(bazel_url + ".sha256", sha256_path) @@ -314,21 +316,20 @@ def download_bazel_into_directory(version, is_commit, directory): sys.stderr.write( "The Bazel mirror does not have a checksum file; skipping checksum verification." ) - if "https://releases.bazel.build" not in bazel_url: - matched=re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version) - if matched: - (version, rc) = matched.groups() - fallback_url="https://releases.bazel.build/{}/{}/{}".format( - version, rc if rc else "release", bazel_filename - ) - download(fallback_url+".sha256", sha256_path) + if "https://releases.bazel.build" in bazel_url or not matched: + return destination_path + if matched: + (version, rc) = matched.groups() + fallback_url="https://releases.bazel.build/{}/{}/{}".format( + version, rc if rc else "release", bazel_filename + ) + try: + download("{}.sha256".format(fallback_url), sha256_path) os.remove(destination_path) download(fallback_url,destination_path) - os.chmod(destination_path, 0o755) - else: + except HTTPError: return destination_path - else: - return destination_path + os.chmod(destination_path, 0o755) raise e with open(sha256_path, "r") as sha_file: expected_hash = sha_file.read().split()[0] @@ -339,20 +340,22 @@ def download_bazel_into_directory(version, is_commit, directory): actual_hash = sha256_hash.hexdigest() if actual_hash != expected_hash: os.remove(sha256_path) + os.remove(destination_path) print( "The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Fallback to default releases.bazel.build url.".format( expected_hash, actual_hash ) ) - matched=re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version) if matched: (version, rc) = matched.groups() fallback_url="https://releases.bazel.build/{}/{}/{}".format( version, rc if rc else "release", bazel_filename ) - download(fallback_url+".sha256", sha256_path) - os.remove(destination_path) - download(fallback_url,destination_path) + try: + download("{}.sha256".format(fallback_url), sha256_path) + download(fallback_url,destination_path) + except HTTPError: + exit(22) os.chmod(destination_path, 0o755) return destination_path