From ac2ae9dad9e79f51485aefa836ca85bb01d9c4a5 Mon Sep 17 00:00:00 2001 From: Zerun Zhao Date: Thu, 2 Mar 2023 19:24:59 -0800 Subject: [PATCH 1/6] add fallback to default release bazel url --- bazelisk.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/bazelisk.py b/bazelisk.py index 8a967c27..889379fd 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -316,6 +316,12 @@ 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: + (version, rc) = re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version).groups() + fallback_url="https://releases.bazel.build/{}/{}/{}".format( + version, rc if rc else "release", bazel_filename + ) + download(fallback_url, destination_path) return destination_path raise e with open(sha256_path, "r") as sha_file: @@ -326,16 +332,17 @@ def download_bazel_into_directory(version, is_commit, directory): sha256_hash.update(byte_block) actual_hash = sha256_hash.hexdigest() if actual_hash != expected_hash: - os.remove(destination_path) os.remove(sha256_path) print( - "The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Please try again.".format( + "The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Fallback to default releases.bazel build url.".format( expected_hash, actual_hash ) ) - # Exiting with a special exit code not used by Bazel, so the calling process may retry based on that. - # https://docs.bazel.build/versions/0.21.0/guide.html#what-exit-code-will-i-get - sys.exit(22) + (version, rc) = re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version).groups() + fallback_url="https://releases.bazel.build/{}/{}/{}".format( + version, rc if rc else "release", bazel_filename + ) + download(fallback_url, destination_path) return destination_path From f49ad68e0539b50adb6d2aa985d49ee9133dc469 Mon Sep 17 00:00:00 2001 From: Zerun Zhao Date: Thu, 2 Mar 2023 19:32:26 -0800 Subject: [PATCH 2/6] add fallback to default release bazel url --- bazelisk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bazelisk.py b/bazelisk.py index 889379fd..ed997ce7 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -334,7 +334,7 @@ def download_bazel_into_directory(version, is_commit, directory): if actual_hash != expected_hash: os.remove(sha256_path) print( - "The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Fallback to default releases.bazel build url.".format( + "The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Fallback to default releases.bazel.build url.".format( expected_hash, actual_hash ) ) From bb3d6f75b2cdee321de4aff30a7ebe8adc686ea2 Mon Sep 17 00:00:00 2001 From: Zerun Zhao Date: Fri, 3 Mar 2023 10:40:15 -0800 Subject: [PATCH 3/6] update --- bazelisk.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bazelisk.py b/bazelisk.py index ed997ce7..5f0cf8c3 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -317,11 +317,13 @@ def download_bazel_into_directory(version, is_commit, directory): "The Bazel mirror does not have a checksum file; skipping checksum verification." ) if "https://releases.bazel.build" not in bazel_url: - (version, rc) = re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version).groups() - fallback_url="https://releases.bazel.build/{}/{}/{}".format( - version, rc if rc else "release", bazel_filename - ) - download(fallback_url, destination_path) + 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, destination_path) return destination_path raise e with open(sha256_path, "r") as sha_file: From 826f2e1b639a37594c4caf1ff788526d9c9b2f2d Mon Sep 17 00:00:00 2001 From: Zerun Zhao Date: Fri, 3 Mar 2023 13:27:40 -0800 Subject: [PATCH 4/6] update --- bazelisk.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/bazelisk.py b/bazelisk.py index 5f0cf8c3..6fff50c0 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -323,8 +323,14 @@ def download_bazel_into_directory(version, is_commit, directory): fallback_url="https://releases.bazel.build/{}/{}/{}".format( version, rc if rc else "release", bazel_filename ) - download(fallback_url, destination_path) - return destination_path + download(fallback_url+".sha256", sha256_path) + os.remove(destination_path) + download(fallback_url,destination_path) + os.chmod(destination_path, 0o755) + else: + return destination_path + else: + return destination_path raise e with open(sha256_path, "r") as sha_file: expected_hash = sha_file.read().split()[0] @@ -340,11 +346,16 @@ def download_bazel_into_directory(version, is_commit, directory): expected_hash, actual_hash ) ) - (version, rc) = re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version).groups() - fallback_url="https://releases.bazel.build/{}/{}/{}".format( - version, rc if rc else "release", bazel_filename - ) - download(fallback_url, destination_path) + 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) + os.chmod(destination_path, 0o755) return destination_path From 8b9e8d63c53b197f56a5e230e90ac4e8423b10b0 Mon Sep 17 00:00:00 2001 From: Zerun Zhao Date: Thu, 16 Mar 2023 11:07:53 -0700 Subject: [PATCH 5/6] address comments --- bazelisk.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/bazelisk.py b/bazelisk.py index 6fff50c0..a17ec652 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -308,6 +308,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) @@ -316,21 +318,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] @@ -341,20 +342,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 From 2e46381c7e18ba45b80b65c056bc63283329146d Mon Sep 17 00:00:00 2001 From: Zerun Zhao Date: Wed, 5 Jul 2023 10:27:57 -0700 Subject: [PATCH 6/6] run black --- bazelisk.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/bazelisk.py b/bazelisk.py index a17ec652..be2cd1cd 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -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, ) @@ -308,8 +307,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) + 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) @@ -322,13 +321,13 @@ def download_bazel_into_directory(version, is_commit, directory): return destination_path if matched: (version, rc) = matched.groups() - fallback_url="https://releases.bazel.build/{}/{}/{}".format( + 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) + download(fallback_url, destination_path) except HTTPError: return destination_path os.chmod(destination_path, 0o755) @@ -350,12 +349,12 @@ def download_bazel_into_directory(version, is_commit, directory): ) if matched: (version, rc) = matched.groups() - fallback_url="https://releases.bazel.build/{}/{}/{}".format( + fallback_url = "https://releases.bazel.build/{}/{}/{}".format( version, rc if rc else "release", bazel_filename ) try: download("{}.sha256".format(fallback_url), sha256_path) - download(fallback_url,destination_path) + download(fallback_url, destination_path) except HTTPError: exit(22) os.chmod(destination_path, 0o755)