bootstrap: add automatic .pth write for editable installs so top-level CLI is importable
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled

This commit is contained in:
nose
2025-12-24 04:05:35 -08:00
parent 7598fd5b2a
commit 7c8b0edb5c
4 changed files with 211 additions and 9 deletions

View File

@@ -285,19 +285,107 @@ if [[ "$NOINSTALL" != "true" ]]; then # If not explicitly requested, auto-selec
# Additional compatibility check: top-level 'CLI' module may be required by
# older entrypoints or direct imports when running from a development checkout.
if ! "$VENV_PY" -c 'import importlib,sys; importlib.import_module("CLI")' >/dev/null 2>&1; then
echo "Note: top-level 'CLI' module not importable; some entrypoints expect it." >&2
echo "Note: top-level 'CLI' module not importable; attempting to add the repo root to venv site-packages via a .pth file so top-level imports work." >&2
# If this appears to be a development checkout, offer to install editable mode
if [[ -d "$REPO/.git" ]] || git -C "$REPO" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
if [[ "$EDITABLE" == "true" ]]; then
echo "Installing project in editable mode to provide top-level 'CLI'..."
"$VENV_PY" -m pip install -e "$REPO" || { echo "Editable install failed" >&2; exit 6; }
# Try to discover venv site-packages and write a .pth pointing at the repo root
site_pkgs=$("$VENV_PY" - <<'PY'
import site, sysconfig
out=[]
try:
out.extend(site.getsitepackages())
except Exception:
pass
try:
p = sysconfig.get_paths().get('purelib')
if p:
out.append(p)
except Exception:
pass
seen=set(); res=[]
for x in out:
if x and x not in seen:
seen.add(x); res.append(x)
for s in res:
print(s)
PY
)
site_pkg_dir=""
while IFS= read -r sp; do
if [[ -d "$sp" ]]; then site_pkg_dir="$sp"; break; fi
done <<< "$site_pkgs"
if [[ -n "$site_pkg_dir" ]]; then
pth_file="$site_pkg_dir/medeia_repo.pth"
if [[ -f "$pth_file" ]]; then
if grep -qxF "$REPO" "$pth_file" >/dev/null 2>&1; then
echo ".pth already present and contains repo root: $pth_file" >&2
else
echo "$REPO" >> "$pth_file"
echo "Appended repo root to existing .pth: $pth_file" >&2
fi
else
if [[ "$QUIET" != "true" && -t 0 ]]; then
read -p "Top-level 'CLI' not importable; install project in editable mode now? (Y/n) " devans2
if [[ -z "$devans2" || "$devans2" == "y" || "$devans2" == "Y" ]]; then
echo "$REPO" > "$pth_file"
echo "Wrote .pth adding repo root to venv site-packages: $pth_file" >&2
fi
# Re-check whether 'CLI' is now importable
if "$VENV_PY" -c 'import importlib,sys; importlib.import_module("CLI")' >/dev/null 2>&1; then
echo "Top-level 'CLI' import works after adding .pth" >&2
else
echo "Adding .pth did not make top-level 'CLI' importable." >&2
# Fallback: if this is a git checkout, try editable reinstall or prompt user
if [[ -d "$REPO/.git" ]] || git -C "$REPO" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
if [[ "$EDITABLE" == "true" ]]; then
echo "Editable install already requested; attempting editable reinstall for good measure..." >&2
"$VENV_PY" -m pip install -e "$REPO" || { echo "Editable install failed" >&2; exit 6; }
if "$VENV_PY" -c 'import importlib,sys; importlib.import_module("CLI")' >/dev/null 2>&1; then
echo "Top-level 'CLI' is now importable after reinstall." >&2
else
echo "Editable reinstall did not make 'CLI' importable. Please inspect the venv or create an egg-link." >&2
exit 6
fi
else
if [[ "$QUIET" != "true" && -t 0 ]]; then
read -p "Top-level 'CLI' not importable; install project in editable mode now? (Y/n) " devans2
if [[ -z "$devans2" || "$devans2" == "y" || "$devans2" == "Y" ]]; then
"$VENV_PY" -m pip install -e "$REPO" || { echo "Editable install failed" >&2; exit 6; }
else
echo "Warning: continuing without top-level 'CLI' importable; some entrypoints may fail." >&2
fi
else
echo "Top-level 'CLI' not importable and cannot prompt (quiet mode); aborting." >&2
exit 6
fi
fi
else
echo "Top-level 'CLI' not importable and not a git checkout; continuing at your own risk." >&2
fi
fi
else
echo "Unable to determine site-packages directory to write .pth; skipping .pth fallback." >&2
if [[ -d "$REPO/.git" ]] || git -C "$REPO" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
if [[ "$EDITABLE" == "true" ]]; then
echo "Attempting editable install to provide top-level 'CLI'..."
"$VENV_PY" -m pip install -e "$REPO" || { echo "Editable install failed" >&2; exit 6; }
else
if [[ "$QUIET" != "true" && -t 0 ]]; then
read -p "Top-level 'CLI' not importable; install project in editable mode now? (Y/n) " devans2
if [[ -z "$devans2" || "$devans2" == "y" || "$devans2" == "Y" ]]; then
"$VENV_PY" -m pip install -e "$REPO" || { echo "Editable install failed" >&2; exit 6; }
else
echo "Warning: continuing without top-level 'CLI' importable; some entrypoints may fail." >&2
fi
else
echo "Top-level 'CLI' not importable and cannot prompt (quiet mode); aborting." >&2
exit 6
fi
fi
else
echo "Top-level 'CLI' not importable and not a git checkout; continuing." >&2
fi
fi
fi
echo "Continuing without editable install; 'mm' may not work as expected." >&2
fi
else