From 4c8f267e5989401d161297d0aa5b67e446db37b4 Mon Sep 17 00:00:00 2001 From: tremor021 Date: Wed, 4 Jun 2025 11:56:46 +0200 Subject: [PATCH] Update setup_uv --- misc/tools.func | 99 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 27 deletions(-) diff --git a/misc/tools.func b/misc/tools.func index f8172690..983798ca 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -989,63 +989,108 @@ function download_with_progress() { # ------------------------------------------------------------------------------ # Installs or upgrades uv (Python package manager) from GitHub releases. -# -# Description: -# - Downloads architecture-specific tarball -# - Places binary in /usr/local/bin +# - Downloads platform-specific tarball (no install.sh!) +# - Extracts uv binary +# - Places it in /usr/local/bin +# - Optionally installs a specific Python version via uv # ------------------------------------------------------------------------------ function setup_uv() { $STD msg_info "Checking uv installation..." - UV_BIN="/usr/local/bin/uv" + local UV_BIN="/usr/local/bin/uv" + local TMP_DIR TMP_DIR=$(mktemp -d) - ARCH=$(uname -m) - if [[ "$ARCH" == "x86_64" ]]; then - UV_TAR="uv-x86_64-unknown-linux-gnu.tar.gz" - elif [[ "$ARCH" == "aarch64" ]]; then - UV_TAR="uv-aarch64-unknown-linux-gnu.tar.gz" - else + # Determine system architecture + local ARCH + ARCH=$(uname -m) + local UV_TAR + + case "$ARCH" in + x86_64) UV_TAR="uv-x86_64-unknown-linux-gnu.tar.gz" ;; + aarch64) UV_TAR="uv-aarch64-unknown-linux-gnu.tar.gz" ;; + *) msg_error "Unsupported architecture: $ARCH" rm -rf "$TMP_DIR" return 1 - fi + ;; + esac + + # Get latest version from GitHub + local LATEST_VERSION + LATEST_VERSION=$(curl -fsSL https://api.github.com/repos/astral-sh/uv/releases/latest | + grep '"tag_name":' | cut -d '"' -f4 | sed 's/^v//') - # get current github version - LATEST_VERSION=$(curl -s https://api.github.com/repos/astral-sh/uv/releases/latest | grep '"tag_name":' | cut -d '"' -f4 | sed 's/^v//') if [[ -z "$LATEST_VERSION" ]]; then msg_error "Could not fetch latest uv version from GitHub." rm -rf "$TMP_DIR" return 1 fi - # check if uv exists + # Check if uv is already up to date if [[ -x "$UV_BIN" ]]; then + local INSTALLED_VERSION INSTALLED_VERSION=$($UV_BIN -V | awk '{print $2}') if [[ "$INSTALLED_VERSION" == "$LATEST_VERSION" ]]; then $STD msg_ok "uv is already at the latest version ($INSTALLED_VERSION)" rm -rf "$TMP_DIR" - # set path - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - export PATH="/usr/local/bin:$PATH" - fi + [[ ":$PATH:" != *":/usr/local/bin:"* ]] && export PATH="/usr/local/bin:$PATH" return 0 else $STD msg_info "Updating uv from $INSTALLED_VERSION to $LATEST_VERSION" fi else - $STD msg_info "uv not found. Installing version $LATEST_VERSION" + $STD msg_info "Installing uv $LATEST_VERSION" fi - # install or update uv - curl -fsSL "https://github.com/astral-sh/uv/releases/latest/download/${UV_TAR}" -o "$TMP_DIR/uv.tar.gz" - tar -xzf "$TMP_DIR/uv.tar.gz" -C "$TMP_DIR" - install -m 755 "$TMP_DIR"/*/uv "$UV_BIN" - rm -rf "$TMP_DIR" + # Download and install manually + local UV_URL="https://github.com/astral-sh/uv/releases/latest/download/${UV_TAR}" + if ! curl -fsSL "$UV_URL" -o "$TMP_DIR/uv.tar.gz"; then + msg_error "Failed to download $UV_URL" + rm -rf "$TMP_DIR" + return 1 + fi - # set path + if ! tar -xzf "$TMP_DIR/uv.tar.gz" -C "$TMP_DIR"; then + msg_error "Failed to extract uv archive" + rm -rf "$TMP_DIR" + return 1 + fi + + install -m 755 "$TMP_DIR"/*/uv "$UV_BIN" || { + msg_error "Failed to install uv binary" + rm -rf "$TMP_DIR" + return 1 + } + + rm -rf "$TMP_DIR" ensure_usr_local_bin_persist - msg_ok "uv installed/updated to $LATEST_VERSION" + msg_ok "uv $LATEST_VERSION installed" + + # Optional: install specific Python version + if [[ -n "${PYTHON_VERSION:-}" ]]; then + $STD msg_info "Ensuring Python $PYTHON_VERSION is available via uv..." + + local VERSION_MATCH + VERSION_MATCH=$(uv python list --only-downloads | + grep -E "^cpython-${PYTHON_VERSION//./\\.}\.[0-9]+-linux" | + cut -d'-' -f2 | sort -V | tail -n1) + + if [[ -z "$VERSION_MATCH" ]]; then + msg_error "No matching Python $PYTHON_VERSION.x version found via uv" + return 1 + fi + + if uv python list | grep -q "cpython-${VERSION_MATCH}-linux.*uv/python"; then + $STD msg_ok "Python $VERSION_MATCH already installed via uv" + else + if ! $STD uv python install "$VERSION_MATCH"; then + msg_error "Failed to install Python $VERSION_MATCH via uv" + return 1 + fi + msg_ok "Installed Python $VERSION_MATCH via uv" + fi + fi } # ------------------------------------------------------------------------------