From 515d2250fc35e033ba761b49f83f60a00b0d21ef Mon Sep 17 00:00:00 2001 From: Wolf Date: Sat, 26 Sep 2026 19:43:50 +0200 Subject: [PATCH] Deploy from a WSL checkout, from boilerplate 1679e88 When the checkout lives under \\wsl.localhost, step 1 runs its tests in the distro through deploy/test-commit.sh and db.ps1 push makes its snapshot there, because the .venv is a Linux one. pipeline/context.py narrows by sys.platform so mypy passes on Linux. See boilerplate docs/wsl.md. Co-Authored-By: Claude Opus 5.5 --- deploy/1-test-and-promote.ps1 | 9 ++++++++- deploy/_common.ps1 | 10 ++++++++++ deploy/db.ps1 | 10 +++++++++- deploy/test-commit.sh | 18 ++++++++++++++++++ pipeline/context.py | 9 +++++++-- 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 deploy/test-commit.sh diff --git a/deploy/1-test-and-promote.ps1 b/deploy/1-test-and-promote.ps1 index a4b4ad7..b31f868 100644 --- a/deploy/1-test-and-promote.ps1 +++ b/deploy/1-test-and-promote.ps1 @@ -27,7 +27,14 @@ try { # tree that is about to become production, with none of the working copy's # uncommitted edits in the way. Worktrees have no .venv of their own, so the # interpreter comes from the main checkout and the code from the worktree. - if (-not $NoTests) { + # + # A checkout inside WSL has a Linux .venv, so there the same steps run in + # the distro through deploy/test-commit.sh. + if (-not $NoTests -and $Wsl.Success) { + Write-Step "tests on $Source, in WSL $($Wsl.Groups[1].Value)" + wsl.exe -d $Wsl.Groups[1].Value --cd (ConvertTo-LinuxPath $root) -- sh deploy/test-commit.sh $commit + Assert-Ok 'tests' + } elseif (-not $NoTests) { Write-Step "tests on $Source" if (-not (Test-Path $Python)) { throw "no interpreter at $Python - set PYTHON in deploy\release.env" diff --git a/deploy/_common.ps1 b/deploy/_common.ps1 index 0e5587a..5338cf8 100644 --- a/deploy/_common.ps1 +++ b/deploy/_common.ps1 @@ -16,6 +16,16 @@ $ErrorActionPreference = 'Stop' #: directory, so the scripts can be run from anywhere. $root = Split-Path -Parent $PSScriptRoot +#: A checkout inside WSL, run from Windows as \\wsl.localhost\\... Its +#: .venv is a Linux one, so the steps that run Python hand that part to the +#: distro. $Wsl.Groups[1] is the distro; ConvertTo-LinuxPath turns a path under +#: the checkout into the distro's own. See docs/wsl.md. +$WslPattern = '^\\\\wsl(?:\.localhost|\$)\\([^\\]+)(\\.*)$' +$Wsl = [regex]::Match($root, $WslPattern) +function ConvertTo-LinuxPath([string] $Path) { + [regex]::Match($Path, $WslPattern).Groups[2].Value -replace '\\', '/' +} + function Assert-Ok([string] $What) { if ($LASTEXITCODE -ne 0) { throw "$What failed (exit $LASTEXITCODE)" } } diff --git a/deploy/db.ps1 b/deploy/db.ps1 index e6f3734..244f31f 100644 --- a/deploy/db.ps1 +++ b/deploy/db.ps1 @@ -84,7 +84,15 @@ with tempfile.TemporaryDirectory() as work: with plain.open("rb") as raw, gzip.open(target, "wb") as out: shutil.copyfileobj(raw, out) '@ - & $Python -c $snapshot $LocalDb $snap; Assert-Ok 'snapshot' + # A checkout inside WSL has a Linux .venv, and SQLite cannot lock a WAL + # database through the \\wsl.localhost share, so the snapshot is taken in + # the distro. The script goes in on stdin, past both shells. + if ($Wsl.Success) { + $snapshot | wsl.exe -d $Wsl.Groups[1].Value --cd (ConvertTo-LinuxPath $root) --exec .venv/bin/python - (ConvertTo-LinuxPath $LocalDb) (ConvertTo-LinuxPath $snap) + } else { + & $Python -c $snapshot $LocalDb $snap + } + Assert-Ok 'snapshot' ' {0:N1} MB' -f ((Get-Item $snap).Length / 1MB) | Write-Host try { diff --git a/deploy/test-commit.sh b/deploy/test-commit.sh new file mode 100644 index 0000000..21833b5 --- /dev/null +++ b/deploy/test-commit.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# Test one commit in a throwaway worktree, with the checkout's Linux .venv. +# 1-test-and-promote.ps1 runs this through wsl.exe when the checkout lives +# inside WSL, where the .venv has no Windows python.exe to call. The gates are +# the same as in that script; keep the two lists in step. +# Usage: sh deploy/test-commit.sh (from the checkout root) +set -eu +root=$(pwd) +python="$root/.venv/bin/python" +[ -x "$python" ] || { echo "no interpreter at $python - run uv sync" >&2; exit 1; } +base=$(mktemp -d /tmp/test-commit-XXXXXXXX) +tree="$base/tree" +trap 'git -C "$root" worktree remove --force "$tree" 2>/dev/null; rm -rf "$base"' EXIT +git worktree add --detach "$tree" "$1" >/dev/null +cd "$tree" +"$python" -m pytest -q +# "$python" -m ruff check . +# "$python" -m mypy --cache-dir "$root/.mypy_cache" diff --git a/pipeline/context.py b/pipeline/context.py index e2ae973..2c4550a 100644 --- a/pipeline/context.py +++ b/pipeline/context.py @@ -19,6 +19,7 @@ import logging import os import signal import subprocess +import sys import tempfile import time from collections.abc import Callable, Sequence @@ -168,8 +169,12 @@ def spawn(argv: Sequence[str], *, check: Callable[[], None] | None = None, Started detached — its own process group, or Windows' equivalent — purely so `kill_tree` has something to aim at. """ - detach: dict[str, Any] = ({"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP} - if os.name == "nt" else {"start_new_session": True}) + # An `if`, because mypy narrows `sys.platform` in statements only. + detach: dict[str, Any] + if sys.platform == "win32": + detach = {"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP} + else: + detach = {"start_new_session": True} with tempfile.TemporaryFile() as out, tempfile.TemporaryFile() as err: popen.setdefault("stdout", out) popen.setdefault("stderr", err)