|
| 1 | +#!/bin/sh |
| 2 | +# /qompassai/python/scripts/quickstart.sh |
| 3 | +# Qompass AI Python Quick Start + pyenv/ruff/uv + config |
| 4 | +# Copyright (C) 2025 Qompass AI, All rights reserved |
| 5 | +######################################################### |
| 6 | +set -eu |
| 7 | +IFS=' |
| 8 | +' |
| 9 | +XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" |
| 10 | +LOCAL_PREFIX="$HOME/.local" |
| 11 | +BIN_DIR="$LOCAL_PREFIX/bin" |
| 12 | +LIB_DIR="$LOCAL_PREFIX/lib" |
| 13 | +SHARE_DIR="$LOCAL_PREFIX/share" |
| 14 | +SRC_DIR="$HOME/.local/src/python" |
| 15 | +PYENV_ROOT="$HOME/.pyenv" |
| 16 | +mkdir -p "$BIN_DIR" "$LIB_DIR" "$SHARE_DIR" "$SRC_DIR" "$XDG_CONFIG_HOME/python" |
| 17 | +case ":$PATH:" in |
| 18 | +*":$BIN_DIR:"*) ;; |
| 19 | +*) export PATH="$BIN_DIR:$PATH" ;; |
| 20 | +esac |
| 21 | +NEEDED_TOOLS="git curl tar make gcc clang bzip2 xz unzip openssl zlib gdbm readline sqlite3 libffi tk tcl build-essential libbz2-dev libssl-dev libreadline-dev libsqlite3-dev zlib1g-dev libffi-dev" |
| 22 | +MISSING="" |
| 23 | +for tool in $NEEDED_TOOLS; do |
| 24 | + if ! command -v "$tool" >/dev/null 2>&1; then |
| 25 | + if [ -x "/usr/bin/$tool" ]; then |
| 26 | + ln -sf "/usr/bin/$tool" "$BIN_DIR/$tool" |
| 27 | + echo " → Added symlink for $tool in $BIN_DIR (not originally in PATH)" |
| 28 | + else |
| 29 | + MISSING="$MISSING $tool" |
| 30 | + fi |
| 31 | + fi |
| 32 | +done |
| 33 | +if [ -n "$MISSING" ]; then |
| 34 | + echo "⚠ Warning: These tools/libraries are missing (not found or not symlinkable):$MISSING" |
| 35 | + echo "Please install them with your package manager (e.g. pacman, apt, dnf) to continue." |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | +PY_VERS="3.13.5" |
| 39 | +PY_MAJ="3.13" |
| 40 | +printf '╭────────────────────────────────────────────╮\n' |
| 41 | +printf '│ Qompass AI · Python Quick‑Start │\n' |
| 42 | +printf '╰────────────────────────────────────────────╯\n' |
| 43 | +printf ' © 2025 Qompass AI. All rights reserved \n\n' |
| 44 | +echo "Which Python do you want to build?" |
| 45 | +echo " 1) Classic CPython (${PY_VERS})" |
| 46 | +echo " 2) Free-threaded (GIL-free, experimental, --enable-free-threaded-interpreter)" |
| 47 | +echo " q) Quit" |
| 48 | +printf "Choose [1]: " |
| 49 | +read -r ans |
| 50 | +[ -z "$ans" ] && ans=1 |
| 51 | +[ "$ans" = "q" ] && exit 0 |
| 52 | +FREE_THREADED="no" |
| 53 | +if [ "$ans" = "2" ]; then |
| 54 | + FREE_THREADED="yes" |
| 55 | +fi |
| 56 | +cd "$SRC_DIR" |
| 57 | +if [ ! -d "cpython" ]; then |
| 58 | + echo "→ Cloning Python source (cpython)..." |
| 59 | + git clone --branch "v$PY_VERS" https://github.com/python/cpython.git |
| 60 | +fi |
| 61 | +cd cpython |
| 62 | +git fetch origin |
| 63 | +git checkout "v$PY_VERS" |
| 64 | +git clean -fdx |
| 65 | +echo "→ Configuring Python build..." |
| 66 | +CONFIG_FLAGS="--prefix=$LOCAL_PREFIX --enable-optimizations --with-lto" |
| 67 | +if [ "$FREE_THREADED" = "yes" ]; then |
| 68 | + CONFIG_FLAGS="$CONFIG_FLAGS --enable-free-threaded-interpreter" |
| 69 | +fi |
| 70 | +./configure "$CONFIG_FLAGS" |
| 71 | +echo "→ Building Python (this may take several minutes, enabling PGO/LTO optimization)..." |
| 72 | +make -j"$(nproc)" |
| 73 | +echo "→ Installing Python (no sudo needed)..." |
| 74 | +make install |
| 75 | +case ":$PATH:" in |
| 76 | +*":$BIN_DIR:"*) ;; |
| 77 | +*) export PATH="$BIN_DIR:$PATH" ;; |
| 78 | +esac |
| 79 | +add_path_to_shell_rc() { |
| 80 | + rcfile=$1 |
| 81 | + line="export PATH=\"$BIN_DIR:\$PATH\"" |
| 82 | + if [ -f "$rcfile" ]; then |
| 83 | + if ! grep -Fxq "$line" "$rcfile"; then |
| 84 | + printf '\n# Added by Qompass AI Python quickstart script\n%s\n' "$line" >>"$rcfile" |
| 85 | + echo " → Added PATH export to $rcfile" |
| 86 | + fi |
| 87 | + fi |
| 88 | +} |
| 89 | +add_path_to_shell_rc "$HOME/.bashrc" |
| 90 | +add_path_to_shell_rc "$HOME/.zshrc" |
| 91 | +add_path_to_shell_rc "$HOME/.profile" |
| 92 | +PIP_PATH="$BIN_DIR/pip$PY_MAJ" |
| 93 | +PYTHON_PATH="$BIN_DIR/python$PY_MAJ" |
| 94 | +echo "→ Upgrading pip and installing core wheels..." |
| 95 | +"$PYTHON_PATH" -m ensurepip --upgrade |
| 96 | +"$PYTHON_PATH" -m pip install --upgrade pip wheel setuptools |
| 97 | +echo |
| 98 | +printf "Do you want to install \033[1mpyenv\033[0m for managing multiple Pythons? [Y/n]: " |
| 99 | +read -r ans |
| 100 | +[ -z "$ans" ] && ans="Y" |
| 101 | +if [ "$ans" = "Y" ] || [ "$ans" = "y" ]; then |
| 102 | + if [ ! -d "$PYENV_ROOT" ]; then |
| 103 | + curl -fsSL https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash |
| 104 | + # Add pyenv to shell |
| 105 | + for rc in "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.profile"; do |
| 106 | + if [ -f "$rc" ]; then |
| 107 | + if ! grep -q "pyenv init" "$rc"; then |
| 108 | + printf '\n# Pyenv config\nexport PYENV_ROOT="%s"\nexport PATH="$PYENV_ROOT/bin:$PATH"\neval "$(pyenv init --path)"\n' "$PYENV_ROOT" >>"$rc" |
| 109 | + echo " → Added pyenv setup to $rc" |
| 110 | + fi |
| 111 | + fi |
| 112 | + done |
| 113 | + else |
| 114 | + echo "→ pyenv already present." |
| 115 | + fi |
| 116 | +fi |
| 117 | +echo |
| 118 | +printf "Do you want to install \033[1mruff\033[0m (fast Python linter)? [Y/n]: " |
| 119 | +read -r ans |
| 120 | +[ -z "$ans" ] && ans="Y" |
| 121 | +if [ "$ans" = "Y" ] || [ "$ans" = "y" ]; then |
| 122 | + "$PIP_PATH" install --user ruff |
| 123 | + echo "→ ruff installed via pip" |
| 124 | +fi |
| 125 | +echo |
| 126 | +printf "Do you want to install \033[1muv\033[0m (pip replacement and package manager)? [Y/n]: " |
| 127 | +read -r ans |
| 128 | +[ -z "$ans" ] && ans="Y" |
| 129 | +if [ "$ans" = "Y" ] || [ "$ans" = "y" ]; then |
| 130 | + if command -v pipx >/dev/null 2>&1; then |
| 131 | + pipx install uv || "$PIP_PATH" install --user uv |
| 132 | + else |
| 133 | + "$PIP_PATH" install --user uv |
| 134 | + fi |
| 135 | + echo "→ uv installed" |
| 136 | +fi |
| 137 | +create_xdg_config() { |
| 138 | + tool="$1" |
| 139 | + default_content="$2" |
| 140 | + confdir="$XDG_CONFIG_HOME/$tool" |
| 141 | + confpath="$confdir/config.toml" |
| 142 | + mkdir -p "$confdir" |
| 143 | + if [ -f "$confpath" ]; then |
| 144 | + echo "→ $tool config already exists at $confpath" |
| 145 | + return |
| 146 | + fi |
| 147 | + printf "Do you want to write an example config for $tool to %s? [Y/n]: " "$confpath" |
| 148 | + read -r ans |
| 149 | + [ -z "$ans" ] && ans="Y" |
| 150 | + if [ "$ans" = "Y" ] || [ "$ans" = "y" ]; then |
| 151 | + echo "→ Creating example $tool config at $confpath" |
| 152 | + printf "%s\n" "$default_content" >"$confpath" |
| 153 | + fi |
| 154 | +} |
| 155 | +RUFF_CFG='[lint]\nselect = ["E", "F", "W"] # Example: style, errors, warnings' |
| 156 | +UV_CFG='[uv]\npypi_mirror = "https://pypi.org/simple"\ncache_dir = "~/.cache/uv"\n' |
| 157 | +PYTHON_CFG='[startup]\n# Put any sitecustomize or startup hooks here\n' |
| 158 | +create_xdg_config "ruff" "$RUFF_CFG" |
| 159 | +create_xdg_config "uv" "$UV_CFG" |
| 160 | +create_xdg_config "python" "$PYTHON_CFG" |
| 161 | +echo |
| 162 | +echo "✅ Python $PY_VERS has been built and installed in $BIN_DIR" |
| 163 | +if [ "$FREE_THREADED" = "yes" ]; then |
| 164 | + echo " (Free-threaded interpreter enabled!)" |
| 165 | +fi |
| 166 | +echo "→ Test it with: $PYTHON_PATH --version" |
| 167 | +echo "→ Your pip is: $PIP_PATH" |
| 168 | +echo "→ pyenv (if installed) is in \$HOME/.pyenv; add to your PATH if desired." |
| 169 | +echo "→ ruff and uv are installed in ~/.local/bin (and can be configured in $XDG_CONFIG_HOME/)" |
| 170 | +echo "→ All binaries/libs/configs are under ~/.local/, ~/.pyenv/, ~/.config/" |
| 171 | +echo "→ Add '$BIN_DIR' to your shell \$PATH if not already present." |
| 172 | +echo "→ For custom packages, use: $PIP_PATH install --user ..." |
| 173 | +echo "→ To uninstall, just rm -rf $LOCAL_PREFIX/{bin/lib/share} $SRC_DIR/cpython ~/.pyenv ~/.cache/ruff ~/.cache/uv $XDG_CONFIG_HOME/ruff $XDG_CONFIG_HOME/uv" |
| 174 | +echo "─ Ready, Set, Python! ─" |
| 175 | +exit 0 |
0 commit comments