From f5cd9f6ed2aa4416ddac7a061ff9ae9891ae9a26 Mon Sep 17 00:00:00 2001 From: Ezra Brooks Date: Wed, 4 Nov 2026 14:09:30 -0700 Subject: [PATCH] Conditional Pythons 3.12-3.14 for lyrical From #193, on top of the vinca-rs stack: vinca-rs builds the packages with Python content for each Python (conditional_python_dependency) and the others once. A package's classification is overridden with python in add_host or remove_host in pkg_additional_info.yaml. rattler-build 0.81 has the fix the temporary pre-release pin was for (prefix-dev/rattler-build#2931, released in 0.80.1), and lyrical keeps the shared Qt 6.12. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 5 + README.md | 10 ++ distros/lyrical/conda_build_config.yaml | 11 +- distros/lyrical/distro.yaml | 19 ++- distros/lyrical/vinca.yaml | 7 +- tools/python_abi_audit.py | 216 ++++++++++++++++++++++++ tools/robostack.py | 2 + tools/tests/test_python_abi_audit.py | 30 ++++ 8 files changed, 291 insertions(+), 9 deletions(-) create mode 100644 tools/python_abi_audit.py create mode 100644 tools/tests/test_python_abi_audit.py diff --git a/AGENTS.md b/AGENTS.md index cb4f13126..ca5a5294f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,6 +22,10 @@ to read without Claude): into `distros//`. - Run everything per distribution with `pixi run rs `, from the repository root. Package names are `ros2-` (dashes), e.g. `ros2-rsl`. +- lyrical builds packages with Python content for Python 3.12-3.14 and the others once + (`conditional_python_dependency`, see README "Python versions"). A built-once package + that fails its `package_contents` test ships Python after all: add `python` to its + `add_host` in `pkg_additional_info.yaml`. - Never edit `conda_build_config.yaml` by hand: change `shared/pinning/` or the distribution's `distro.yaml`, then `pixi run rs render-pinning` (CI checks it). - A change outside `distros/` rebuilds every distribution in CI; keep such PRs focused. @@ -52,6 +56,7 @@ pixi run rs build --continue-on-failure # everything missing on the chan pixi run rs check-patches [--recipe ros2-] pixi run rs check-deps # pin conflicts, before building anything pixi run rs gap-report # recipes without artifacts and vice versa +pixi run rs python-audit [--elf] # per-Python vs built-once recipes against the published packages pixi run rs add-package ... [...] # select packages (as the bot does) pixi run rs check # sanity checks (CI) ``` diff --git a/README.md b/README.md index 669ed408c..2dbbd7773 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,16 @@ pixi run rs check # sanity checks (also run in CI) of a distribution uses `full_rebuild: true` there. A new build number goes into its `pkg_additional_info.yaml` (one package) or `vinca.yaml` (everything). +**Python versions:** a distribution's `pinning_overrides` can list several Pythons, as +lyrical does for 3.12-3.14. With `conditional_python_dependency: true` in its +`vinca.yaml`, only packages with Python content (Python modules, rosidl Python bindings, +extensions) get python in host/run and are built once per Python; the others are built +once, without a `python_abi` dependency, and fail their build if they ship +`site-packages` content or a CPython extension anyway. When vinca-rs's guess from +`package.xml` is wrong, add `python` to the package's `add_host` (or `remove_host`) in +`pkg_additional_info.yaml`. `pixi run rs python-audit [--elf]` compares the +guess with the published packages and lists the misses. + **Building only some distributions in a PR:** a PR builds the distributions it changes, and all of them as soon as it touches anything outside `distros//`. To limit that (e.g. a humble rebuild that also changes `shared/` or `tools/`), add the label diff --git a/distros/lyrical/conda_build_config.yaml b/distros/lyrical/conda_build_config.yaml index 939e50355..cab359277 100644 --- a/distros/lyrical/conda_build_config.yaml +++ b/distros/lyrical/conda_build_config.yaml @@ -998,8 +998,9 @@ pythia8: python: # conda-forge supports only 3.14+ for win-arm64 and linux-riscv64 # part of a zip_keys: python, is_python_min + - 3.12.* *_cpython # [not ((win and arm64) or riscv64)] + - 3.13.* *_cp313 # [not ((win and arm64) or riscv64)] - 3.14.* *_cp314 - - 3.12.* *_cpython python_impl: - cpython @@ -1007,14 +1008,16 @@ python_impl: python_min: # minimum supported python version per CFEP-25 # bump to next minor version when we drop python versions - - '3.11' # [not ((win and arm64) or riscv64)] + - '3.12' # [not ((win and arm64) or riscv64)] - '3.14' # [(win and arm64) or riscv64] is_freethreading: - false is_python_min: # part of a zip_keys: python, is_python_min - - false - - false + - true # [not ((win and arm64) or riscv64)] + - false # [not ((win and arm64) or riscv64)] + - false # [not ((win and arm64) or riscv64)] + - true # [(win and arm64) or riscv64] is_abi3: - true pytorch: diff --git a/distros/lyrical/distro.yaml b/distros/lyrical/distro.yaml index ce31e3f74..ea1dd1a6e 100644 --- a/distros/lyrical/distro.yaml +++ b/distros/lyrical/distro.yaml @@ -7,7 +7,18 @@ upload_to: [prefix, anaconda] read_from: prefix rosdistro_sync: lyrical/2026-10-05 pinning_overrides: - # build every python package for 3.14 (the shared default) and 3.12; - # is_python_min is zipped with python, so it needs one entry per version - python: [3.14.* *_cp314, 3.12.* *_cpython] - is_python_min: [false, false] + # Lyrical supports Python 3.12-3.14 (REP 2000). Packages with Python content are + # built for each (vinca.yaml: conditional_python_dependency), the others once. + # conda-forge has only 3.14+ for win-arm64 and linux-riscv64, so those get one variant. + python: + - 3.12.* *_cpython # [not ((win and arm64) or riscv64)] + - 3.13.* *_cp313 # [not ((win and arm64) or riscv64)] + - 3.14.* *_cp314 + is_python_min: + - true # [not ((win and arm64) or riscv64)] + - false # [not ((win and arm64) or riscv64)] + - false # [not ((win and arm64) or riscv64)] + - true # [(win and arm64) or riscv64] + python_min: + - '3.12' # [not ((win and arm64) or riscv64)] + - '3.14' # [(win and arm64) or riscv64] diff --git a/distros/lyrical/vinca.yaml b/distros/lyrical/vinca.yaml index 15070a041..6b5bf9f9d 100644 --- a/distros/lyrical/vinca.yaml +++ b/distros/lyrical/vinca.yaml @@ -3,7 +3,12 @@ ros_distro: lyrical patch_dir: patch rosdistro_snapshot: rosdistro_snapshot.yaml rosdistro_additional_recipes: rosdistro_additional_recipes.yaml -build_number: 30 +build_number: 31 +# Only packages with Python content get python in host/run, and with it the Python +# variants of distro.yaml (cp312-cp314); the others are built once, without a +# python_abi dependency. Override the classification per package with python in +# add_host (or remove_host) in pkg_additional_info.yaml. +conditional_python_dependency: true mutex_package: version: 0.22.0 diff --git a/tools/python_abi_audit.py b/tools/python_abi_audit.py new file mode 100644 index 000000000..3106729ee --- /dev/null +++ b/tools/python_abi_audit.py @@ -0,0 +1,216 @@ +"""Check vinca's Python classification (conditional_python_dependency) against the +published packages. + + pixi run rs lyrical python-audit # linux-64, info/paths.json only + pixi run rs lyrical python-audit --elf # also scan ELF files for libpython + pixi run rs lyrical python-audit --platform osx-arm64 --json audit.json + +A generated recipe (work/recipes) either has python in host, and is built for every +Python of the distribution's pinning, or not, and is built once. This compares that +with what the newest published build of each ros2-* package actually contains: + +- native: CPython extensions (*.cpython-*.so, *.pyd), rosidl_generator_py libraries, + or (--elf) ELF files that link libpython; +- pure: only Python modules (site-packages); +- none: no Python content. + +A recipe built once whose published package has Python content is a miss: that +content would only work with the Python it happened to be built with. Misses make +the exit status 1; fix them with `add_host: ["python"]` in pkg_additional_info.yaml. +Recipes built per Python without Python content in the package are only reported +(an unneeded rebuild per Python, not a broken package). + +Run it before the switch to per-Python builds, while the channel still has the +packages that were built with python in host (their content is complete). +""" + +from __future__ import annotations + +import argparse +import io +import json +import re +import sys +import tarfile +import urllib.request +import zipfile +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +import yaml +import zstandard + +UA = {"User-Agent": "robostack-python-audit"} +SITE_PACKAGES = re.compile(r"(^|/)(lib/python[0-9.]*/site-packages|Lib/site-packages)/") +EXTENSION = re.compile(r"\.cpython-[^/]*\.(so|dylib)$|\.pyd$") + + +class RangeReader(io.RawIOBase): + """A remote file read with HTTP range requests (zipfile only needs the end and + the info-*.tar.zst member of a .conda).""" + + def __init__(self, url: str): + with urllib.request.urlopen(urllib.request.Request(url, headers={**UA, "Range": "bytes=0-0"}), + timeout=120) as r: + self.url = r.url + content_range = r.headers.get("Content-Range") + if r.status != 206 or not content_range: + raise OSError(f"{url}: no range support") + self.size = int(content_range.split("/")[1]) + self.pos = 0 + + def readable(self) -> bool: + return True + + def seekable(self) -> bool: + return True + + def tell(self) -> int: + return self.pos + + def seek(self, offset: int, whence: int = 0) -> int: + self.pos = {0: offset, 1: self.pos + offset, 2: self.size + offset}[whence] + return self.pos + + def readinto(self, b) -> int: + n = min(len(b), self.size - self.pos) + if n <= 0: + return 0 + headers = {**UA, "Range": f"bytes={self.pos}-{self.pos + n - 1}"} + with urllib.request.urlopen(urllib.request.Request(self.url, headers=headers), timeout=300) as r: + data = r.read() + b[: len(data)] = data + self.pos += len(data) + return len(data) + + +def open_conda(url: str, whole: bool) -> zipfile.ZipFile: + if not whole: + try: + return zipfile.ZipFile(io.BufferedReader(RangeReader(url), buffer_size=1 << 16)) + except OSError: + pass + with urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=600) as r: + return zipfile.ZipFile(io.BytesIO(r.read())) + + +def members(z: zipfile.ZipFile, prefix: str): + name = next(n for n in z.namelist() if n.startswith(prefix)) + stream = zstandard.ZstdDecompressor().stream_reader(z.open(name)) + with tarfile.open(fileobj=stream, mode="r|") as tar: + for member in tar: + if member.isfile(): + yield member, tar.extractfile(member) + + +def inspect(url: str, elf: bool) -> dict: + """Classify by info/paths.json; with *elf*, a package without Python files is + downloaded whole and its ELF files are searched for libpython (embedding).""" + for attempt in range(3): + try: + z = open_conda(url, whole=False) + paths = [] + for member, f in members(z, "info-"): + if member.name == "info/paths.json": + paths = [p["_path"] for p in json.load(f)["paths"]] + break + result = classify(paths, []) + if elf and result["content"] == "none": + linked = [member.name for member, f in members(open_conda(url, whole=True), "pkg-") + if (data := f.read())[:4] == b"\x7fELF" and b"libpython3." in data] + result = classify(paths, linked) + return result + except Exception as err: # noqa: BLE001 - network or archive trouble: retried, then reported + error = err + return {"content": "error", "error": str(error)} + + +def classify(paths: list[str], linked: list[str]) -> dict: + site = [p for p in paths if SITE_PACKAGES.search(p)] + native = [p for p in paths if EXTENSION.search(p)] + native += [p for p in paths if "rosidl_generator_py" in p and re.search(r"\.(so|dylib|dll)$", p)] + native += linked + content = "native" if native else "pure" if site else "none" + return {"content": content, "examples": (native or site)[:3]} + + +def flatten(requirements) -> list[str]: + out = [] + for r in requirements or []: + if isinstance(r, dict): + out += flatten(r.get("then")) + flatten(r.get("else")) + else: + out.append(str(r)) + return out + + +def recipes_needing_python(recipes: Path) -> dict[str, bool]: + result = {} + for path in sorted(recipes.glob("ros2-*/recipe.yaml")): + recipe = yaml.safe_load(path.read_text()) + host = flatten((recipe.get("requirements") or {}).get("host")) + result[recipe["package"]["name"]] = "python" in host + return result + + +def newest_builds(repodata: dict) -> dict[str, str]: + records = {**repodata.get("packages", {}), **repodata.get("packages.conda", {})} + newest: dict[str, tuple] = {} + for fn, rec in records.items(): + key = (rec["build_number"], rec.get("timestamp", 0)) + if rec["name"].startswith("ros2-") and key > newest.get(rec["name"], ((-1, 0), ""))[0]: + newest[rec["name"]] = (key, fn) + return {name: fn for name, (_, fn) in newest.items()} + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--channel", required=True, help="channel URL, e.g. https://prefix.dev/robostack-lyrical") + parser.add_argument("--platform", default="linux-64") + parser.add_argument("--recipes", default="recipes", type=Path, help="generated recipes (default: ./recipes)") + parser.add_argument("--elf", action="store_true", + help="download the packages without Python files and look for libpython in their ELF files") + parser.add_argument("--json", type=Path, help="write the per-package results here") + args = parser.parse_args() + + needs = recipes_needing_python(args.recipes) + if not needs: + print(f"No ros2-* recipes in {args.recipes}: run generate-recipes first", file=sys.stderr) + return 1 + with urllib.request.urlopen(urllib.request.Request(f"{args.channel}/{args.platform}/repodata.json", headers=UA), + timeout=300) as r: + published = newest_builds(json.load(r)) + + names = [n for n in needs if n in published] + def work(name: str) -> tuple[str, dict]: + url = f"{args.channel}/{args.platform}/{published[name]}" + return name, {"file": published[name], **inspect(url, elf=args.elf)} + + with ThreadPoolExecutor(16) as pool: + results = dict(pool.map(work, names)) + + misses = sorted(n for n, r in results.items() if not needs[n] and r["content"] in ("native", "pure")) + unneeded = sorted(n for n, r in results.items() if needs[n] and r["content"] == "none") + errors = sorted(n for n, r in results.items() if r["content"] == "error") + counts: dict[tuple[bool, str], int] = {} + for n, r in results.items(): + counts[(needs[n], r["content"])] = counts.get((needs[n], r["content"]), 0) + 1 + + print(f"{len(needs)} recipes, {len(results)} with a published {args.platform} package, " + f"{len(needs) - len(results)} unpublished") + for (per_python, content), count in sorted(counts.items()): + print(f" {'per Python' if per_python else 'built once':10} package content {content:7} {count}") + for name in misses: + print(f"MISS {name}: built once, but ships {results[name]['content']} Python: {results[name]['examples']}") + if unneeded: + print(f"{len(unneeded)} recipes are built per Python without Python content: {', '.join(unneeded)}") + for name in errors: + print(f"ERROR {name}: {results[name]['error']}", file=sys.stderr) + if args.json: + args.json.write_text(json.dumps({n: {"per_python": needs[n], **r} for n, r in results.items()}, + indent=1, sort_keys=True) + "\n") + return 1 if misses or errors else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/robostack.py b/tools/robostack.py index 6dc653a90..3e5687e38 100644 --- a/tools/robostack.py +++ b/tools/robostack.py @@ -588,6 +588,8 @@ def task(distro: str, name: str, args: list[str]) -> int: return run(py + [str(TOOLS / "check_dependency_compat.py"), *args], w) if name == "gap-report": return run(py + [str(TOOLS / "build_gap_report.py"), *args], w) + if name == "python-audit": + return run(py + [str(TOOLS / "python_abi_audit.py"), "--channel", repodata_url(distro), *args], w) if name in ("build", "build-one"): if not (w / "recipes").is_dir(): print(f"No {(w / 'recipes').relative_to(ROOT)}: run `pixi run rs {distro} generate-recipes` first", diff --git a/tools/tests/test_python_abi_audit.py b/tools/tests/test_python_abi_audit.py new file mode 100644 index 000000000..580f858fd --- /dev/null +++ b/tools/tests/test_python_abi_audit.py @@ -0,0 +1,30 @@ +import python_abi_audit as audit + + +def test_classify_by_package_content(): + assert audit.classify(["share/a/package.xml", "share/a/launch/__pycache__/x.cpython-314.pyc"], [])["content"] == "none" + assert audit.classify(["lib/python3.14/site-packages/a/__init__.py"], [])["content"] == "pure" + assert audit.classify(["lib/python/site-packages/a/__init__.py"], [])["content"] == "pure" + assert audit.classify(["Lib/site-packages/a/__init__.py"], [])["content"] == "pure" + assert audit.classify(["lib/a/_a.cpython-314-x86_64-linux-gnu.so"], [])["content"] == "native" + assert audit.classify(["Lib/site-packages/a/_a.cp314-win_amd64.pyd"], [])["content"] == "native" + assert audit.classify(["lib/liba_msgs__rosidl_generator_py.so"], [])["content"] == "native" + assert audit.classify(["lib/a/node"], ["lib/a/node"])["content"] == "native" + + +def test_recipes_needing_python_reads_host(tmp_path): + for name, host in (("ros2-a", "[{if: unix, then: [python]}, ros2-b]"), + ("ros2-b", "[\"python ${{ python_min }}.*\"]"), + ("ros-lyrical-a", "[python]")): + (tmp_path / name).mkdir() + (tmp_path / name / "recipe.yaml").write_text(f"package: {{name: {name}}}\nrequirements:\n host: {host}\n") + assert audit.recipes_needing_python(tmp_path) == {"ros2-a": True, "ros2-b": False} + + +def test_newest_builds_takes_the_highest_build_number(): + def rec(name, number, timestamp=0): + return {"name": name, "build_number": number, "timestamp": timestamp} + + repodata = {"packages.conda": {"a-1-h_30.conda": rec("ros2-a", 30), "a-1-h_29.conda": rec("ros2-a", 29, 9), + "a-1-g_30.conda": rec("ros2-a", 30, 5), "x.conda": rec("ros-lyrical-a", 31)}} + assert audit.newest_builds(repodata) == {"ros2-a": "a-1-g_30.conda"}