From 2208fdad9673dabc43b533b94f438ab92244a84a Mon Sep 17 00:00:00 2001 From: nose Date: Wed, 24 Dec 2025 03:06:16 -0800 Subject: [PATCH] installer: make 'mm' launcher determine repo/python at runtime and prefer venv's python3 before system python --- scripts/bootstrap.sh | 54 ++++++++++++++++++++++++++++++++------------ scripts/setup.py | 37 +++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 26 deletions(-) diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 9f64c60..5931002 100644 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -412,23 +412,47 @@ if [[ -f "$USER_BIN/mm" ]]; then echo "Backing up existing $USER_BIN/mm to $USER_BIN/mm.bak.$(date +%s)" mv "$USER_BIN/mm" "$USER_BIN/mm.bak.$(date +%s)" fi -cat > "$USER_BIN/mm" < "$USER_BIN/mm" <<'MM' #!/usr/bin/env bash -REPO="$REPO" -VENV="$REPO/.venv" -if [ -x "$VENV/bin/mm" ]; then - exec "$VENV/bin/mm" "\$@" -elif [ -x "$VENV/bin/python" ]; then - exec "$VENV/bin/python" -m medeia_macina.cli_entry "\$@" -elif command -v python3 >/dev/null 2>&1; then - exec python3 -m medeia_macina.cli_entry "\$@" -elif command -v python >/dev/null 2>&1; then - exec python -m medeia_macina.cli_entry "\$@" -else - echo "Error: no Python interpreter found (python3 or python). Activate the venv with 'source $VENV/bin/activate' or install system Python 3." >&2 - exit 127 +set -e + +# Resolve script path (follow symlinks if possible) and derive repo root dynamically +SCRIPT="$0" +if command -v readlink >/dev/null 2>&1; then + if readlink -f "$SCRIPT" >/dev/null 2>&1; then + SCRIPT="$(readlink -f "$SCRIPT")" + fi fi -EOF +SCRIPT_DIR="$(cd "$(dirname "$SCRIPT")" && pwd -P)" +REPO="$(cd "$SCRIPT_DIR/.." && pwd -P)" +VENV="$REPO/.venv" + +# Prefer a packaged console script in the venv +if [ -x "$VENV/bin/mm" ]; then + exec "$VENV/bin/mm" "$@" +fi + +# Prefer venv's python3, then venv's python +if [ -x "$VENV/bin/python3" ]; then + exec "$VENV/bin/python3" -m medeia_macina.cli_entry "$@" +fi +if [ -x "$VENV/bin/python" ]; then + exec "$VENV/bin/python" -m medeia_macina.cli_entry "$@" +fi + +# Fallback to system python3, then python (ensure python is v3) +if command -v python3 >/dev/null 2>&1; then + exec python3 -m medeia_macina.cli_entry "$@" +fi +if command -v python >/dev/null 2>&1; then + if python -c 'import sys; sys.exit(0 if sys.version_info[0] >= 3 else 1)'; then + exec python -m medeia_macina.cli_entry "$@" + fi +fi + +printf "Error: no suitable Python 3 interpreter found. Activate the venv with 'source %s/bin/activate' or install Python 3.\n" "$VENV" >&2 +exit 127 +MM chmod +x "$USER_BIN/mm" # Quick verification of the global launcher; helps catch packaging issues early. diff --git a/scripts/setup.py b/scripts/setup.py index ca3ac43..7181714 100644 --- a/scripts/setup.py +++ b/scripts/setup.py @@ -362,20 +362,35 @@ python $cli @args mm_sh = user_bin / "mm" sh_text = ( "#!/usr/bin/env bash\n" - f"REPO=\"{repo}\"\n" - f"VENV=\"{repo}/.venv\"\n" + "set -e\n" + "SCRIPT=\"$0\"\n" + "if command -v readlink >/dev/null 2>&1; then\n" + " if readlink -f \"$SCRIPT\" >/dev/null 2>&1; then\n" + " SCRIPT=\"$(readlink -f \"$SCRIPT\")\"\n" + " fi\n" + "fi\n" + "SCRIPT_DIR=\"$(cd \"$(dirname \"$SCRIPT\")\" && pwd -P)\"\n" + "REPO=\"$(cd \"$SCRIPT_DIR/..\" && pwd -P)\"\n" + "VENV=\"$REPO/.venv\"\n" "if [ -x \"$VENV/bin/mm\" ]; then\n" " exec \"$VENV/bin/mm\" \"$@\"\n" - "elif [ -x \"$VENV/bin/python\" ]; then\n" - " exec \"$VENV/bin/python\" -m medeia_macina.cli_entry \"$@\"\n" - "elif command -v python3 >/dev/null 2>&1; then\n" - " exec python3 -m medeia_macina.cli_entry \"$@\"\n" - "elif command -v python >/dev/null 2>&1; then\n" - " exec python -m medeia_macina.cli_entry \"$@\"\n" - "else\n" - " echo 'Error: no Python interpreter (python3 or python) found in PATH. Please install Python 3 or use the venv.' >&2\n" - " exit 127\n" "fi\n" + "if [ -x \"$VENV/bin/python3\" ]; then\n" + " exec \"$VENV/bin/python3\" -m medeia_macina.cli_entry \"$@\"\n" + "fi\n" + "if [ -x \"$VENV/bin/python\" ]; then\n" + " exec \"$VENV/bin/python\" -m medeia_macina.cli_entry \"$@\"\n" + "fi\n" + "if command -v python3 >/dev/null 2>&1; then\n" + " exec python3 -m medeia_macina.cli_entry \"$@\"\n" + "fi\n" + "if command -v python >/dev/null 2>&1; then\n" + " if python -c 'import sys; sys.exit(0 if sys.version_info[0] >= 3 else 1)'; then\n" + " exec python -m medeia_macina.cli_entry \"$@\"\n" + " fi\n" + "fi\n" + "echo 'Error: no suitable Python 3 interpreter found. Please install Python 3 or use the venv.' >&2\n" + "exit 127\n" ) if mm_sh.exists(): bak = mm_sh.with_suffix(f".bak{int(time.time())}")