Skip to content

Commit

Permalink
Add fallback for github release data (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharsadhwani authored Jan 24, 2024
1 parent 2793e4a commit 69d7340
Show file tree
Hide file tree
Showing 7 changed files with 19,903 additions and 9 deletions.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = yen
version = 0.4.1post1
version = 0.4.2
description = Yet another Python environment manager.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down Expand Up @@ -49,6 +49,7 @@ dev =
yen =
py.typed
activate.sh
fallback_release_data.json

[tool:pytest]
addopts = --cov --cov-report=term-missing
2 changes: 1 addition & 1 deletion src/yen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def ensure_python(python_version: str) -> tuple[str, str]:
# already installed
return python_version, python_bin_path

os.makedirs(download_directory)
os.makedirs(download_directory, exist_ok=True)
downloaded_filepath = download(
download_link,
f"Downloading {python_version}",
Expand Down
19,874 changes: 19,874 additions & 0 deletions src/yen/fallback_release_data.json

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions src/yen/github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
import os.path
import platform
import re
import urllib.error
from typing import Any
from urllib.request import urlopen

MACHINE_SUFFIX = {
Expand All @@ -22,19 +25,32 @@
}

GITHUB_API_URL = (
f"https://api.github.com/repos/indygreg/python-build-standalone/releases/latest"
"https://api.github.com/repos/indygreg/python-build-standalone/releases/latest"
)
PYTHON_VERSION_REGEX = re.compile(r"cpython-(\d+\.\d+\.\d+)")


def fallback_release_data() -> dict[str, Any]:
"""Returns the fallback release data, for when GitHub API gives an error."""
print("\033[33mWarning: GitHub unreachable. Using fallback release data.\033[m")
data_file = os.path.join(os.path.dirname(__file__), "fallback_release_data.json")
with open(data_file) as data:
return json.load(data)


class NotAvailable(Exception):
"""Raised when the asked Python version is not available."""


def get_latest_python_releases() -> list[str]:
"""Returns the list of python download links from the latest github release."""
with urlopen(GITHUB_API_URL) as response:
release_data = json.load(response)
try:
with urlopen(GITHUB_API_URL) as response:
release_data = json.load(response)

except urllib.error.URLError:
# raise
release_data = fallback_release_data()

return [asset["browser_download_url"] for asset in release_data["assets"]]

Expand Down
2 changes: 1 addition & 1 deletion yen-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion yen-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yen-rs"
version = "0.4.0"
version = "0.4.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
7 changes: 5 additions & 2 deletions yen-rs/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ impl MachineSuffix {
}
}

const FALLBACK_RESPONSE_BYTES: &[u8] = include_bytes!("../../src/yen/fallback_release_data.json");

async fn get_latest_python_release() -> miette::Result<Vec<String>> {
let response = YEN_CLIENT
.get(*GITHUB_API_URL)
Expand All @@ -124,10 +126,11 @@ async fn get_latest_python_release() -> miette::Result<Vec<String>> {
// Log the response body if the status is not successful
let status_code = response.status().as_u16();
let success = response.status().is_success();
let body = response.text().await.into_diagnostic()?;
let mut body = response.text().await.into_diagnostic()?;
if !success {
log::error!("Error response: {}\nStatus Code: {}", body, status_code);
miette::bail!("Non-successful API response, check the logs for more info.");
let fallback_response = String::from_utf8_lossy(FALLBACK_RESPONSE_BYTES).into_owned();
body = fallback_response;
}

// Attempt to parse the JSON
Expand Down

0 comments on commit 69d7340

Please sign in to comment.