Created
December 20, 2024 03:22
-
-
Save jefftriplett/8aec12967a47318ceefe998f6c783aa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# dependencies = [ | |
# "httpx", | |
# "typer", | |
# ] | |
# /// | |
import sys | |
import platform | |
from datetime import datetime | |
import httpx | |
import typer | |
from rich import print | |
from typing import Optional | |
app = typer.Typer(help="Check Python version EOL dates") | |
def get_python_version() -> str: | |
"""Get the current Python version.""" | |
return platform.python_version() | |
def fetch_python_eol_data() -> list: | |
"""Fetch Python EOL data from endoflife.date API.""" | |
url = "https://endoflife.date/api/python.json" | |
with httpx.Client() as client: | |
try: | |
response = client.get(url) | |
response.raise_for_status() | |
return response.json() | |
except httpx.HTTPError as e: | |
print(f"[red]Error fetching EOL data: {e}[/red]") | |
sys.exit(1) | |
def find_version_eol(version: str, eol_data: list) -> Optional[str]: | |
"""Find EOL date for specific Python version.""" | |
major_minor = ".".join(version.split(".")[:2]) | |
for entry in eol_data: | |
if entry.get("cycle") == major_minor: | |
return entry.get("eol") | |
return None | |
@app.command() | |
def check( | |
version: str = typer.Option( | |
None, | |
"--version", | |
"-v", | |
help="Python version to check (defaults to current version)", | |
) | |
): | |
"""Check the EOL date for a specific Python version.""" | |
# Use specified version or get current version | |
check_version = version or get_python_version() | |
print(f"[blue]Checking EOL date for Python {check_version}[/blue]") | |
# Fetch EOL data | |
eol_data = fetch_python_eol_data() | |
# Find EOL date | |
eol_date = find_version_eol(check_version, eol_data) | |
if eol_date: | |
# Parse the EOL date | |
eol_datetime = datetime.strptime(eol_date, "%Y-%m-%d") | |
current_date = datetime.now() | |
# Calculate days until EOL | |
days_remaining = (eol_datetime - current_date).days | |
print(f"\nPython {check_version} EOL date: [green]{eol_date}[/green]") | |
if days_remaining > 0: | |
print(f"Days until EOL: [yellow]{days_remaining}[/yellow]") | |
else: | |
print("[red]⚠️ This version has reached its end of life![/red]") | |
else: | |
print(f"[red]Could not find EOL information for Python {check_version}[/red]") | |
if __name__ == "__main__": | |
app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment