|
| 1 | +# script/reinstall_dev.py |
| 2 | +""" |
| 3 | +Reinstall multiple local libraries in editable mode for development. |
| 4 | +
|
| 5 | +Workflow: |
| 6 | + 1) Try to uninstall all target libraries in one command (ignore errors if some are not installed). |
| 7 | + 2) Reinstall each library in editable mode from a sibling folder: ../<library>. |
| 8 | +
|
| 9 | +This script uses the same Python interpreter that runs it (sys.executable), |
| 10 | +so pip operations happen in the same environment (e.g., your active venv). |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import os |
| 16 | +import shutil |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | +import sysconfig |
| 20 | +from typing import Iterable |
| 21 | + |
| 22 | +# Absolute path to the Python executable that runs this script. |
| 23 | +# Using sys.executable ensures pip targets the same environment (e.g., your venv). |
| 24 | +PY = sys.executable |
| 25 | + |
| 26 | + |
| 27 | +def run(cmd: list[str]) -> None: |
| 28 | + """ |
| 29 | + Echo and execute a subprocess command. |
| 30 | + Raises: |
| 31 | + subprocess.CalledProcessError: if the command returns a non-zero exit code. |
| 32 | + """ |
| 33 | + print("$", " ".join(cmd), flush=True) |
| 34 | + subprocess.check_call(cmd) |
| 35 | + |
| 36 | + |
| 37 | +def uninstall_many(packages: Iterable[str]) -> None: |
| 38 | + """ |
| 39 | + Attempt to uninstall multiple packages at once. |
| 40 | + If uninstall fails (e.g., a package is not installed), we keep going. |
| 41 | +
|
| 42 | + Args: |
| 43 | + packages: An iterable of package names (pip distribution names). |
| 44 | + """ |
| 45 | + pkgs = list(packages) |
| 46 | + if not pkgs: |
| 47 | + return |
| 48 | + try: |
| 49 | + run([PY, "-m", "pip", "uninstall", "-y", *pkgs]) |
| 50 | + except subprocess.CalledProcessError as e: |
| 51 | + # Continue even if the uninstall step fails (for one or more packages) |
| 52 | + print(f"[WARN] Uninstall returned {e.returncode} — continuing...", flush=True) |
| 53 | + |
| 54 | + |
| 55 | +def install_editable_many(packages: Iterable[str]) -> None: |
| 56 | + """ |
| 57 | + Install each package in editable mode from ../<package_dir>. |
| 58 | +
|
| 59 | + Assumes your project layout has sibling folders one level up, e.g.: |
| 60 | + ../guidata |
| 61 | +
|
| 62 | + Args: |
| 63 | + packages: An iterable of package names (also used as directory names). |
| 64 | + """ |
| 65 | + for pkg in packages: |
| 66 | + run([PY, "-m", "pip", "install", "-e", f"../{pkg}"]) |
| 67 | + |
| 68 | + |
| 69 | +def remove_residual_dirs(packages: Iterable[str]) -> None: |
| 70 | + """ |
| 71 | + Force remove residual package directories from site-packages. |
| 72 | + This mimics: rm -rf .venv/Lib/site-packages/<pkg> |
| 73 | + to ensure a clean slate before reinstalling. |
| 74 | + """ |
| 75 | + # Locates site-packages, e.g. .venv/Lib/site-packages |
| 76 | + site_packages = sysconfig.get_path("purelib") |
| 77 | + |
| 78 | + for pkg in packages: |
| 79 | + target = os.path.join(site_packages, pkg) |
| 80 | + if os.path.isdir(target): |
| 81 | + print(f"Removing residual directory: {target}", flush=True) |
| 82 | + try: |
| 83 | + shutil.rmtree(target) |
| 84 | + except OSError as e: |
| 85 | + print(f"[WARN] Failed to remove {target}: {e}", flush=True) |
| 86 | + |
| 87 | + |
| 88 | +def reinstall_packages(packages: list[str]) -> None: |
| 89 | + """ |
| 90 | + High-level orchestration for many packages: |
| 91 | + - Uninstall all of them (ignore failures) |
| 92 | + - Force remove residual directories from site-packages |
| 93 | + - Install each in editable mode |
| 94 | + """ |
| 95 | + # 1) Uninstall (ignore if not installed) |
| 96 | + uninstall_many(packages) |
| 97 | + |
| 98 | + # 2) Force remove residual directories (essential for clean reinstall) |
| 99 | + remove_residual_dirs(packages) |
| 100 | + |
| 101 | + # 3) Editable installs |
| 102 | + install_editable_many(packages) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + # ⭐ Fixed, editable local libraries to manage (edit as needed) |
| 107 | + PACKAGES = ["guidata"] |
| 108 | + |
| 109 | + print("🏃 Reinstalling editable packages:", ", ".join(PACKAGES)) |
| 110 | + reinstall_packages(PACKAGES) |
0 commit comments