| 1 | #!/usr/bin/env python3 |
| 2 | """Stage or activate project command wrappers in the development VM.""" |
| 3 | |
| 4 | import argparse |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import shutil |
| 8 | import sys |
| 9 | import time |
| 10 | |
| 11 | |
| 12 | def main(): |
| 13 | parser = argparse.ArgumentParser(description=__doc__) |
| 14 | parser.add_argument("action", choices=("stage", "activate")) |
| 15 | parser.add_argument("release") |
| 16 | args = parser.parse_args() |
| 17 | if sys.platform != "darwin" or not os.path.ismount("/Volumes/Code") or os.geteuid() == 0: |
| 18 | parser.error("Run this installer as the development VM user with Code mounted.") |
| 19 | if not args.release or any(c not in "abcdefghijklmnopqrstuvwxyz0123456789-." for c in args.release): |
| 20 | parser.error("Use a release name containing lowercase letters, numbers, dots, or hyphens.") |
| 21 | local = Path.home() / ".local" |
| 22 | base = local / "share/project-tools" |
| 23 | release = base / "releases" / args.release |
| 24 | names = ("client.py", "pnpm", "jj", "project") |
| 25 | if args.action == "stage": |
| 26 | release.mkdir(parents=True) |
| 27 | for name in names: |
| 28 | shutil.copy2(Path(__file__).with_name(name), release / name) |
| 29 | print(release) |
| 30 | return |
| 31 | if any(not (release / name).is_file() for name in names): |
| 32 | parser.error("Stage this release before activating it.") |
| 33 | if not (local / "bin/jj-real").is_file(): |
| 34 | parser.error("Install the native Jujutsu executable as ~/.local/bin/jj-real first.") |
| 35 | native_pnpm = local / "bin/pnpm-real" |
| 36 | if not native_pnpm.exists(): |
| 37 | original = (local / "bin/pnpm").resolve(strict=True) |
| 38 | with original.open("rb") as binary: |
| 39 | magic = binary.read(4) |
| 40 | if magic != b"\xcf\xfa\xed\xfe": |
| 41 | parser.error("Preserve the native pnpm executable as ~/.local/bin/pnpm-real first.") |
| 42 | native_pnpm.symlink_to(original) |
| 43 | overlay = local / "share/agents/vm" |
| 44 | current = base / "current" |
| 45 | if not current.is_symlink(): |
| 46 | previous = base / "releases" / ("before-recovery-" + time.strftime("%Y%m%d%H%M%S")) |
| 47 | previous.mkdir() |
| 48 | for name in ("jj", "project"): |
| 49 | shutil.copy2(overlay / name, previous / name) |
| 50 | (previous / "pnpm").symlink_to(native_pnpm.resolve()) |
| 51 | shutil.copy2(release / "client.py", previous / "client.py") |
| 52 | (base / "previous").symlink_to(previous.relative_to(base)) |
| 53 | elif current.resolve() != release.resolve(): |
| 54 | previous = base / ".previous-next" |
| 55 | previous.symlink_to(os.readlink(current)) |
| 56 | previous.replace(base / "previous") |
| 57 | link = base / ".current-next" |
| 58 | link.symlink_to(release.relative_to(base)) |
| 59 | link.replace(current) |
| 60 | for name in names: |
| 61 | link = overlay / ("." + name + "-next") |
| 62 | link.symlink_to(os.path.relpath(current / name, overlay)) |
| 63 | link.replace(overlay / name) |
| 64 | link = local / "bin/.pnpm-next" |
| 65 | link.symlink_to("../share/agents/vm/pnpm") |
| 66 | link.replace(local / "bin/pnpm") |
| 67 | for directory in (local / "share/vite-plus/package_manager/pnpm").glob("*/pnpm/bin"): |
| 68 | binary = directory / "pnpm" |
| 69 | if not binary.is_file(): |
| 70 | continue |
| 71 | native = directory / "pnpm-real" |
| 72 | if not native.exists(): |
| 73 | os.link(binary, native) |
| 74 | link = directory / ".pnpm-next" |
| 75 | link.symlink_to(os.path.relpath(overlay / "pnpm", directory)) |
| 76 | link.replace(binary) |
| 77 | print(release) |
| 78 | |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | main() |