Created
November 14, 2025 12:16
-
-
Save larsr/675b873e1b01503517865d6477590237 to your computer and use it in GitHub Desktop.
bash script to run python modules with uvx. it installs uvx if needed, and can start inside a docker instance with DOCKER=1. rename it to set which python module to run, ie. ipython
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # | |
| # This command runs python modules with uvx. | |
| # Rename or make a link to this script to decide what module to run. | |
| # Example: | |
| # ln -s quick-command.sh ipython; ./ipython | |
| # If you set DOCKER=1 it will run them inside an alpine linux instance. | |
| # ln -s quick-command.sh ipython; DOCKER=1 ./ipython | |
| # Don't be shy to modify the uvx command at the bottom if you need i.e. some more libraries! | |
| # | |
| set -euo pipefail | |
| CACHE_DIR="${HOME}/.cache/quick-command" | |
| FALLBACK_DIR="/tmp/quick-command" | |
| make_writable_dir_at() ( | |
| mkdir -p "$1" 2>/dev/null || return 1 | |
| [ -w "$1" ] || return 1 | |
| echo "$1" | |
| ) | |
| get_cache_dir() ( | |
| make_writable_dir_at "$CACHE_DIR" 2>/dev/null || make_writable_dir_at "$FALLBACK_DIR" | |
| ) | |
| put_in_cache() ( | |
| dest="$(get_cache_dir)/$2" | |
| cp "$1" "$dest" | |
| echo "$dest" | |
| ) | |
| lookup_cache() ( | |
| dir="$(get_cache_dir)" || return 1 | |
| [ -f "$dir/$1" ] && echo "$dir/$1" | |
| ) | |
| fetch_url() ( | |
| echo FETCHING "$1" > /dev/stderr | |
| fetch_with_curl "$1" "$2" || fetch_with_wget "$1" "$2" ) | |
| valid_command () ( command -v "$1" >/dev/null 2>&1 ) | |
| valid_command_echo () ( valid_command "$1" && echo "$1") | |
| fetch_with_curl() ( valid_command curl && curl -fsSL "$1" -o "$2" ) | |
| fetch_with_wget() ( valid_command wget && wget -q "$1" -O "$2" ) | |
| uv_arch() ( | |
| case "$(uname -m)" in | |
| x86_64) base="x86_64-unknown-linux" ;; | |
| aarch64) base="aarch64-unknown-linux" ;; | |
| *) return 1 ;; | |
| esac | |
| if (ldd 2>&1 || true) | grep -qi musl; then | |
| echo "${base}-musl" | |
| else | |
| echo "${base}-gnu" | |
| fi | |
| ) | |
| UV_BINARY_URL() ( | |
| echo "https://github.com/astral-sh/uv/releases/download/0.9.9/uv-$(uv_arch).tar.gz" | |
| ) | |
| fetch_uv() ( | |
| set -e | |
| tmpdir="$(mktemp -d)" | |
| archive="$tmpdir/archive.tar.gz" | |
| cachedir="$(get_cache_dir)" | |
| url="$(UV_BINARY_URL)" | |
| echo "url=$url" > /dev/stderr | |
| fetch_url "$url" "$archive" | |
| tar -xf "$archive" -C "$cachedir" --strip-components=1 --wildcards '*/uv' '*/uvx' > /dev/stderr | |
| find "$cachedir" > /dev/stderr | |
| rm -rf "$tmpdir" | |
| uvbin="$cachedir/uv" | |
| uvxbin="$cachedir/uvx" | |
| chmod +x "$uvbin" "$uvxbin" | |
| echo "$uvxbin" | |
| ) | |
| scriptname="$(basename $0)" | |
| if [ "${DOCKER:-}" = "1" ]; then | |
| docker run \ | |
| --rm \ | |
| -v "$PWD/${scriptname}:/${scriptname}" \ | |
| -ti \ | |
| alpine:3.20 \ | |
| sh -c "apk add bash tar curl; /${scriptname}" | |
| else | |
| UVX=$(valid_command_echo uvx || valid_command_echo "$(get_cache_dir)/uvx" || fetch_uv) | |
| "$UVX" "$scriptname" "${@:1}" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment