@@ -308,6 +308,8 @@ def download_bazel_into_directory(version, is_commit, directory):
308308
309309 sha256_path = destination_path + ".sha256"
310310 expected_hash = ""
311+ matcher = re .compile (r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?" )
312+ matched = matcher .match (version )
311313 if not os .path .exists (sha256_path ):
312314 try :
313315 download (bazel_url + ".sha256" , sha256_path )
@@ -316,21 +318,20 @@ def download_bazel_into_directory(version, is_commit, directory):
316318 sys .stderr .write (
317319 "The Bazel mirror does not have a checksum file; skipping checksum verification."
318320 )
319- if "https://releases.bazel.build" not in bazel_url :
320- matched = re .match (r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?" , version )
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- download (fallback_url + ".sha256" , sha256_path )
321+ if "https://releases.bazel.build" in bazel_url or not matched :
322+ return destination_path
323+ if matched :
324+ (version , rc ) = matched .groups ()
325+ fallback_url = "https://releases.bazel.build/{}/{}/{}" .format (
326+ version , rc if rc else "release" , bazel_filename
327+ )
328+ try :
329+ download ("{}.sha256" .format (fallback_url ), sha256_path )
327330 os .remove (destination_path )
328331 download (fallback_url ,destination_path )
329- os .chmod (destination_path , 0o755 )
330- else :
332+ except HTTPError :
331333 return destination_path
332- else :
333- return destination_path
334+ os .chmod (destination_path , 0o755 )
334335 raise e
335336 with open (sha256_path , "r" ) as sha_file :
336337 expected_hash = sha_file .read ().split ()[0 ]
@@ -341,20 +342,22 @@ def download_bazel_into_directory(version, is_commit, directory):
341342 actual_hash = sha256_hash .hexdigest ()
342343 if actual_hash != expected_hash :
343344 os .remove (sha256_path )
345+ os .remove (destination_path )
344346 print (
345347 "The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Fallback to default releases.bazel.build url." .format (
346348 expected_hash , actual_hash
347349 )
348350 )
349- matched = re .match (r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?" , version )
350351 if matched :
351352 (version , rc ) = matched .groups ()
352353 fallback_url = "https://releases.bazel.build/{}/{}/{}" .format (
353354 version , rc if rc else "release" , bazel_filename
354355 )
355- download (fallback_url + ".sha256" , sha256_path )
356- os .remove (destination_path )
357- download (fallback_url ,destination_path )
356+ try :
357+ download ("{}.sha256" .format (fallback_url ), sha256_path )
358+ download (fallback_url ,destination_path )
359+ except HTTPError :
360+ exit (22 )
358361 os .chmod (destination_path , 0o755 )
359362 return destination_path
360363
0 commit comments