|
| 1 | +#!/usr/bin/env node |
| 2 | +// Stamp today's CalVer date onto each Python server that changed since its |
| 3 | +// last version bump (#4472). |
| 4 | +// |
| 5 | +// The Python servers stay on CalVer (`2026.8.1`) while the TypeScript ones move |
| 6 | +// to semver under changesets: pip and uv have no dist-tags and always install |
| 7 | +// the numerically highest version, so the `2026.x` line on PyPI has to keep |
| 8 | +// climbing. What changes is WHEN the version is written. The old pipeline |
| 9 | +// (`scripts/release.py`) stamped it inside the release run and pushed the |
| 10 | +// result as a tag only, so `v2/main` never held the version that shipped. Now |
| 11 | +// the version in `pyproject.toml` is the source of truth, it changes only in a |
| 12 | +// reviewed PR, and `release.yml` publishes whatever version it finds that the |
| 13 | +// registry does not have yet. This script is what writes that PR's content; |
| 14 | +// `.github/workflows/prepare-python-release.yml` runs it and opens the PR. |
| 15 | +// |
| 16 | +// For each `src/*/pyproject.toml`: |
| 17 | +// |
| 18 | +// 1. Find the last commit that changed its `version` line. |
| 19 | +// 2. If a file that ships in the package changed since then, set the version |
| 20 | +// to today's date and refresh `uv.lock`, which records the project's own |
| 21 | +// version and is installed `--locked` in CI. |
| 22 | +// |
| 23 | +// "Ships in the package" is read off the path: Python sources, the `py.typed` |
| 24 | +// marker, Markdown (the README is the sdist's long description) and |
| 25 | +// `pyproject.toml` itself (a dependency change). A test-only change does not |
| 26 | +// count: nothing a user installs is different. Over-stamping would be harmless |
| 27 | +// anyway, since the registry guard in `release.yml` decides what publishes; |
| 28 | +// under-stamping is the failure that matters, because a changed package whose |
| 29 | +// version did not move is silently not released. |
| 30 | +// |
| 31 | +// It is JavaScript rather than the Python it replaces so that it sits under the |
| 32 | +// same format, lint and `node --test` checks as every other script here, with |
| 33 | +// no second toolchain for the gate to cover. Only `uv lock` needs `uv`. |
| 34 | +// |
| 35 | +// stdout carries one `name: old -> new` line per stamped package, which the |
| 36 | +// workflow uses as the PR body; everything else goes to stderr. |
| 37 | + |
| 38 | +import { spawnSync } from "node:child_process"; |
| 39 | +import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 40 | +import path from "node:path"; |
| 41 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 42 | + |
| 43 | +const repoRoot = path.resolve( |
| 44 | + path.dirname(fileURLToPath(import.meta.url)), |
| 45 | + "..", |
| 46 | +); |
| 47 | + |
| 48 | +/** |
| 49 | + * The CalVer version for a date: `YEAR.MONTH.DAY`, no zero padding, in UTC so |
| 50 | + * the result does not depend on the runner's timezone. |
| 51 | + * |
| 52 | + * @param {Date} date |
| 53 | + * @returns {string} |
| 54 | + */ |
| 55 | +export function calver(date) { |
| 56 | + return `${date.getUTCFullYear()}.${date.getUTCMonth() + 1}.${date.getUTCDate()}`; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Does a change to this path change what the package ships? |
| 61 | + * |
| 62 | + * @param {string} file a path relative to the package directory, POSIX-style |
| 63 | + * @returns {boolean} |
| 64 | + */ |
| 65 | +export function shipsInPackage(file) { |
| 66 | + const parts = file.split("/"); |
| 67 | + if (parts.some((p) => p === "tests" || p === "test")) return false; |
| 68 | + const name = parts.at(-1) ?? ""; |
| 69 | + return name === "pyproject.toml" || /\.(?:py|md|typed)$/.test(name); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * The `[project]` table's `name` and `version`, read without a TOML parser: |
| 74 | + * both are plain one-line strings in every server here, and a manifest where |
| 75 | + * they are not fails loudly rather than being guessed at. |
| 76 | + * |
| 77 | + * @param {string} toml the text of a pyproject.toml |
| 78 | + * @returns {{ name: string, version: string }} |
| 79 | + */ |
| 80 | +export function readProject(toml) { |
| 81 | + const table = projectTable(toml); |
| 82 | + const field = (key) => { |
| 83 | + const m = new RegExp(`^${key}\\s*=\\s*"([^"]*)"\\s*(?:#.*)?$`, "m").exec( |
| 84 | + table.text, |
| 85 | + ); |
| 86 | + if (!m) |
| 87 | + throw new Error(`pyproject.toml: no \`${key} = "…"\` line in [project]`); |
| 88 | + return m[1]; |
| 89 | + }; |
| 90 | + return { name: field("name"), version: field("version") }; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * `toml` with the `[project]` version replaced, everything else byte-identical. |
| 95 | + * |
| 96 | + * @param {string} toml the text of a pyproject.toml |
| 97 | + * @param {string} version |
| 98 | + * @returns {string} |
| 99 | + */ |
| 100 | +export function stampVersion(toml, version) { |
| 101 | + const table = projectTable(toml); |
| 102 | + const line = /^version\s*=\s*"[^"]*"/m; |
| 103 | + if (!line.test(table.text)) |
| 104 | + throw new Error('pyproject.toml: no `version = "…"` line in [project]'); |
| 105 | + return ( |
| 106 | + toml.slice(0, table.start) + |
| 107 | + table.text.replace(line, `version = "${version}"`) + |
| 108 | + toml.slice(table.end) |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +/** The span of the `[project]` table: from its header to the next table. */ |
| 113 | +function projectTable(toml) { |
| 114 | + const header = /^\[project\]\s*$/m.exec(toml); |
| 115 | + if (!header) throw new Error("pyproject.toml: no [project] table"); |
| 116 | + const start = header.index + header[0].length; |
| 117 | + const next = /^\[/m.exec(toml.slice(start)); |
| 118 | + const end = next ? start + next.index : toml.length; |
| 119 | + return { start, end, text: toml.slice(start, end) }; |
| 120 | +} |
| 121 | + |
| 122 | +function run(command, args, cwd) { |
| 123 | + const res = spawnSync(command, args, { cwd, encoding: "utf8" }); |
| 124 | + if (res.error) throw res.error; |
| 125 | + if (res.status !== 0) |
| 126 | + throw new Error( |
| 127 | + `${command} ${args.join(" ")} exited ${res.status} in ${cwd}: ${res.stderr.trim()}`, |
| 128 | + ); |
| 129 | + return res.stdout.trim(); |
| 130 | +} |
| 131 | + |
| 132 | +/** The real git and uv; `main` takes a replacement as a test seam. */ |
| 133 | +export const TOOLS = { |
| 134 | + /** The last commit that changed the version line, or "" when there is none. */ |
| 135 | + lastVersionBump: (dir) => |
| 136 | + run( |
| 137 | + "git", |
| 138 | + [ |
| 139 | + "log", |
| 140 | + "-1", |
| 141 | + "--format=%H", |
| 142 | + "-G", |
| 143 | + "^version[[:space:]]*=", |
| 144 | + "--", |
| 145 | + "pyproject.toml", |
| 146 | + ], |
| 147 | + dir, |
| 148 | + ), |
| 149 | + /** Paths under `dir` that differ from `commit`, relative to `dir`. */ |
| 150 | + changedSince: (dir, commit) => |
| 151 | + run("git", ["diff", "--name-only", "--relative", commit, "--", "."], dir) |
| 152 | + .split("\n") |
| 153 | + .filter(Boolean), |
| 154 | + /** Bring `uv.lock` in line with the stamped pyproject.toml. */ |
| 155 | + lock: (dir) => { |
| 156 | + run("uv", ["lock"], dir); |
| 157 | + }, |
| 158 | +}; |
| 159 | + |
| 160 | +/** |
| 161 | + * @param {object} [options] |
| 162 | + * @param {string} [options.root] the repository root |
| 163 | + * @param {Date} [options.today] |
| 164 | + * @param {typeof TOOLS} [options.tools] |
| 165 | + * @returns {number} the exit code |
| 166 | + */ |
| 167 | +export function main({ |
| 168 | + root = repoRoot, |
| 169 | + today = new Date(), |
| 170 | + tools = TOOLS, |
| 171 | +} = {}) { |
| 172 | + const src = path.join(root, "src"); |
| 173 | + const packages = readdirSync(src, { withFileTypes: true }) |
| 174 | + .filter((e) => e.isDirectory()) |
| 175 | + .map((e) => path.join(src, e.name)) |
| 176 | + .filter((dir) => existsSync(path.join(dir, "pyproject.toml"))) |
| 177 | + .sort(); |
| 178 | + // A moved `src/` would otherwise read as "nothing to stamp" and the release |
| 179 | + // would ship no Python package without anyone being told. |
| 180 | + if (packages.length === 0) { |
| 181 | + console.error(`prepare-python-release: no pyproject.toml under ${src}`); |
| 182 | + return 1; |
| 183 | + } |
| 184 | + |
| 185 | + const version = calver(today); |
| 186 | + for (const dir of packages) { |
| 187 | + const manifest = path.join(dir, "pyproject.toml"); |
| 188 | + const toml = readFileSync(manifest, "utf8"); |
| 189 | + const { name, version: current } = readProject(toml); |
| 190 | + |
| 191 | + if (current === version) { |
| 192 | + console.error(`${name}: already at ${version}, skipping`); |
| 193 | + continue; |
| 194 | + } |
| 195 | + // No bump commit at all means the history is shallow or the package is |
| 196 | + // new. Stamp it: the registry guard makes a needless stamp harmless. |
| 197 | + const since = tools.lastVersionBump(dir); |
| 198 | + if (since && !tools.changedSince(dir, since).some(shipsInPackage)) { |
| 199 | + console.error(`${name}: no shipped change since ${current}, skipping`); |
| 200 | + continue; |
| 201 | + } |
| 202 | + |
| 203 | + writeFileSync(manifest, stampVersion(toml, version)); |
| 204 | + tools.lock(dir); |
| 205 | + console.error(`${name}: ${current} -> ${version}`); |
| 206 | + console.log(`${name}: ${current} -> ${version}`); |
| 207 | + } |
| 208 | + return 0; |
| 209 | +} |
| 210 | + |
| 211 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) |
| 212 | + process.exit(main()); |
0 commit comments