|
| 1 | +"""Release workflow validation helpers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import ast |
| 7 | +import pathlib |
| 8 | +import re |
| 9 | +from collections.abc import Sequence |
| 10 | + |
| 11 | +try: |
| 12 | + import tomllib |
| 13 | +except ModuleNotFoundError: |
| 14 | + import toml as tomllib # type: ignore[no-redef] |
| 15 | + |
| 16 | + |
| 17 | +def _checked_in_version() -> str: |
| 18 | + pyproject_version = tomllib.loads(pathlib.Path("pyproject.toml").read_text())[ |
| 19 | + "project" |
| 20 | + ]["version"] |
| 21 | + service_tree = ast.parse(pathlib.Path("temporalio/service.py").read_text()) |
| 22 | + service_version = None |
| 23 | + for stmt in service_tree.body: |
| 24 | + if ( |
| 25 | + isinstance(stmt, ast.Assign) |
| 26 | + and any( |
| 27 | + isinstance(target, ast.Name) and target.id == "__version__" |
| 28 | + for target in stmt.targets |
| 29 | + ) |
| 30 | + and isinstance(stmt.value, ast.Constant) |
| 31 | + and isinstance(stmt.value.value, str) |
| 32 | + ): |
| 33 | + service_version = stmt.value.value |
| 34 | + break |
| 35 | + |
| 36 | + if pyproject_version != service_version: |
| 37 | + raise RuntimeError( |
| 38 | + f"pyproject.toml version {pyproject_version!r} does not match " |
| 39 | + f"temporalio/service.py version {service_version!r}" |
| 40 | + ) |
| 41 | + if pyproject_version.startswith("v"): |
| 42 | + raise RuntimeError("Checked-in version must not start with 'v'") |
| 43 | + if not re.fullmatch(r"[0-9]+(?:\.[0-9]+)+(?:[a-zA-Z0-9_.+-]+)?", pyproject_version): |
| 44 | + raise RuntimeError(f"Invalid checked-in version: {pyproject_version!r}") |
| 45 | + return pyproject_version |
| 46 | + |
| 47 | + |
| 48 | +def _write_github_output(path: pathlib.Path, *, version: str, sha: str) -> None: |
| 49 | + with path.open("a", encoding="utf-8") as output: |
| 50 | + print(f"version={version}", file=output) |
| 51 | + print(f"sha={sha}", file=output) |
| 52 | + |
| 53 | + |
| 54 | +def validate_version(args: argparse.Namespace) -> None: |
| 55 | + version = _checked_in_version() |
| 56 | + if args.github_output: |
| 57 | + _write_github_output( |
| 58 | + pathlib.Path(args.github_output), |
| 59 | + version=version, |
| 60 | + sha=args.sha, |
| 61 | + ) |
| 62 | + else: |
| 63 | + print(version) |
| 64 | + |
| 65 | + |
| 66 | +def verify_dist(args: argparse.Namespace) -> None: |
| 67 | + dist_dir = pathlib.Path(args.dist_dir) |
| 68 | + files = sorted(path.name for path in dist_dir.iterdir() if path.is_file()) |
| 69 | + wheels = [name for name in files if name.endswith(".whl")] |
| 70 | + sdists = [name for name in files if name.endswith(".tar.gz")] |
| 71 | + |
| 72 | + if len(files) != len(set(files)): |
| 73 | + raise RuntimeError("Duplicate distribution filenames found") |
| 74 | + expected_sdist = f"temporalio-{args.version}.tar.gz" |
| 75 | + if sdists != [expected_sdist]: |
| 76 | + raise RuntimeError(f"Expected only sdist {expected_sdist!r}, found {sdists!r}") |
| 77 | + if len(wheels) != 5: |
| 78 | + raise RuntimeError( |
| 79 | + f"Expected 5 platform wheels, found {len(wheels)}: {wheels!r}" |
| 80 | + ) |
| 81 | + |
| 82 | + for name in files: |
| 83 | + if not name.startswith(f"temporalio-{args.version}"): |
| 84 | + raise RuntimeError( |
| 85 | + f"Distribution filename does not match requested version " |
| 86 | + f"{args.version!r}: {name}" |
| 87 | + ) |
| 88 | + |
| 89 | + expected_platforms = { |
| 90 | + "linux-x86_64": lambda name: "manylinux" in name and "x86_64" in name, |
| 91 | + "linux-aarch64": lambda name: "manylinux" in name and "aarch64" in name, |
| 92 | + "macos-x86_64": lambda name: "macosx" in name and "x86_64" in name, |
| 93 | + "macos-arm64": lambda name: "macosx" in name and "arm64" in name, |
| 94 | + "windows-amd64": lambda name: "win_amd64" in name, |
| 95 | + } |
| 96 | + missing = [ |
| 97 | + platform |
| 98 | + for platform, predicate in expected_platforms.items() |
| 99 | + if not any(predicate(name) for name in wheels) |
| 100 | + ] |
| 101 | + if missing: |
| 102 | + raise RuntimeError( |
| 103 | + f"Missing expected platform wheels: {missing!r}; found {wheels!r}" |
| 104 | + ) |
| 105 | + |
| 106 | + print("Verified release artifacts:") |
| 107 | + for name in files: |
| 108 | + print(f" {name}") |
| 109 | + |
| 110 | + |
| 111 | +def main(argv: Sequence[str] | None = None) -> None: |
| 112 | + parser = argparse.ArgumentParser() |
| 113 | + subparsers = parser.add_subparsers(required=True) |
| 114 | + |
| 115 | + validate_parser = subparsers.add_parser("validate-version") |
| 116 | + validate_parser.add_argument("--sha", required=True) |
| 117 | + validate_parser.add_argument("--github-output") |
| 118 | + validate_parser.set_defaults(func=validate_version) |
| 119 | + |
| 120 | + verify_parser = subparsers.add_parser("verify-dist") |
| 121 | + verify_parser.add_argument("--version", required=True) |
| 122 | + verify_parser.add_argument("--dist-dir", default="dist") |
| 123 | + verify_parser.set_defaults(func=verify_dist) |
| 124 | + |
| 125 | + args = parser.parse_args(argv) |
| 126 | + args.func(args) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main() |
0 commit comments