From c0d756c4e7fba5f605e7eb816793d03b0b684b36 Mon Sep 17 00:00:00 2001 From: nvaldez Date: Thu, 19 Nov 2026 10:35:59 +0100 Subject: [PATCH] phronesis v0.4.0-dev: publish.py - guarded PyPI release, ported from daedalus - publish.py at repo root: guards (clean tree, v 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 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 --- MANIFEST.in | 7 ++ publish.py | 203 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 + test/test_publish.py | 193 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 407 insertions(+) create mode 100644 MANIFEST.in create mode 100644 publish.py create mode 100644 test/test_publish.py diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..e9b496c --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,7 @@ +# ship the bundled skills in the sdist (they build into the wheel as +# phronesis._skills package data) +recursive-include skill * + +include LICENSE +include SPEC.md +include CONTRIBUTING.md diff --git a/publish.py b/publish.py new file mode 100644 index 0000000..4c24668 --- /dev/null +++ b/publish.py @@ -0,0 +1,203 @@ +"""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 plus the +stable/latest rollers). Guards, build, and upload only run after the +tree is clean, HEAD carries v, 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 + ``--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()) diff --git a/requirements.txt b/requirements.txt index 20bf87e..cac7d49 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,7 @@ shellcheck-py>=0.10 setuptools>=68 pytest>=8.0 pytest-cov>=5.0 + +# release tooling (publish.py: python -m build / twine) +build>=1.2 +twine>=6.0 diff --git a/test/test_publish.py b/test/test_publish.py new file mode 100644 index 0000000..bdf006f --- /dev/null +++ b/test/test_publish.py @@ -0,0 +1,193 @@ +"""Tests for publish.py: guarded PyPI release (ported from daedalus). + +No network, no git state, no real build: every guard and step runs +through an injected runner and an injectable PyPI status fetch. +""" +import importlib.util +import pathlib +import re + +REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent +_SPEC = importlib.util.spec_from_file_location( + "publish", REPO_ROOT / "publish.py") +publish = importlib.util.module_from_spec(_SPEC) +_SPEC.loader.exec_module(publish) + + +def make_runner(script, calls=None): + """Build an injectable run_cmd(cmd, cwd) from {prefix: (code, out)}.""" + + def run_cmd(cmd, cwd=None): + if calls is not None: + calls.append((list(cmd), cwd)) + for prefix, reply in script.items(): + if list(cmd)[:len(prefix)] == list(prefix): + return reply + raise AssertionError(f"unexpected command: {cmd}") + + return run_cmd + + +def repo_version(): + """The version the checkout's pyproject carries.""" + _name, version = publish.read_project( + publish.find_repo_root() / "pyproject.toml") + return version + + +def clean_runner(version, calls=None): + """A runner with every guard green for `version` (plus roller tags).""" + py = publish.PY + return make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (0, f"stable\nv{version}\nlatest\n"), + (py, "-m", "build", "--version"): (0, "build 1.6.1\n"), + (py, "-m", "twine", "--version"): (0, "twine 7.0.0\n"), + (py, "-m", "build"): (0, "Successfully built\n"), + (py, "-m", "twine", "check"): (0, "PASSED\n"), + (py, "-m", "twine", "upload"): (0, "Uploaded\n"), + }, calls) + + +def test_read_project_from_this_repo(): + """The script's own repo resolves: name phronesis, semver version.""" + root = publish.find_repo_root() + assert root is not None + name, version = publish.read_project(root / "pyproject.toml") + assert name == "phronesis" + assert re.fullmatch(r"\d+\.\d+\.\d+", version) + + +def test_dirty_tree_stops_before_building(): + """A dirty tree is exit 3; no build runs.""" + calls = [] + runner = make_runner({("git", "status"): (0, " M file\n")}, calls) + args = publish.parse_args([]) + assert publish.cmd_publish(args, runner, + lambda _u: 404) == 3 + assert not any(cmd[:2] == ["git", "tag"] + or cmd[1:3] == ["-m", "build"] + for cmd, _cwd in calls) + + +def test_tag_missing_stops(): + """A HEAD without v among its tags is exit 4.""" + calls = [] + runner = make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (0, "stable\nlatest\n"), + }, calls) + assert publish.cmd_publish(publish.parse_args([]), runner, + lambda _u: 404) == 4 + + +def test_tag_probe_failure_stops(): + """A failing tag probe is exit 4.""" + runner = make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (128, "no tags\n"), + }) + assert publish.cmd_publish(publish.parse_args([]), runner, + lambda _u: 404) == 4 + + +def test_roller_tags_do_not_confuse_the_guard(): + """v among stable/latest on the same HEAD passes the guard.""" + assert publish.cmd_publish(publish.parse_args([]), + clean_runner(repo_version()), + lambda _u: 404) == 0 + + +def test_tooling_missing_stops(): + """No build/twine in the interpreter is exit 5.""" + py = publish.PY + runner = make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), + (py, "-m", "build", "--version"): (127, "no module"), + (py, "-m", "twine", "--version"): (0, "twine\n"), + }) + assert publish.cmd_publish(publish.parse_args([]), runner, + lambda _u: 404) == 5 + + +def test_already_on_pypi_stops(): + """A published version is exit 6 before any artifact is built.""" + calls = [] + py = publish.PY + runner = make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), + (py, "-m", "build", "--version"): (0, "build\n"), + (py, "-m", "twine", "--version"): (0, "twine\n"), + }, calls) + assert publish.cmd_publish(publish.parse_args([]), runner, + lambda _u: 200) == 6 + assert not any(cmd[1:3] == ["-m", "build"] and len(cmd) == 3 + for cmd, _cwd in calls) + + +def test_unreachable_pypi_stops(): + """An unreachable PyPI is exit 6 (never publish blind).""" + py = publish.PY + runner = make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), + (py, "-m", "build", "--version"): (0, "build\n"), + (py, "-m", "twine", "--version"): (0, "twine\n"), + }) + assert publish.cmd_publish(publish.parse_args([]), runner, + lambda _u: None) == 6 + + +def test_build_failure_is_seven(): + """A failed build is exit 7.""" + py = publish.PY + runner = make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), + (py, "-m", "build", "--version"): (0, "build\n"), + (py, "-m", "twine", "--version"): (0, "twine\n"), + (py, "-m", "build"): (1, "boom"), + }) + assert publish.cmd_publish(publish.parse_args([]), runner, + lambda _u: 404) == 7 + + +def test_twine_check_failure_is_eight(): + """A failed twine check is exit 8.""" + py = publish.PY + runner = make_runner({ + ("git", "status"): (0, ""), + ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), + (py, "-m", "build", "--version"): (0, "build\n"), + (py, "-m", "twine", "--version"): (0, "twine\n"), + (py, "-m", "build"): (0, "built\n"), + (py, "-m", "twine", "check"): (1, "README failed"), + }) + assert publish.cmd_publish(publish.parse_args([]), runner, + lambda _u: 404) == 8 + + +def test_check_without_upload_never_uploads(): + """Without --upload the flow stops after the check (exit 0).""" + calls = [] + assert publish.cmd_publish(publish.parse_args([]), + clean_runner(repo_version(), calls), + lambda _u: 404) == 0 + assert not any(cmd[1:4] == ["-m", "twine", "upload"] + for cmd, _cwd in calls) + + +def test_upload_selects_exactly_this_version(): + """--upload twine-ups exactly the version's sdist and wheel.""" + calls = [] + version = repo_version() + assert publish.cmd_publish(publish.parse_args(["--upload"]), + clean_runner(version, calls), + lambda _u: 404) == 0 + upload = [cmd for cmd, _cwd in calls + if cmd[1:4] == ["-m", "twine", "upload"]] + assert len(upload) == 1 + sdist, wheel = publish.artifact_names("phronesis", version) + assert upload[0][-2:] == [f"dist/{sdist}", f"dist/{wheel}"]