installer: make 'mm' launcher determine repo/python at runtime and prefer venv's python3 before system python
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
This commit is contained in:
@@ -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" <<EOF
|
||||
cat > "$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
|
||||
EOF
|
||||
fi
|
||||
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.
|
||||
|
||||
@@ -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())}")
|
||||
|
||||
Reference in New Issue
Block a user