"""Assemble and build the Nuitka wheel with setuptools.""" from __future__ import annotations import re import shutil import subprocess import sys from pathlib import Path _BUILD_ROOT = Path(__file__).resolve().parents[1] if str(_BUILD_ROOT) not in sys.path: sys.path.insert(0, str(_BUILD_ROOT)) from common.paths import sdk_dir # PyPI rejects bare tags like "manylinux_x86_64"; require a glibc ABI version. _MANYLINUX_TAG_RE = re.compile( r"(?:manylinux2014|manylinux_2_(?:17|24|27|28|31|34|35))_x86_64" ) def write_wheel_setup( wheel_dir: Path, *, version: str, package_data: dict[str, list[str]], install_requires: list[str], extras_require: dict[str, list[str]], ) -> Path: setup_py_path = wheel_dir / "setup.py" setup_content = f""" from setuptools import setup, Distribution class BinaryDistribution(Distribution): def has_ext_modules(self): return True setup( name="orisys", version="{version}", description="Orisys - Optical Tactile Sensor SDK (Nuitka Compiled)", author="Orisys", packages=['orisys', 'orisys.examples', 'orisys_blend'], package_dir={{'': 'wheel_root'}}, package_data={package_data}, include_package_data=True, install_requires={install_requires!r}, extras_require={extras_require!r}, distclass=BinaryDistribution, zip_safe=False, ) """ setup_py_path.write_text(setup_content, encoding="utf-8") return setup_py_path def run_bdist_wheel(wheel_dir: Path) -> Path | None: print("\n=== Building Wheel ===") subprocess.check_call([sys.executable, "setup.py", "bdist_wheel"], cwd=wheel_dir) wheels = sorted((wheel_dir / "dist").glob("*.whl")) return wheels[0] if wheels else None def _ensure_auditwheel() -> None: try: subprocess.check_call( [sys.executable, "-m", "auditwheel", "--version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) except (subprocess.CalledProcessError, FileNotFoundError): print("Installing auditwheel...") subprocess.check_call([sys.executable, "-m", "pip", "install", "auditwheel", "-q"]) def is_compliant_manylinux_wheel(wheel_path: Path) -> bool: """True if the wheel filename uses a PyPI-accepted manylinux x86_64 tag.""" return bool(_MANYLINUX_TAG_RE.search(wheel_path.name)) def repair_linux_wheel(wheel_path: Path, repair_dir: Path) -> Path: """Run auditwheel show/repair and return a PyPI-compliant manylinux wheel. Raises RuntimeError if repair fails or the result still lacks a valid tag. On non-Linux platforms the input wheel is returned unchanged. """ if sys.platform != "linux": print("Skipping auditwheel repair (not running on Linux).") return wheel_path _ensure_auditwheel() repair_dir = repair_dir.resolve() if repair_dir.exists(): shutil.rmtree(repair_dir) repair_dir.mkdir(parents=True) print("\n=== auditwheel show ===") subprocess.check_call([sys.executable, "-m", "auditwheel", "show", str(wheel_path)]) print("\n=== auditwheel repair ===") subprocess.check_call( [ sys.executable, "-m", "auditwheel", "repair", str(wheel_path), "--wheel-dir", str(repair_dir), ] ) repaired = sorted(repair_dir.glob("*.whl")) if not repaired: raise RuntimeError(f"auditwheel repair produced no wheels in {repair_dir}") compliant = [path for path in repaired if is_compliant_manylinux_wheel(path)] if not compliant: names = ", ".join(path.name for path in repaired) raise RuntimeError( "auditwheel repair did not produce a PyPI-compliant manylinux tag. " f"Got: {names}. Rebuild inside quay.io/pypa/manylinux_2_28_x86_64 " "(or manylinux2014_x86_64) instead of a generic Linux host." ) final = compliant[0] print(f"Repaired wheel: {final.name}") return final def publish_wheel_to_sdk(wheel_path: Path, version: str) -> Path: sdk_version_dir = sdk_dir(version) sdk_version_dir.mkdir(parents=True, exist_ok=True) dest_wheel = sdk_version_dir / wheel_path.name if dest_wheel.exists(): dest_wheel.unlink() shutil.copy2(str(wheel_path), str(dest_wheel)) return dest_wheel