|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Checks to run on a release branch before pushing a release tag. |
| 3 | +
|
| 4 | +Usage (from the root of the repo, on the commit to be tagged): |
| 5 | +
|
| 6 | + python maint/scripts/pre_tag_checks.py v6.7.0b1 |
| 7 | + python maint/scripts/pre_tag_checks.py v6.6.1 --quick # metadata only |
| 8 | +
|
| 9 | +This matters most for security releases, which skip the betas that would |
| 10 | +otherwise catch regressions. See maint/RELEASING.md for the parts of the |
| 11 | +process that can't be automated. |
| 12 | +
|
| 13 | +The checks: |
| 14 | +
|
| 15 | +* The working tree is clean. |
| 16 | +* ``tornado.version`` and ``tornado.version_info`` match the tag. |
| 17 | +* The release notes exist, are in docs/releases.rst, and (for a final |
| 18 | + release) have a date. |
| 19 | +* Unless ``--quick``: the full test suite with the optional dependencies |
| 20 | + (property tests and the curl client included), lint, docs and coverage, |
| 21 | + via tox. |
| 22 | +* Unless ``--quick``: maint/benchmark/compare_benchmarks.py against the |
| 23 | + previous release tag (``--previous`` overrides the guess). |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse |
| 27 | +import os |
| 28 | +import re |
| 29 | +import subprocess |
| 30 | +import sys |
| 31 | +import tempfile |
| 32 | +from typing import NamedTuple |
| 33 | + |
| 34 | +_TAG_RE = re.compile(r"v(\d+)\.(\d+)\.(\d+)(?:(a|b|rc)(\d+))?") |
| 35 | +_PRERELEASE_INFO = {"a": -99, "b": -98, "rc": -97} |
| 36 | + |
| 37 | + |
| 38 | +class Version(NamedTuple): |
| 39 | + version: str |
| 40 | + version_info: tuple[int, int, int, int] |
| 41 | + notes: str |
| 42 | + |
| 43 | + |
| 44 | +def expected_version(tag: str) -> Version: |
| 45 | + """Returns what tornado/__init__.py and the notes should say for ``tag``. |
| 46 | +
|
| 47 | + >>> expected_version("v6.5.0") |
| 48 | + Version(version='6.5', version_info=(6, 5, 0, 0), notes='v6.5.0') |
| 49 | + >>> expected_version("v6.5.10") |
| 50 | + Version(version='6.5.10', version_info=(6, 5, 10, 0), notes='v6.5.10') |
| 51 | + >>> expected_version("v6.6.0b1") |
| 52 | + Version(version='6.6b1', version_info=(6, 6, 0, -98), notes='v6.6.0') |
| 53 | + """ |
| 54 | + m = _TAG_RE.fullmatch(tag) |
| 55 | + if m is None: |
| 56 | + raise ValueError(f"not a release tag: {tag!r}") |
| 57 | + major, minor, patch = int(m[1]), int(m[2]), int(m[3]) |
| 58 | + base = f"{major}.{minor}" if patch == 0 else f"{major}.{minor}.{patch}" |
| 59 | + if m[4]: |
| 60 | + # Pre-releases have used the same fourth element for every |
| 61 | + # number so far (6.6a1 and 6.5b1 alike), so only the kind is |
| 62 | + # checked. |
| 63 | + return Version( |
| 64 | + base + m[4] + m[5], |
| 65 | + (major, minor, patch, _PRERELEASE_INFO[m[4]]), |
| 66 | + f"v{major}.{minor}.{patch}", |
| 67 | + ) |
| 68 | + return Version(base, (major, minor, patch, 0), tag) |
| 69 | + |
| 70 | + |
| 71 | +def git(*args: str) -> str: |
| 72 | + return subprocess.run( |
| 73 | + ["git", *args], check=True, capture_output=True, text=True |
| 74 | + ).stdout.strip() |
| 75 | + |
| 76 | + |
| 77 | +def check_metadata(tag: str) -> list[str]: |
| 78 | + errors = [] |
| 79 | + if git("status", "--porcelain"): |
| 80 | + errors.append("the working tree has uncommitted changes") |
| 81 | + |
| 82 | + expected = expected_version(tag) |
| 83 | + with open("tornado/__init__.py") as f: |
| 84 | + init = f.read() |
| 85 | + version = re.search(r'^version = "(.*)"$', init, re.M) |
| 86 | + info = re.search(r"^version_info = (\(.*\))$", init, re.M) |
| 87 | + if version is None or version[1] != expected.version: |
| 88 | + errors.append( |
| 89 | + f"tornado.version is {version and version[1]!r}, expected " |
| 90 | + f"{expected.version!r}" |
| 91 | + ) |
| 92 | + if info is None or info[1] != repr(expected.version_info): |
| 93 | + errors.append( |
| 94 | + f"tornado.version_info is {info and info[1]}, expected " |
| 95 | + f"{expected.version_info}" |
| 96 | + ) |
| 97 | + |
| 98 | + notes = f"docs/releases/{expected.notes}.rst" |
| 99 | + if not os.path.exists(notes): |
| 100 | + errors.append(f"{notes} does not exist") |
| 101 | + else: |
| 102 | + with open("docs/releases.rst") as f: |
| 103 | + if f"releases/{expected.notes}\n" not in f.read(): |
| 104 | + errors.append(f"{notes} is not listed in docs/releases.rst") |
| 105 | + with open(notes) as f: |
| 106 | + if expected.version_info[3] == 0 and re.search(r"\bXX\b", f.read()): |
| 107 | + errors.append(f"{notes} still has a placeholder (XX) date") |
| 108 | + return errors |
| 109 | + |
| 110 | + |
| 111 | +def previous_tag(tag: str) -> str: |
| 112 | + # The highest final release below tag. Release branches aren't merged |
| 113 | + # back, so this can't be found from the commit history. |
| 114 | + target = expected_version(tag).version_info[:3] |
| 115 | + finals = [] |
| 116 | + for t in git("tag", "--list", "v*").split(): |
| 117 | + m = _TAG_RE.fullmatch(t) |
| 118 | + if m and not m[4]: |
| 119 | + v = (int(m[1]), int(m[2]), int(m[3])) |
| 120 | + if v < target: |
| 121 | + finals.append((v, t)) |
| 122 | + if not finals: |
| 123 | + raise ValueError("no previous release tag found") |
| 124 | + return max(finals)[1] |
| 125 | + |
| 126 | + |
| 127 | +def run(*cmd: str) -> bool: |
| 128 | + print("+", " ".join(cmd), flush=True) |
| 129 | + return subprocess.run(cmd).returncode == 0 |
| 130 | + |
| 131 | + |
| 132 | +def main() -> int: |
| 133 | + parser = argparse.ArgumentParser() |
| 134 | + parser.add_argument("tag", help="the tag to be pushed, such as v6.7.0b1") |
| 135 | + parser.add_argument("--previous", help="release to compare benchmarks with") |
| 136 | + parser.add_argument( |
| 137 | + "--quick", action="store_true", help="skip the tests and benchmarks" |
| 138 | + ) |
| 139 | + args = parser.parse_args() |
| 140 | + |
| 141 | + errors = check_metadata(args.tag) |
| 142 | + for e in errors: |
| 143 | + print("error:", e) |
| 144 | + if not args.quick: |
| 145 | + if not run(sys.executable, "-m", "tox", "-e", "py3-full,lint,docs,coverage"): |
| 146 | + errors.append("tox failed") |
| 147 | + previous = args.previous or previous_tag(args.tag) |
| 148 | + with tempfile.TemporaryDirectory() as tmp: |
| 149 | + base = os.path.join(tmp, previous) |
| 150 | + git("worktree", "add", "--detach", base, previous) |
| 151 | + try: |
| 152 | + if not run( |
| 153 | + sys.executable, |
| 154 | + "maint/benchmark/compare_benchmarks.py", |
| 155 | + "--base", |
| 156 | + base, |
| 157 | + "--head", |
| 158 | + ".", |
| 159 | + ): |
| 160 | + errors.append(f"benchmarks regressed since {previous}") |
| 161 | + finally: |
| 162 | + git("worktree", "remove", "--force", base) |
| 163 | + if errors: |
| 164 | + print(f"\n{len(errors)} problem(s); do not tag {args.tag} yet.") |
| 165 | + return 1 |
| 166 | + print(f"\nAll checks passed for {args.tag}. See maint/RELEASING.md for the rest.") |
| 167 | + return 0 |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + sys.exit(main()) |
0 commit comments