-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathsync.py
More file actions
382 lines (324 loc) · 11 KB
/
sync.py
File metadata and controls
382 lines (324 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from __future__ import annotations
import json
import os
import shlex
import shutil
import subprocess
import tempfile
import urllib.request
import zipfile
from devenv.lib import colima, config, fs, limactl, proc
from devenv import constants
# TODO: need to replace this with a nicer process executor in devenv.lib
def run_procs(
repo: str,
reporoot: str,
venv_path: str,
_procs: tuple[tuple[str, tuple[str, ...], dict[str, str]], ...],
verbose: bool = False,
) -> bool:
procs: list[tuple[str, tuple[str, ...], subprocess.Popen[bytes]]] = []
stdout = subprocess.PIPE if not verbose else None
stderr = subprocess.STDOUT if not verbose else None
for name, cmd, extra_env in _procs:
print(f"⏳ {name}")
if constants.DEBUG:
proc.xtrace(cmd)
env = {
**constants.user_environ,
**proc.base_env,
"VIRTUAL_ENV": venv_path,
"PATH": f"{venv_path}/bin:{reporoot}/.devenv/bin:{proc.base_path}",
}
if extra_env:
env = {**env, **extra_env}
procs.append(
(
name,
cmd,
subprocess.Popen(
cmd,
stdout=stdout,
stderr=stderr,
env=env,
cwd=reporoot,
),
)
)
all_good = True
for name, final_cmd, p in procs:
out, _ = p.communicate()
if p.returncode != 0:
all_good = False
out_str = f"Output:\n{out.decode()}" if not verbose else ""
print(
f"""
❌ {name}
failed command (code {p.returncode}):
{shlex.join(final_cmd)}
{out_str}
"""
)
else:
print(f"✅ {name}")
return all_good
def sync_chromedriver(reporoot: str) -> int:
if not constants.DARWIN:
print(
"not on macOS; for acceptance testing you'll need to install Google Chrome and chromedriver of the same version"
)
return 0
CHROME = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
install = f"{reporoot}/.devenv/bin/chromedriver"
try:
chrome_ver = subprocess.check_output([CHROME, "--version"], text=True).split()[-1]
except FileNotFoundError:
print(f"{CHROME} not found; install Google Chrome to enable acceptance testing")
return 1
major = chrome_ver.split(".")[0]
with urllib.request.urlopen(
"https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
) as r:
versions = json.load(r)["versions"]
entry = next(
(
v
for v in reversed(versions)
if v["version"].split(".")[0] == major and "chromedriver" in v["downloads"]
),
None,
)
if not entry:
print(f"no ChromeDriver release found for Chrome {major}.x")
return 1
url = next(
(d["url"] for d in entry["downloads"]["chromedriver"] if d["platform"] == "mac-arm64"), None
)
if not url:
print(f"no mac-arm64 ChromeDriver download available for {entry['version']}")
return 1
try:
if (
subprocess.check_output([install, "--version"], text=True).split()[1]
== entry["version"]
):
return 0
except (FileNotFoundError, subprocess.CalledProcessError, IndexError):
pass
print(f"⏳ chromedriver {entry['version']}")
tmpdir = tempfile.mkdtemp()
try:
tmp = os.path.join(tmpdir, "chromedriver.zip")
urllib.request.urlretrieve(url, tmp)
with zipfile.ZipFile(tmp) as zf:
extracted = zf.extract("chromedriver-mac-arm64/chromedriver", tmpdir)
shutil.move(extracted, install)
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
os.chmod(install, 0o755)
print(f"✅ chromedriver {entry['version']}")
return 0
def installed_pnpm(version: str, binroot: str) -> bool:
if shutil.which("pnpm", path=binroot) != f"{binroot}/pnpm" or not os.path.exists(
f"{binroot}/node-env/bin/pnpm"
):
return False
stdout = proc.run((f"{binroot}/pnpm", "--version"), stdout=True)
installed_version = stdout.strip()
return version == installed_version
def install_pnpm(version: str, reporoot: str) -> None:
binroot = fs.ensure_binroot(reporoot)
if installed_pnpm(version, binroot):
return
print(f"installing pnpm {version}...")
# {binroot}/npm is a devenv-managed shim, so
# this install -g ends up putting pnpm into
# .devenv/bin/node-env/bin/pnpm which is pointed
# to by the {binroot}/pnpm shim
proc.run((f"{binroot}/npm", "install", "-g", f"pnpm@{version}"), stdout=True)
fs.write_script(
f"{binroot}/pnpm",
"""#!/bin/sh
export PATH={binroot}/node-env/bin:"${{PATH}}"
exec {binroot}/node-env/bin/pnpm "$@"
""",
shell_escape={"binroot": binroot},
)
def main(context: dict[str, str]) -> int:
repo = context["repo"]
reporoot = context["reporoot"]
cfg = config.get_repo(reporoot)
# TODO: context["verbose"]
verbose = os.environ.get("SENTRY_DEVENV_VERBOSE") is not None
FRONTEND_ONLY = os.environ.get("SENTRY_DEVENV_FRONTEND_ONLY") is not None
SKIP_FRONTEND = os.environ.get("SENTRY_DEVENV_SKIP_FRONTEND") is not None
IN_GIT_WORKTREE = os.path.isfile(f"{reporoot}/.git")
if constants.DARWIN and os.path.exists(f"{constants.root}/bin/colima"):
binroot = f"{reporoot}/.devenv/bin"
colima.uninstall(binroot)
limactl.uninstall(binroot)
if os.path.exists(f"{reporoot}/.devenv/bin/uv"):
os.remove(f"{reporoot}/.devenv/bin/uv")
if os.path.exists(f"{reporoot}/.devenv/bin/uvx"):
os.remove(f"{reporoot}/.devenv/bin/uvx")
if not shutil.which("uv"):
print("\n\n\ndevenv is no longer managing uv; please run `brew install uv`.\n\n\n")
return 1
from devenv.lib import node
node.install(
cfg["node"]["version"],
cfg["node"][constants.SYSTEM_MACHINE],
cfg["node"][f"{constants.SYSTEM_MACHINE}_sha256"],
reporoot,
)
with open(f"{reporoot}/package.json") as f:
package_json = json.load(f)
pnpm = package_json["packageManager"]
pnpm_version = pnpm.split("@")[-1]
# TODO: move pnpm install into devenv
install_pnpm(pnpm_version, reporoot)
# chromedriver required for acceptance testing
if sync_chromedriver(reporoot) != 0:
return 1
# no more imports from devenv past this point! if the venv is recreated
# then we won't have access to devenv libs until it gets reinstalled
# venv's still needed for frontend because repo-local devenv and pre-commit
# exist inside it
venv_dir = f"{reporoot}/.venv"
if not SKIP_FRONTEND and not run_procs(
repo,
reporoot,
venv_dir,
(
(
# Spreading out the network load by installing js,
# then py in the next batch.
"javascript dependencies",
(
"pnpm",
"install",
"--frozen-lockfile",
"--reporter=append-only",
),
{
"NODE_ENV": "development",
# this ensures interactive prompts are answered by
# the defaults (usually yes), useful for recreating
# node_modules if configuration or node version changes
"CI": "true",
},
),
),
verbose,
):
return 1
if not run_procs(
repo,
reporoot,
venv_dir,
(
# could opt out of syncing python if FRONTEND_ONLY but only if repo-local devenv
# and pre-commit were moved to inside devenv and not the sentry venv
(
"python dependencies",
(
"uv",
"sync",
"--frozen",
# don't uninstall sentry/getsentry fast_editable shims
"--inexact",
"--quiet",
"--active",
),
{},
),
),
verbose,
):
return 1
if not run_procs(
repo,
reporoot,
venv_dir,
(
("pre-commit dependencies", ("pre-commit", "install", "--install-hooks", "-f"), {}),
("fast editable", ("python3", "-m", "tools.fast_editable", "--path", "."), {}),
),
verbose,
):
return 1
# Agent skills are non-fatal — private skill repos may not be accessible in CI
if os.path.exists(f"{reporoot}/agents.toml") and shutil.which(
"pnpm", path=f"{reporoot}/.devenv/bin"
):
if not run_procs(
repo,
reporoot,
venv_dir,
(("agent skills", ("pnpm", "dlx", "@sentry/dotagents", "install"), {}),),
verbose,
):
print("⚠️ agent skills failed to install (non-fatal)")
if not IN_GIT_WORKTREE:
fs.ensure_symlink("../../config/hooks/post-merge", f"{reporoot}/.git/hooks/post-merge")
fs.ensure_symlink(
"../../config/hooks/post-checkout", f"{reporoot}/.git/hooks/post-checkout"
)
sentry_conf = os.environ.get("SENTRY_CONF", f"{constants.home}/.sentry")
if not os.path.exists(f"{sentry_conf}/config.yml") or not os.path.exists(
f"{sentry_conf}/sentry.conf.py"
):
proc.run((f"{venv_dir}/bin/sentry", "init", "--dev"))
# Frontend engineers don't necessarily always have devservices running and
# can configure to skip them to save on local resources
if FRONTEND_ONLY:
print("Skipping python migrations since SENTRY_DEVENV_FRONTEND_ONLY is set.")
return 0
proc.run(
(f"{venv_dir}/bin/devservices", "up", "--mode", "migrations"),
pathprepend=f"{reporoot}/.devenv/bin",
exit=True,
)
if not run_procs(
repo,
reporoot,
venv_dir,
(
(
"python migrations",
("make", "apply-migrations"),
{},
),
),
verbose,
):
return 1
# faster prerequisite check than starting up sentry and running createuser idempotently
stdout = proc.run(
(
"docker",
"exec",
"postgres-postgres-1",
"psql",
"sentry",
"postgres",
"-t",
"-c",
"select exists (select from auth_user where email = 'admin@sentry.io')",
),
stdout=True,
)
if stdout != "t":
proc.run(
(
f"{venv_dir}/bin/sentry",
"createuser",
"--superuser",
"--email",
"admin@sentry.io",
"--password",
"admin",
"--no-input",
)
)
return 0