@@ -306,6 +306,8 @@ def download_bazel_into_directory(version, is_commit, directory):
306306
307307 sha256_path = destination_path + ".sha256"
308308 expected_hash = ""
309+ matcher = re .compile (r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?" )
310+ matched = matcher .match (version )
309311 if not os .path .exists (sha256_path ):
310312 try :
311313 download (bazel_url + ".sha256" , sha256_path )
@@ -314,21 +316,20 @@ def download_bazel_into_directory(version, is_commit, directory):
314316 sys .stderr .write (
315317 "The Bazel mirror does not have a checksum file; skipping checksum verification."
316318 )
317- if "https://releases.bazel.build" not in bazel_url :
318- matched = re .match (r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?" , version )
319- if matched :
320- (version , rc ) = matched .groups ()
321- fallback_url = "https://releases.bazel.build/{}/{}/{}" .format (
322- version , rc if rc else "release" , bazel_filename
323- )
324- download (fallback_url + ".sha256" , sha256_path )
319+ if "https://releases.bazel.build" in bazel_url or not matched :
320+ return destination_path
321+ if matched :
322+ (version , rc ) = matched .groups ()
323+ fallback_url = "https://releases.bazel.build/{}/{}/{}" .format (
324+ version , rc if rc else "release" , bazel_filename
325+ )
326+ try :
327+ download ("{}.sha256" .format (fallback_url ), sha256_path )
325328 os .remove (destination_path )
326329 download (fallback_url ,destination_path )
327- os .chmod (destination_path , 0o755 )
328- else :
330+ except HTTPError :
329331 return destination_path
330- else :
331- return destination_path
332+ os .chmod (destination_path , 0o755 )
332333 raise e
333334 with open (sha256_path , "r" ) as sha_file :
334335 expected_hash = sha_file .read ().split ()[0 ]
@@ -339,20 +340,22 @@ def download_bazel_into_directory(version, is_commit, directory):
339340 actual_hash = sha256_hash .hexdigest ()
340341 if actual_hash != expected_hash :
341342 os .remove (sha256_path )
343+ os .remove (destination_path )
342344 print (
343345 "The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Fallback to default releases.bazel.build url." .format (
344346 expected_hash , actual_hash
345347 )
346348 )
347- matched = re .match (r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?" , version )
348349 if matched :
349350 (version , rc ) = matched .groups ()
350351 fallback_url = "https://releases.bazel.build/{}/{}/{}" .format (
351352 version , rc if rc else "release" , bazel_filename
352353 )
353- download (fallback_url + ".sha256" , sha256_path )
354- os .remove (destination_path )
355- download (fallback_url ,destination_path )
354+ try :
355+ download ("{}.sha256" .format (fallback_url ), sha256_path )
356+ download (fallback_url ,destination_path )
357+ except HTTPError :
358+ exit (22 )
356359 os .chmod (destination_path , 0o755 )
357360 return destination_path
358361
0 commit comments