Files
pplus-web/scripts/mojo/mojocorpus/processors/release.py
T
2026-10-05 03:03:38 +00:00

98 lines
3.9 KiB
Python

"""Release notes for the pinned version, and whether the snapshot matches the pin.
Two jobs:
1. From docs/site/releases/v<pin>.md, record each section's bullets (the
first sentence of each, with its anchor). "Removed" and "Language changes"
are where Stage 2's quick fixes come from.
2. Say how the snapshot relates to the pin. The newest release file and the
nightly changelog tell us: a snapshot whose newest release is the pin and
whose nightly changelog is empty IS the pin; one with nightly entries is
newer, and those entries are exactly what the pin does not have.
Writes release/release.json. A newer snapshot is a warning in the report, not
an error: the dialect markers and verify keep its newer material out.
"""
from __future__ import annotations
import re
from ..common import write_json
from . import Context, fresh_dir
NAME = "release"
SOURCE = "modular"
INPUTS = ["docs/site/releases"]
VERSION = re.compile(r"^v(\d+)\.(\d+)\.(\d+)$")
def sections(md: str) -> list[dict]:
out: list[dict] = []
cur: dict | None = None
bullet: list[str] | None = None
def close_bullet():
nonlocal bullet
if cur is not None and bullet:
text = " ".join(x.strip() for x in bullet)
first = re.split(r"(?<=[.!?])\s+(?=[A-Z`])", text, maxsplit=1)[0]
cur["bullets"].append(first[:300])
bullet = None
for line in md.split("\n"):
h = re.match(r"^(#{2,3})\s+(.*?)\s*(?:\{#([\w-]+)\})?\s*$", line)
if h:
close_bullet()
cur = {"level": len(h.group(1)), "title": h.group(2), "anchor": h.group(3) or "", "bullets": []}
out.append(cur)
elif line.startswith("- "):
close_bullet()
bullet = [line[2:]]
elif bullet is not None and line.startswith(" ") and line.strip() and not line.strip().startswith("```"):
bullet.append(line)
elif not line.strip():
close_bullet()
close_bullet()
return out
def run(ctx: Context) -> dict:
out = fresh_dir(NAME)
rel_dir = ctx.root / "docs" / "site" / "releases"
versions = sorted(tuple(map(int, m.groups())) for p in rel_dir.glob("v*.md") if (m := VERSION.match(p.stem)))
newest = ".".join(map(str, versions[-1])) if versions else None
pin = ctx.pin.version
notes = rel_dir / f"v{pin}.md"
nightly = ctx.root / "docs" / "site" / "nightly-changelog.md"
nightly_secs = sections(nightly.read_text(encoding="utf-8")) if nightly.exists() else []
nightly_bullets = [{"section": s["title"], "bullet": b} for s in nightly_secs for b in s["bullets"]]
if newest is None:
relation = "unknown"
elif tuple(map(int, pin.split("."))) > versions[-1]:
relation = "older"
elif newest != pin:
relation = "newer"
else:
relation = "newer" if nightly_bullets else "same"
if relation == "newer":
ctx.warn(f"release: the modular snapshot is newer than Mojo {pin} (newest release notes v{newest}, "
f"{len(nightly_bullets)} nightly changelog entries). Dialect markers and verify keep newer material out; "
f"pin a release tag to remove this warning.")
elif relation == "older":
ctx.warn(f"release: the modular snapshot (newest release v{newest}) is older than the pin {pin}")
if not notes.exists():
ctx.warn(f"release: no release notes for v{pin} in the snapshot")
doc = {
"pin": pin, "newestReleaseInSnapshot": newest, "snapshot": relation,
"notes": {"provenance": ctx.prov(f"docs/site/releases/v{pin}.md"),
"sections": sections(notes.read_text(encoding="utf-8")) if notes.exists() else []},
"nightlyBeyondPin": {"provenance": ctx.prov("docs/site/nightly-changelog.md"), "entries": nightly_bullets},
}
write_json(out / "release.json", doc)
return {"snapshot": relation, "newestRelease": newest,
"sections": len(doc["notes"]["sections"]), "nightlyEntries": len(nightly_bullets)}