From 26e0d749d75b602e49da4f78ef282fdb2c17692a Mon Sep 17 00:00:00 2001 From: nose Date: Wed, 24 Dec 2025 03:09:59 -0800 Subject: [PATCH] installer: embed repo path in global 'mm' launcher and fall back to locating repo from CWD; prefer venv's python3 --- scripts/bootstrap.sh | 29 +++++++++++++++++++---------- scripts/setup.py | 20 +++++++++++++------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 5931002..1396be7 100644 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -416,18 +416,23 @@ cat > "$USER_BIN/mm" <<'MM' #!/usr/bin/env bash 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 +# REPO is injected at install time; if it doesn't look like a project, try to +# find the repo by walking up from the current working directory. +REPO="__REPO__" +if [ ! -f "$REPO/CLI.py" ] && [ ! -f "$REPO/pyproject.toml" ] && [ ! -f "$REPO/setup.py" ]; then + CUR="$(pwd -P)" + while [ "$CUR" != "/" ] && [ "$CUR" != "" ]; do + if [ -f "$CUR/CLI.py" ] || [ -f "$CUR/pyproject.toml" ] || [ -f "$CUR/setup.py" ]; then + REPO="$CUR" + break + fi + CUR="$(dirname "$CUR")" + done 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 +# Packaged console script in the venv if available if [ -x "$VENV/bin/mm" ]; then exec "$VENV/bin/mm" "$@" fi @@ -440,7 +445,7 @@ 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) +# Fallback to system python3, then system python (only if it's Python 3) if command -v python3 >/dev/null 2>&1; then exec python3 -m medeia_macina.cli_entry "$@" fi @@ -453,6 +458,10 @@ 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 + +# Inject absolute repo path into the installed script so global launcher prefers the project venv +escaped_repo=$(printf '%s' "$REPO" | sed -e 's/[\/&]/\\&/g') +sed -i "s|__REPO__|$escaped_repo|g" "$USER_BIN/mm" || true 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 7181714..6084cf9 100644 --- a/scripts/setup.py +++ b/scripts/setup.py @@ -363,14 +363,20 @@ python $cli @args sh_text = ( "#!/usr/bin/env bash\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" + f"REPO=\"{repo}\"\n" + "# If the packaged REPO does not look valid at runtime, try to locate the repo by walking\n" + "# up from the current working directory so the launcher works when executed inside the\n" + "# project tree or when the global bin is symlinked into the repo.\n" + "if [ ! -f \"$REPO/CLI.py\" ] && [ ! -f \"$REPO/pyproject.toml\" ] && [ ! -f \"$REPO/setup.py\" ]; then\n" + " CUR=\"$(pwd -P)\"\n" + " while [ \"$CUR\" != \"/\" ] && [ \"$CUR\" != \"\" ]; do\n" + " if [ -f \"$CUR/CLI.py\" ] || [ -f \"$CUR/pyproject.toml\" ] || [ -f \"$CUR/setup.py\" ]; then\n" + " REPO=\"$CUR\"\n" + " break\n" + " fi\n" + " CUR=\"$(dirname \"$CUR\")\"\n" + " done\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"