|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build a PyPI alias distribution for the ksadk package. |
| 3 | +
|
| 4 | +The alias package keeps the Python import/package layout as ``ksadk`` and only |
| 5 | +changes the distribution metadata name. It is used for the compatibility PyPI |
| 6 | +project ``agentengine-sdk-python``. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import re |
| 13 | +import shutil |
| 14 | +import subprocess |
| 15 | +import tempfile |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | + |
| 19 | +ROOT = Path(__file__).resolve().parents[1] |
| 20 | +DEFAULT_ALIAS_PROJECT = "agentengine-sdk-python" |
| 21 | +DEFAULT_ALIAS_DESCRIPTION = "Kingsoft Cloud Agent Engine SDK alias package for ksadk" |
| 22 | + |
| 23 | +IGNORED_DIRS = { |
| 24 | + ".git", |
| 25 | + ".mypy_cache", |
| 26 | + ".pytest_cache", |
| 27 | + ".ruff_cache", |
| 28 | + ".venv", |
| 29 | + ".worktrees", |
| 30 | + ".zread", |
| 31 | + "__pycache__", |
| 32 | + "build", |
| 33 | + "dist", |
| 34 | + "dist-alias", |
| 35 | + "htmlcov", |
| 36 | + "ksadk.egg-info", |
| 37 | + "node_modules", |
| 38 | + "site", |
| 39 | +} |
| 40 | +IGNORED_SUFFIXES = (".pyc", ".pyo") |
| 41 | + |
| 42 | + |
| 43 | +def _ignore(_dir: str, names: list[str]) -> set[str]: |
| 44 | + ignored: set[str] = set() |
| 45 | + for name in names: |
| 46 | + if name in IGNORED_DIRS or name.endswith(IGNORED_SUFFIXES): |
| 47 | + ignored.add(name) |
| 48 | + return ignored |
| 49 | + |
| 50 | + |
| 51 | +def _rewrite_pyproject(pyproject: Path, *, alias_project: str, description: str) -> None: |
| 52 | + text = pyproject.read_text(encoding="utf-8") |
| 53 | + text, name_count = re.subn( |
| 54 | + r'(?m)^name = "ksadk"$', |
| 55 | + f'name = "{alias_project}"', |
| 56 | + text, |
| 57 | + count=1, |
| 58 | + ) |
| 59 | + text, description_count = re.subn( |
| 60 | + r'(?m)^description = ".*"$', |
| 61 | + f'description = "{description}"', |
| 62 | + text, |
| 63 | + count=1, |
| 64 | + ) |
| 65 | + if name_count != 1: |
| 66 | + raise RuntimeError("pyproject.toml 中未找到唯一的 ksadk project name") |
| 67 | + if description_count != 1: |
| 68 | + raise RuntimeError("pyproject.toml 中未找到唯一的 project description") |
| 69 | + pyproject.write_text(text, encoding="utf-8") |
| 70 | + |
| 71 | + |
| 72 | +def build_alias_distribution( |
| 73 | + *, |
| 74 | + alias_project: str, |
| 75 | + description: str, |
| 76 | + out_dir: Path, |
| 77 | +) -> None: |
| 78 | + out_dir = out_dir.resolve() |
| 79 | + if out_dir.exists(): |
| 80 | + shutil.rmtree(out_dir) |
| 81 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 82 | + |
| 83 | + with tempfile.TemporaryDirectory(prefix="ksadk-alias-src-") as tmp: |
| 84 | + alias_root = Path(tmp) / "src" |
| 85 | + shutil.copytree(ROOT, alias_root, ignore=_ignore) |
| 86 | + _rewrite_pyproject( |
| 87 | + alias_root / "pyproject.toml", |
| 88 | + alias_project=alias_project, |
| 89 | + description=description, |
| 90 | + ) |
| 91 | + subprocess.run( |
| 92 | + ["uv", "build", "--out-dir", str(out_dir)], |
| 93 | + cwd=alias_root, |
| 94 | + check=True, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def main() -> int: |
| 99 | + parser = argparse.ArgumentParser() |
| 100 | + parser.add_argument("--alias-project", default=DEFAULT_ALIAS_PROJECT) |
| 101 | + parser.add_argument("--description", default=DEFAULT_ALIAS_DESCRIPTION) |
| 102 | + parser.add_argument("--out-dir", type=Path, default=ROOT / "dist-alias") |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + build_alias_distribution( |
| 106 | + alias_project=args.alias_project, |
| 107 | + description=args.description, |
| 108 | + out_dir=args.out_dir, |
| 109 | + ) |
| 110 | + return 0 |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + raise SystemExit(main()) |
0 commit comments