Skip to content

Commit

Permalink
fix(scripts): wraps download request in try/except block
Browse files Browse the repository at this point in the history
  • Loading branch information
j-luong committed Jul 25, 2024
1 parent c4c8feb commit 90605e7
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions scripts/install-snyk.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,43 +59,51 @@ def download_snyk_cli(download_version, base_url, use_backup_url=1):
url = f"{base_url}/cli/{download_version}/{filename}"

print(f"Downloading '{filename}' from: {url}")
response = requests.get(url)

if response.status_code == 200:
sha_response = requests.get(url + ".sha256")
if not sha_response:
print("SHA256 checksum not available. Aborting download.")
return

sha256_checksum = sha_response.text.split()[0]

downloaded_file_path = filename

with open(downloaded_file_path, "wb") as f:
f.write(response.content)

print("Verifying checksum")
if verify_checksum(downloaded_file_path, sha256_checksum):
print("Checksum verified")
os.rename(downloaded_file_path, filename)
print(f"Snyk CLI {download_version} downloaded successfully to {filename}")

# Make the file executable
os.chmod(filename, 0o755)
os.rename(filename, output_filename)

print("Running 'snyk -v' to check the version:")

executable = os.path.join(os.getcwd(), output_filename)
os.system(f"{executable} -v")


try:
response = requests.get(url)

if response.status_code == 200:
sha_response = requests.get(url + ".sha256")
if not sha_response:
print("SHA256 checksum not available. Aborting download.")
return

sha256_checksum = sha_response.text.split()[0]

downloaded_file_path = filename

with open(downloaded_file_path, "wb") as f:
f.write(response.content)

print("Verifying checksum")
if verify_checksum(downloaded_file_path, sha256_checksum):
print("Checksum verified")
os.rename(downloaded_file_path, filename)
print(f"Snyk CLI {download_version} downloaded successfully to {filename}")

# Make the file executable
os.chmod(filename, 0o755)
os.rename(filename, output_filename)

print("Running 'snyk -v' to check the version:")

executable = os.path.join(os.getcwd(), output_filename)
os.system(f"{executable} -v")

else:
os.remove(downloaded_file_path)
print("SHA256 checksum verification failed. Downloaded file deleted.")
return fail
return success
else:
os.remove(downloaded_file_path)
print("SHA256 checksum verification failed. Downloaded file deleted.")
print(f"Failed to download Snyk CLI {download_version} via {base_url}")
if use_backup_url == 1:
print(f"Trying download from: {backup_url}")
return download_snyk_cli(download_version, backup_url, use_backup_url=0)
return fail
return success
else:
print(f"Failed to download Snyk CLI {download_version} via {base_url}")
except requests.RequestException as e:
print(f"Error trying to download Snyk CLI {download_version} via {base_url}: {e}")
if use_backup_url == 1:
print(f"Trying download from: {backup_url}")
return download_snyk_cli(download_version, backup_url, use_backup_url=0)
Expand Down

0 comments on commit 90605e7

Please sign in to comment.