Skip to content

Commit 2201958

Browse files
cliffhallclaude
andcommitted
ci(python): one validate chain per server, a root validate:py, ruff in CI
scripts/validate-py.mjs runs uv sync --locked, ruff check, ruff format --check, pyright, pytest and uv build in each Python server (discovered by pyproject.toml). npm run validate:py runs it for every server; the python.yml test job runs it per matrix package, so CI now fails on a ruff lint or format finding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent cf05824 commit 2201958

4 files changed

Lines changed: 423 additions & 18 deletions

File tree

‎.github/workflows/python.yml‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ jobs:
2525
PACKAGES=$(find . -name pyproject.toml -exec dirname {} \; | sed 's/^\.\///' | jq -R -s -c 'split("\n")[:-1]')
2626
echo "packages=$PACKAGES" >> $GITHUB_OUTPUT
2727
28+
script-tests:
29+
name: Test the Python gate script
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v6
33+
34+
- uses: actions/setup-node@v7
35+
with:
36+
node-version: 22
37+
38+
- name: Run node --test
39+
run: node --test scripts/validate-py.test.mjs
40+
2841
test:
2942
needs: [detect-packages]
3043
strategy:
@@ -43,24 +56,14 @@ jobs:
4356
with:
4457
python-version-file: "src/${{ matrix.package }}/.python-version"
4558

46-
- name: Install dependencies
47-
working-directory: src/${{ matrix.package }}
48-
run: uv sync --frozen --all-extras --dev
59+
- uses: actions/setup-node@v7
60+
with:
61+
node-version: 22
4962

50-
- name: Check if tests exist
51-
id: check-tests
52-
working-directory: src/${{ matrix.package }}
53-
run: |
54-
if [ -d "tests" ] || [ -d "test" ] || grep -q "pytest" pyproject.toml; then
55-
echo "has-tests=true" >> $GITHUB_OUTPUT
56-
else
57-
echo "has-tests=false" >> $GITHUB_OUTPUT
58-
fi
59-
60-
- name: Run tests
61-
if: steps.check-tests.outputs.has-tests == 'true'
62-
working-directory: src/${{ matrix.package }}
63-
run: uv run pytest
63+
# The same per-server chain `npm run validate:py` runs locally: locked
64+
# sync, ruff check, ruff format --check, pyright, pytest, uv build.
65+
- name: Validate
66+
run: node scripts/validate-py.mjs ${{ matrix.package }}
6467

6568
build:
6669
needs: [detect-packages, test]

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"build": "npm run build --workspaces",
1717
"watch": "npm run watch --workspaces",
1818
"publish-all": "npm publish --workspaces --access public",
19-
"link-all": "npm link --workspaces"
19+
"link-all": "npm link --workspaces",
20+
"validate:py": "node scripts/validate-py.mjs"
2021
},
2122
"dependencies": {
2223
"@modelcontextprotocol/server-everything": "*",

‎scripts/validate-py.mjs‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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

Comments
 (0)