1"""Guest transport and lifecycle hooks for the shared project store."""
2
3import errno
4import hashlib
5import json
6import os
7from pathlib import Path
8import re
9import subprocess
10import sys
11import time
12
13
14ROOT = Path(os.environ.get("PROJECT_ROOT", "/Volumes/Code/project")).resolve()
15
16
17def host():
18 value = os.environ.get("PROJECT_STORE_SSH")
19 if value is None:
20 value = "project-store" if (Path.home() / ".ssh/project-store").is_file() else ""
21 if value and not re.fullmatch(r"[a-zA-Z0-9][a-zA-Z0-9_.-]*", value):
22 raise ValueError("Use an SSH alias for PROJECT_STORE_SSH.")
23 return value
24
25
26def inside(path):
27 path = Path(path).resolve()
28 if not path.is_relative_to(ROOT):
29 raise ValueError(f"Path is outside the project share: {path}")
30 return path
31
32
33def request(payload, *, capture=True):
34 return subprocess.run(["ssh", "-T", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10",
35 "--", host()], input=json.dumps(payload) + "\n", text=True,
36 stdout=subprocess.PIPE if capture else None)
37
38
39def engine():
40 return subprocess.check_output(["node", "-p",
41 "[process.platform,process.arch,'node'+process.versions.node].join(';')"],
42 text=True).strip()
43
44
45def wait_workspace(destination):
46 deadline = time.monotonic() + 30
47 while True:
48 try:
49 list(destination.parent.iterdir())
50 list(destination.iterdir())
51 list((destination / ".jj/working_copy").iterdir())
52 return
53 except OSError as error:
54 if error.errno not in (errno.ENOENT, errno.ESTALE):
55 raise
56 if time.monotonic() >= deadline:
57 raise ValueError("Workspace was created but is not visible over NFS yet. Wait before opening it.")
58 time.sleep(0.1)
59
60
61def pnpm_arguments(args, cwd):
62 command = None
63 forwarded = []
64 parsed = iter(args)
65 for arg in parsed:
66 if arg in ("-C", "--dir"):
67 cwd = (cwd / next(parsed)).resolve()
68 elif arg.startswith("--dir="):
69 cwd = (cwd / arg.split("=", 1)[1]).resolve()
70 elif arg in ("--filter", "--filter-prod", "-F"):
71 forwarded.extend((arg, next(parsed)))
72 elif arg.startswith(("--filter=", "--filter-prod=")) or arg in {
73 "--frozen-lockfile", "--no-frozen-lockfile", "--offline", "--prefer-offline",
74 "--force", "--ignore-scripts", "--lockfile-only", "-r", "--recursive",
75 "-w", "--workspace-root", "-D", "--save-dev", "-P", "--save-prod",
76 "-O", "--save-optional", "--save-peer", "-E", "--save-exact", "--latest", "-L",
77 "--prod", "--dev", "--no-optional", "--ignore-workspace-root-check"}:
78 forwarded.append(arg)
79 elif command is None and arg in {"install", "i", "add", "update", "up", "upgrade",
80 "remove", "rm", "uninstall", "un", "uni"}:
81 command = arg
82 forwarded.append(arg)
83 elif command is not None and not arg.startswith("-"):
84 forwarded.append(arg)
85 else:
86 return cwd, None, arg if command is None and not arg.startswith("-") else command
87 return cwd, forwarded if command else None, command
88
89
90def finish_install(workspace, native, *, ignore_scripts=False, modules_hash=None, cwd=None, args=()):
91 deadline = time.monotonic() + 30
92 while True:
93 try:
94 list(workspace.iterdir())
95 modules = workspace / "node_modules/.modules.yaml"
96 content = modules.read_bytes()
97 if modules_hash is None or hashlib.sha256(content).hexdigest() == modules_hash:
98 data = json.loads(content)
99 break
100 except OSError as error:
101 if error.errno not in (errno.ENOENT, errno.ESTALE):
102 raise
103 if time.monotonic() >= deadline:
104 raise ValueError("Installation is not visible over NFS yet. Retry after the mount catches up.")
105 time.sleep(0.1)
106 if ignore_scripts or not data.get("pendingBuilds"):
107 return
108 store = Path(data["storeDir"])
109 target = workspace
110 command = ["--recursive", "--include-workspace-root", "rebuild", "--pending"]
111 extra = []
112 if data["packageManager"].startswith("pnpm@11."):
113 store.mkdir(parents=True, exist_ok=True)
114 files = store / "files"
115 if not os.path.lexists(files):
116 relative = store.relative_to(Path.home() / "Library/Caches/project")
117 files.symlink_to(Path("/Volumes/Code/cache/project-pnpm") / relative / "files")
118 elif data["packageManager"].startswith("pnpm@12."):
119 # pnpm 12 rebuild can recopy patched packages that the storage host already prepared.
120 target = cwd or workspace
121 command = ["install", "--frozen-lockfile"]
122 filtered = False
123 parsed = iter(args)
124 for arg in parsed:
125 if arg in ("--filter", "--filter-prod", "-F"):
126 command.extend((arg, next(parsed)))
127 filtered = True
128 elif arg.startswith(("--filter=", "--filter-prod=")):
129 command.append(arg)
130 filtered = True
131 elif arg in ("-r", "--recursive", "-w", "--workspace-root", "--offline", "--prefer-offline"):
132 command.append(arg)
133 _, _, operation = pnpm_arguments(args, target)
134 if operation not in (None, "install", "i") and not filtered and not any(
135 arg in args for arg in ("-r", "--recursive")):
136 command.append("--filter=./")
137 included = data["included"]
138 if included["dependencies"] and not included["devDependencies"]:
139 command.append("--prod")
140 elif included["devDependencies"] and not included["dependencies"]:
141 command.append("--dev")
142 if not included["optionalDependencies"]:
143 command.append("--no-optional")
144 extra = ["--config.virtual-store-type=local", "--config.store-index-format=files",
145 "--config.side-effects-cache=false", "--config.side-effects-cache-readonly=false",
146 "--config.prefer-symlinked-executables=true",
147 "--config.optimistic-repeat-install=false"]
148 print("pnpm: Running Mac build hooks.", file=sys.stderr)
149 subprocess.run([str(native), "--dir", str(target), *command,
150 "--store-dir=" + str(store.parent),
151 "--config.cache-dir=" + str(workspace.parent / ".pnpm-cache"),
152 "--config.enable-global-virtual-store=false", "--config.verify-deps-before-run=false", *extra],
153 cwd=workspace, check=True)
154
155
156VALUES = {"-R", "--repository", "--config", "--config-file", "--color", "--at-operation", "--at-op",
157 "-r", "--revision", "-s", "--source", "-d", "--destination", "-b", "--branch",
158 "--from", "--to", "-A", "--insert-after", "-B", "--insert-before", "-T", "--template",
159 "-m", "--message", "--name", "--sparse-patterns", "--author", "--tool", "--editor",
160 "-n", "--limit", "--sort", "--add", "--remove"}
161GLOBALS = {"-R", "--repository", "--config", "--config-file", "--color", "--at-operation", "--at-op"}
162
163
164def jj_arguments(args):
165 words, options, config = [], [], []
166 args = iter(args)
167 for arg in args:
168 if arg == "--":
169 words.extend(args)
170 break
171 name, equal, value = arg.partition("=")
172 if name in VALUES:
173 value = value if equal else next(args)
174 options.append((name, value))
175 if name in GLOBALS:
176 config.extend((name, value))
177 elif len(arg) > 2 and arg[:2] in VALUES and not arg.startswith("--"):
178 name, value = arg[:2], arg[2:].removeprefix("=")
179 options.append((name, value))
180 if name in GLOBALS:
181 config.extend((name, value))
182 elif arg.startswith("-"):
183 options.append((arg, None))
184 else:
185 words.append(arg)
186 return words, options, config
187
188def jj_paths(words, options):
189 if not words:
190 return []
191 command = tuple(words[:2]) if words and words[0] in ("workspace", "file", "sparse") else tuple(words[:1])
192 paths = []
193 if command == ("workspace", "add"):
194 paths = words[2:]
195 elif command[0] in ("diff", "show", "restore"):
196 paths = words[1:]
197 elif command[0] == "file":
198 paths = words[2:]
199 elif command == ("sparse", "set"):
200 paths = [value for name, value in options if name in ("--add", "--remove")]
201 return paths