Files
nvaldez c0d756c4e7 phronesis v0.4.0-dev: publish.py - guarded PyPI release, ported from daedalus
- publish.py at repo root: guards (clean tree, v<version> tag on HEAD,
  build+twine present, version free on PyPI), chained sdist->wheel build,
  twine check, exact-version twine upload; exit codes 2-9 per guard
- tag guard accepts roller tags: uses git tag --points-at HEAD so
  stable/latest beside v<version> do not trip it
- MANIFEST.in ships skill/ in the sdist
- test/test_publish.py: 12 tests via injected runner + fetch, no network
- requirements.txt: build, twine

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-19 10:35:59 +01:00

204 lines
7.5 KiB
Python

"""Release phronesis to PyPI: guarded build, check, upload.
Copied from daedalus python/src/publish/publish.py and adapted for this
repository: the script lives at the repo root, and the exact-tag guard
accepts the release tag among several tags on HEAD (v<version> plus the
stable/latest rollers). Guards, build, and upload only run after the
tree is clean, HEAD carries v<version>, the version is not yet on PyPI,
and the build and twine tooling is importable in this interpreter.
Credentials are never handled by this script: twine prompts or reads
``~/.pypirc``.
"""
from __future__ import annotations
import argparse
import logging
import pathlib
import subprocess
import sys
import tomllib
import urllib.error
import urllib.request
try: # completion is optional and never breaks the script
from argcomplete import autocomplete
except ImportError: # pragma: no cover - completion off without argcomplete
autocomplete = None
LOGGER = logging.getLogger(__name__)
EXIT_OK = 0
EXIT_NO_REPO = 2
EXIT_DIRTY = 3
EXIT_TAG = 4
EXIT_TOOLING = 5
EXIT_PYPI = 6
EXIT_BUILD = 7
EXIT_CHECK = 8
EXIT_UPLOAD = 9
PY = sys.executable
PYPI_VERSION_URL = "https://pypi.org/pypi/{name}/{version}/json"
def find_repo_root() -> pathlib.Path | None:
"""The checkout root beside the script, or None."""
root = pathlib.Path(__file__).resolve().parent
return root if (root / "pyproject.toml").is_file() else None
def read_project(pyproject: pathlib.Path) -> tuple[str, str]:
"""Read [project] name and version from pyproject.toml."""
project = tomllib.loads(
pyproject.read_text(encoding="utf-8"))["project"]
return project["name"], project["version"]
def run_in_cwd(cmd: list[str],
cwd: pathlib.Path | None = None) -> tuple[int, str]:
"""Run a command (in cwd when given); return (code, output)."""
try:
result = subprocess.run(cmd, capture_output=True, text=True,
check=False,
cwd=str(cwd) if cwd else None)
except FileNotFoundError:
return 127, f"{' '.join(cmd)}: not found"
return result.returncode, (result.stdout or "") + (result.stderr or "")
def pypi_status(url: str) -> int | None:
"""HTTP status of a PyPI JSON URL, or None when unreachable."""
try:
with urllib.request.urlopen(url, timeout=30) as response:
return response.status
except urllib.error.HTTPError as err:
return err.code
except (urllib.error.URLError, ValueError):
return None
def artifact_names(name: str, version: str) -> tuple[str, str]:
"""The version's sdist and wheel file names.
The wheel is a pure-Python wheel, so its name follows
``<name>-<version>-py3-none-any.whl``.
"""
return (f"{name}-{version}.tar.gz",
f"{name}-{version}-py3-none-any.whl")
def cmd_publish(args: argparse.Namespace, run_cmd=None,
fetch_status=None) -> int:
"""publish flow: guards, build, check, upload.
Args:
args: parsed arguments (upload).
run_cmd: ``(cmd, cwd) -> (code, output)`` runner;
defaults to run_in_cwd.
fetch_status: ``(url) -> HTTP status | None``;
defaults to pypi_status.
Returns:
Exit code: 0 checked (or uploaded); 2 no checkout; 3 dirty
tree; 4 tag missing; 5 tooling missing; 6 version on PyPI or
PyPI unreachable; 7 build failed; 8 twine check failed;
9 upload failed.
"""
if run_cmd is None:
run_cmd = run_in_cwd
if fetch_status is None:
fetch_status = pypi_status
root = find_repo_root()
if root is None:
LOGGER.error("not a phronesis checkout: no pyproject.toml "
"beside %s", __file__)
return EXIT_NO_REPO
name, version = read_project(root / "pyproject.toml")
code, output = run_cmd(["git", "status", "--porcelain"], cwd=root)
if code != 0 or output.strip():
LOGGER.error("working tree is dirty (or git failed); commit "
"or stash first - what ships must be committed")
return EXIT_DIRTY
code, output = run_cmd(["git", "tag", "--points-at", "HEAD"], cwd=root)
tags = [line.strip() for line in output.splitlines() if line.strip()]
if code != 0 or f"v{version}" not in tags:
LOGGER.error("HEAD is not tagged v%s (it carries %s); tag the "
"release first: git tag -a v%s -m \"...\"",
version, ", ".join(tags) or "no tags", version)
return EXIT_TAG
for module in ("build", "twine"):
code, _output = run_cmd([PY, "-m", module, "--version"], cwd=root)
if code != 0:
LOGGER.error("python -m %s is unavailable in this "
"interpreter; pipx inject phronesis build "
"twine, or pip install -r requirements.txt",
module)
return EXIT_TOOLING
status = fetch_status(
PYPI_VERSION_URL.format(name=name, version=version))
if status == 200:
LOGGER.error("%s %s is already on PyPI; a published version is "
"immutable - bump the version", name, version)
return EXIT_PYPI
if status != 404:
LOGGER.error("could not check PyPI (status %s); never publish "
"blind", status)
return EXIT_PYPI
sdist, wheel = artifact_names(name, version)
print(f"release : {name} {version} "
"(tree clean, tag exact, PyPI free)")
print("build : python -m build (sdist -> wheel, chained)")
code, output = run_cmd([PY, "-m", "build"], cwd=root)
if code != 0:
LOGGER.error("build failed; fastest repro: %s -m build --wheel"
"\n%s", PY, output.strip())
return EXIT_BUILD
code, output = run_cmd([PY, "-m", "twine", "check",
f"dist/{sdist}", f"dist/{wheel}"], cwd=root)
if code != 0:
LOGGER.error("twine check failed:\n%s", output.strip())
return EXIT_CHECK
print(f"artifacts : dist/{sdist}, dist/{wheel}")
if not args.upload:
print("checked; upload this exact version with: "
"python publish.py --upload")
return EXIT_OK
print("upload : twine (credentials from ~/.pypirc or the "
"prompt; username __token__ with your PyPI API token)")
code, output = run_cmd([PY, "-m", "twine", "upload",
f"dist/{sdist}", f"dist/{wheel}"], cwd=root)
if code != 0:
LOGGER.error("twine upload failed:\n%s", output.strip())
return EXIT_UPLOAD
print(f"uploaded : {name} {version} -> "
f"https://pypi.org/project/{name}/{version}/")
return EXIT_OK
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
"""Parse publish command-line arguments."""
parser = argparse.ArgumentParser(
prog="publish",
description="Release phronesis to PyPI: guarded build, twine "
"check, optional upload",
)
parser.add_argument("--upload", action="store_true",
help="upload this exact version to PyPI after "
"the checks pass (default: build and "
"check only)")
if autocomplete is not None:
autocomplete(parser)
return parser.parse_args(argv)
def main(argv: list[str] | None = None) -> int:
"""CLI entry point; return the exit code."""
logging.basicConfig(level=logging.INFO,
format="%(levelname)-5s %(message)s")
return cmd_publish(parse_args(argv), run_in_cwd, pypi_status)
if __name__ == "__main__":
sys.exit(main())