|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Updates the bundled Distrobox release version and SHA256 hash. |
| 4 | +
|
| 5 | +This script: |
| 6 | +1. Fetches the latest release from GitHub |
| 7 | +2. Downloads the release tarball |
| 8 | +3. Computes its SHA256 hash |
| 9 | +4. Updates the constants in src/distrobox_downloader.rs |
| 10 | +""" |
| 11 | + |
| 12 | +import hashlib |
| 13 | +import json |
| 14 | +import re |
| 15 | +import sys |
| 16 | +import tempfile |
| 17 | +import urllib.request |
| 18 | +from pathlib import Path |
| 19 | +from typing import Optional, Tuple |
| 20 | + |
| 21 | + |
| 22 | +GITHUB_API_URL = "https://api.github.com/repos/89luca89/distrobox/releases/latest" |
| 23 | +DISTROBOX_DOWNLOADER_PATH = Path(__file__).parent.parent / "src" / "distrobox_downloader.rs" |
| 24 | + |
| 25 | + |
| 26 | +def fetch_latest_release() -> Tuple[str, str]: |
| 27 | + """ |
| 28 | + Fetches the latest Distrobox release information from GitHub. |
| 29 | + |
| 30 | + Returns: |
| 31 | + Tuple of (version, download_url) |
| 32 | + """ |
| 33 | + print("Fetching latest Distrobox release from GitHub...", file=sys.stderr) |
| 34 | + |
| 35 | + req = urllib.request.Request( |
| 36 | + GITHUB_API_URL, |
| 37 | + headers={"Accept": "application/vnd.github+json"} |
| 38 | + ) |
| 39 | + |
| 40 | + with urllib.request.urlopen(req) as response: |
| 41 | + data = json.loads(response.read().decode('utf-8')) |
| 42 | + |
| 43 | + tag_name = data['tag_name'] |
| 44 | + # Remove 'v' prefix if present |
| 45 | + version = tag_name.lstrip('v') |
| 46 | + |
| 47 | + # Construct download URL |
| 48 | + download_url = f"https://github.com/89luca89/distrobox/archive/refs/tags/{tag_name}.tar.gz" |
| 49 | + |
| 50 | + print(f"Latest release: {version}", file=sys.stderr) |
| 51 | + print(f"Download URL: {download_url}", file=sys.stderr) |
| 52 | + |
| 53 | + return version, download_url |
| 54 | + |
| 55 | + |
| 56 | +def compute_sha256(url: str) -> str: |
| 57 | + """ |
| 58 | + Downloads a file and computes its SHA256 hash. |
| 59 | + |
| 60 | + Args: |
| 61 | + url: URL of the file to download |
| 62 | + |
| 63 | + Returns: |
| 64 | + The SHA256 hash as a hexadecimal string |
| 65 | + """ |
| 66 | + print(f"Downloading {url} to compute hash...", file=sys.stderr) |
| 67 | + |
| 68 | + sha256_hash = hashlib.sha256() |
| 69 | + |
| 70 | + with urllib.request.urlopen(url) as response: |
| 71 | + # Read in chunks to handle large files |
| 72 | + while chunk := response.read(8192): |
| 73 | + sha256_hash.update(chunk) |
| 74 | + |
| 75 | + hash_hex = sha256_hash.hexdigest() |
| 76 | + print(f"SHA256: {hash_hex}", file=sys.stderr) |
| 77 | + |
| 78 | + return hash_hex |
| 79 | + |
| 80 | + |
| 81 | +def update_rust_constants(version: str, sha256: str) -> None: |
| 82 | + """ |
| 83 | + Updates the version and SHA256 constants in the Rust source file. |
| 84 | + |
| 85 | + Args: |
| 86 | + version: The new version string |
| 87 | + sha256: The new SHA256 hash |
| 88 | + """ |
| 89 | + if not DISTROBOX_DOWNLOADER_PATH.exists(): |
| 90 | + raise FileNotFoundError(f"Could not find {DISTROBOX_DOWNLOADER_PATH}") |
| 91 | + |
| 92 | + print(f"Updating {DISTROBOX_DOWNLOADER_PATH}...", file=sys.stderr) |
| 93 | + |
| 94 | + content = DISTROBOX_DOWNLOADER_PATH.read_text() |
| 95 | + |
| 96 | + # Update version |
| 97 | + content = re.sub( |
| 98 | + r'pub const DISTROBOX_VERSION: &str = "[^"]+";', |
| 99 | + f'pub const DISTROBOX_VERSION: &str = "{version}";', |
| 100 | + content |
| 101 | + ) |
| 102 | + |
| 103 | + # Update SHA256 |
| 104 | + content = re.sub( |
| 105 | + r'pub const DISTROBOX_SHA256: &str =\s*"[^"]+";', |
| 106 | + f'pub const DISTROBOX_SHA256: &str =\n "{sha256}";', |
| 107 | + content |
| 108 | + ) |
| 109 | + |
| 110 | + DISTROBOX_DOWNLOADER_PATH.write_text(content) |
| 111 | + |
| 112 | + print("✓ Updated successfully", file=sys.stderr) |
| 113 | + |
| 114 | + |
| 115 | +def get_current_version() -> Optional[str]: |
| 116 | + """ |
| 117 | + Reads the current version from the Rust source file. |
| 118 | + |
| 119 | + Returns: |
| 120 | + The current version string or None if not found |
| 121 | + """ |
| 122 | + if not DISTROBOX_DOWNLOADER_PATH.exists(): |
| 123 | + return None |
| 124 | + |
| 125 | + content = DISTROBOX_DOWNLOADER_PATH.read_text() |
| 126 | + match = re.search(r'pub const DISTROBOX_VERSION: &str = "([^"]+)";', content) |
| 127 | + |
| 128 | + return match.group(1) if match else None |
| 129 | + |
| 130 | + |
| 131 | +def main() -> int: |
| 132 | + """Main entry point.""" |
| 133 | + try: |
| 134 | + current_version = get_current_version() |
| 135 | + if current_version: |
| 136 | + print(f"Current bundled version: {current_version}", file=sys.stderr) |
| 137 | + |
| 138 | + version, download_url = fetch_latest_release() |
| 139 | + |
| 140 | + if current_version == version: |
| 141 | + print(f"Already up to date (version {version})", file=sys.stderr) |
| 142 | + return 0 |
| 143 | + |
| 144 | + sha256 = compute_sha256(download_url) |
| 145 | + update_rust_constants(version, sha256) |
| 146 | + |
| 147 | + print(f"\n✓ Updated from {current_version} to {version}", file=sys.stderr) |
| 148 | + print(f" Version: {version}") |
| 149 | + print(f" SHA256: {sha256}") |
| 150 | + |
| 151 | + return 0 |
| 152 | + |
| 153 | + except Exception as e: |
| 154 | + print(f"Error: {e}", file=sys.stderr) |
| 155 | + return 1 |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + sys.exit(main()) |
0 commit comments