|
| 1 | +import shutil |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import zipfile |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def _read_version(cargo_toml: Path) -> str: |
| 10 | + for line in cargo_toml.read_text(encoding="utf-8").splitlines(): |
| 11 | + if line.strip().startswith("version"): |
| 12 | + return line.split("=", 1)[1].strip().strip('"') |
| 13 | + raise SystemExit("Version not found in Cargo.toml") |
| 14 | + |
| 15 | + |
| 16 | +def _load_setup_template(template_path: Path, version: str) -> str: |
| 17 | + template = template_path.read_text(encoding="utf-8") |
| 18 | + if "__VERSION__" not in template: |
| 19 | + raise SystemExit("setup.py template missing __VERSION__ placeholder") |
| 20 | + return template.replace("__VERSION__", version) |
| 21 | + |
| 22 | + |
| 23 | +def main() -> None: |
| 24 | + repo_root = Path.cwd() |
| 25 | + dist_dir = repo_root / "dist" |
| 26 | + cargo_toml = repo_root / "Cargo.toml" |
| 27 | + build_src = repo_root / "build_bin.py" |
| 28 | + ipsearcher_src = repo_root / "example_production.py" |
| 29 | + setup_template = repo_root / ".github" / "scripts" / "setup.py" |
| 30 | + |
| 31 | + if not cargo_toml.exists(): |
| 32 | + raise SystemExit("Cargo.toml not found") |
| 33 | + if not build_src.exists(): |
| 34 | + raise SystemExit("build_bin.py not found") |
| 35 | + if not ipsearcher_src.exists(): |
| 36 | + raise SystemExit("example_production.py not found") |
| 37 | + if not setup_template.exists(): |
| 38 | + raise SystemExit("setup.py template not found") |
| 39 | + |
| 40 | + version = _read_version(cargo_toml) |
| 41 | + setup_py = _load_setup_template(setup_template, version) |
| 42 | + |
| 43 | + wheels = sorted(dist_dir.glob("*.whl")) |
| 44 | + if not wheels: |
| 45 | + raise SystemExit("No wheels found in dist") |
| 46 | + |
| 47 | + for wheel in wheels: |
| 48 | + temp_dir = Path(tempfile.mkdtemp()) |
| 49 | + with zipfile.ZipFile(wheel, "r") as zf: |
| 50 | + zf.extractall(temp_dir) |
| 51 | + package_dir = temp_dir / "poptrie" |
| 52 | + if not package_dir.exists(): |
| 53 | + raise SystemExit(f"poptrie package not found in {wheel.name}") |
| 54 | + |
| 55 | + suffixes = (".so", ".pyd", ".dll", ".dylib") |
| 56 | + for ext_file in temp_dir.iterdir(): |
| 57 | + if ext_file.is_file() and ext_file.name.startswith("poptrie") and ext_file.suffix in suffixes: |
| 58 | + target = package_dir / ext_file.name |
| 59 | + if not target.exists(): |
| 60 | + shutil.move(str(ext_file), str(target)) |
| 61 | + |
| 62 | + shutil.copy2(build_src, package_dir / "build.py") |
| 63 | + shutil.copy2(ipsearcher_src, package_dir / "ipsearcher.py") |
| 64 | + (temp_dir / "setup.py").write_text(setup_py, encoding="utf-8") |
| 65 | + |
| 66 | + wheel.unlink() |
| 67 | + subprocess.run( |
| 68 | + [sys.executable, "setup.py", "bdist_wheel"], |
| 69 | + cwd=temp_dir, |
| 70 | + check=True, |
| 71 | + ) |
| 72 | + built_wheels = sorted((temp_dir / "dist").glob("*.whl")) |
| 73 | + if not built_wheels: |
| 74 | + raise SystemExit("bdist_wheel did not produce a wheel") |
| 75 | + for built_wheel in built_wheels: |
| 76 | + shutil.copy2(built_wheel, dist_dir / built_wheel.name) |
| 77 | + shutil.rmtree(temp_dir) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments