|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * The Python gate: one `validate` chain per Python server, and one command |
| 4 | + * that runs it for every server (#4865). |
| 5 | + * |
| 6 | + * node scripts/validate-py.mjs # every server under src/ with a pyproject.toml |
| 7 | + * node scripts/validate-py.mjs fetch git # just the named ones |
| 8 | + * npm run validate:py # the root entry point (all servers) |
| 9 | + * |
| 10 | + * Each server runs the same chain, in its own directory, through `uv` (never |
| 11 | + * pip): a locked sync, `ruff check`, `ruff format --check`, `pyright`, |
| 12 | + * `pytest`, then `uv build`. It is the Python counterpart of the TypeScript |
| 13 | + * `validate` (format check, lint, typecheck, test, build). |
| 14 | + * |
| 15 | + * Why a Node script rather than a `uv run` chain in each pyproject: Python has |
| 16 | + * no script runner in `pyproject.toml`, and a root npm script chaining three |
| 17 | + * `cd … && … &&` strings would stop at the first failing server. This runs the |
| 18 | + * chain fail-fast *within* a server — a later step is meaningless once the |
| 19 | + * lockfile or the lint is red — but carries on to the next server, so one run |
| 20 | + * reports every server's verdict. CI calls the same script with one server per |
| 21 | + * matrix leg, so the local command and CI cannot drift. |
| 22 | + * |
| 23 | + * Servers are discovered, not listed: a new Python server is gated the moment |
| 24 | + * it lands with a `pyproject.toml`, rather than silently falling outside a |
| 25 | + * hardcoded table. |
| 26 | + * |
| 27 | + * `--locked` on the sync fails when `uv.lock` no longer matches |
| 28 | + * `pyproject.toml` (the same check CI's build job makes); every later `uv run` |
| 29 | + * then uses `--frozen`, so no step can re-resolve the environment it is |
| 30 | + * checking. |
| 31 | + */ |
| 32 | +import { spawnSync } from "node:child_process"; |
| 33 | +import { existsSync, readdirSync } from "node:fs"; |
| 34 | +import path from "node:path"; |
| 35 | +import { fileURLToPath } from "node:url"; |
| 36 | + |
| 37 | +/** The chain every Python server runs, in order. */ |
| 38 | +export const STEPS = [ |
| 39 | + { name: "sync", argv: ["uv", "sync", "--locked", "--all-extras", "--dev"] }, |
| 40 | + { name: "ruff check", argv: ["uv", "run", "--frozen", "ruff", "check", "."] }, |
| 41 | + { |
| 42 | + name: "ruff format --check", |
| 43 | + argv: ["uv", "run", "--frozen", "ruff", "format", "--check", "."], |
| 44 | + }, |
| 45 | + { name: "pyright", argv: ["uv", "run", "--frozen", "pyright"] }, |
| 46 | + { name: "pytest", argv: ["uv", "run", "--frozen", "pytest"] }, |
| 47 | + { name: "build", argv: ["uv", "build"] }, |
| 48 | +]; |
| 49 | + |
| 50 | +/** |
| 51 | + * Every directory directly under `srcDir` that holds a `pyproject.toml`, |
| 52 | + * sorted so the run order is stable. |
| 53 | + * @param {string} srcDir |
| 54 | + * @returns {string[]} |
| 55 | + */ |
| 56 | +export function discoverServers(srcDir) { |
| 57 | + return readdirSync(srcDir, { withFileTypes: true }) |
| 58 | + .filter( |
| 59 | + (entry) => |
| 60 | + entry.isDirectory() && |
| 61 | + existsSync(path.join(srcDir, entry.name, "pyproject.toml")) |
| 62 | + ) |
| 63 | + .map((entry) => entry.name) |
| 64 | + .sort(); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Resolve the servers to validate from the CLI arguments. No arguments means |
| 69 | + * every discovered server; a name that is not a Python server is an error, |
| 70 | + * never a silent skip. |
| 71 | + * @param {string[]} args |
| 72 | + * @param {string[]} available |
| 73 | + * @returns {{ servers: string[] } | { error: string }} |
| 74 | + */ |
| 75 | +export function selectServers(args, available) { |
| 76 | + if (available.length === 0) { |
| 77 | + return { error: "no Python servers found (no src/*/pyproject.toml)" }; |
| 78 | + } |
| 79 | + if (args.length === 0) return { servers: available }; |
| 80 | + const unknown = args.filter((name) => !available.includes(name)); |
| 81 | + if (unknown.length > 0) { |
| 82 | + return { |
| 83 | + error: `not a Python server: ${unknown.join( |
| 84 | + ", " |
| 85 | + )} (known: ${available.join(", ")})`, |
| 86 | + }; |
| 87 | + } |
| 88 | + return { servers: args }; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Run one command and report whether it succeeded. |
| 93 | + * @param {string[]} argv |
| 94 | + * @param {string} cwd |
| 95 | + * @returns {{ ok: boolean, detail?: string }} |
| 96 | + */ |
| 97 | +export function runCommand(argv, cwd) { |
| 98 | + const [cmd, ...rest] = argv; |
| 99 | + const result = spawnSync(cmd, rest, { cwd, stdio: "inherit" }); |
| 100 | + if (result.error) { |
| 101 | + const code = /** @type {NodeJS.ErrnoException} */ (result.error).code; |
| 102 | + return { |
| 103 | + ok: false, |
| 104 | + detail: |
| 105 | + code === "ENOENT" |
| 106 | + ? `${cmd} not found on PATH (install uv: https://docs.astral.sh/uv/)` |
| 107 | + : result.error.message, |
| 108 | + }; |
| 109 | + } |
| 110 | + if (result.status !== 0) { |
| 111 | + return { |
| 112 | + ok: false, |
| 113 | + detail: |
| 114 | + result.status === null |
| 115 | + ? `killed by ${result.signal}` |
| 116 | + : `exit ${result.status}`, |
| 117 | + }; |
| 118 | + } |
| 119 | + return { ok: true }; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Validate each server: its chain stops at the first failing step, and the |
| 124 | + * run moves on to the next server either way. |
| 125 | + * @param {{ |
| 126 | + * servers: string[], |
| 127 | + * srcDir: string, |
| 128 | + * steps?: typeof STEPS, |
| 129 | + * run?: (argv: string[], cwd: string) => { ok: boolean, detail?: string }, |
| 130 | + * log?: (line: string) => void, |
| 131 | + * }} options |
| 132 | + * @returns {{ server: string, failedStep?: string, detail?: string }[]} |
| 133 | + */ |
| 134 | +export function validateServers({ |
| 135 | + servers, |
| 136 | + srcDir, |
| 137 | + steps = STEPS, |
| 138 | + run = runCommand, |
| 139 | + log = console.log, |
| 140 | +}) { |
| 141 | + const results = []; |
| 142 | + for (const server of servers) { |
| 143 | + const cwd = path.join(srcDir, server); |
| 144 | + let failure; |
| 145 | + for (const step of steps) { |
| 146 | + log(`\n[validate:py] ${server}: ${step.name}`); |
| 147 | + const outcome = run(step.argv, cwd); |
| 148 | + if (!outcome.ok) { |
| 149 | + failure = { failedStep: step.name, detail: outcome.detail }; |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + results.push({ server, ...failure }); |
| 154 | + } |
| 155 | + return results; |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * One line per server, and the process exit code the results imply. |
| 160 | + * @param {{ server: string, failedStep?: string, detail?: string }[]} results |
| 161 | + * @returns {{ lines: string[], exitCode: number }} |
| 162 | + */ |
| 163 | +export function summarize(results) { |
| 164 | + const lines = results.map((r) => |
| 165 | + r.failedStep === undefined |
| 166 | + ? ` PASS ${r.server}` |
| 167 | + : ` FAIL ${r.server} — ${r.failedStep}${ |
| 168 | + r.detail ? ` (${r.detail})` : "" |
| 169 | + }` |
| 170 | + ); |
| 171 | + const exitCode = results.some((r) => r.failedStep !== undefined) ? 1 : 0; |
| 172 | + return { lines, exitCode }; |
| 173 | +} |
| 174 | + |
| 175 | +function main() { |
| 176 | + const repoRoot = path.resolve( |
| 177 | + path.dirname(fileURLToPath(import.meta.url)), |
| 178 | + ".." |
| 179 | + ); |
| 180 | + const srcDir = path.join(repoRoot, "src"); |
| 181 | + const selection = selectServers( |
| 182 | + process.argv.slice(2), |
| 183 | + discoverServers(srcDir) |
| 184 | + ); |
| 185 | + if ("error" in selection) { |
| 186 | + console.error(`[validate:py] ${selection.error}`); |
| 187 | + return 2; |
| 188 | + } |
| 189 | + const results = validateServers({ servers: selection.servers, srcDir }); |
| 190 | + const { lines, exitCode } = summarize(results); |
| 191 | + console.log(["\n[validate:py] summary", ...lines].join("\n")); |
| 192 | + return exitCode; |
| 193 | +} |
| 194 | + |
| 195 | +if ( |
| 196 | + process.argv[1] && |
| 197 | + path.resolve(process.argv[1]) === fileURLToPath(import.meta.url) |
| 198 | +) { |
| 199 | + process.exitCode = main(); |
| 200 | +} |
0 commit comments