hatch_build.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from __future__ import annotations
  2. import os
  3. import platform
  4. import stat
  5. from pathlib import Path
  6. from hatchling.builders.hooks.plugin.interface import BuildHookInterface
  7. _PLATFORMS = {
  8. "linux-x64": ("manylinux_2_28_x86_64", "dsh-jsonrpc-agent-pkg-linux-x64"),
  9. "linux-arm64": ("manylinux_2_28_aarch64", "dsh-jsonrpc-agent-pkg-linux-arm64"),
  10. "macos-arm64": ("macosx_11_0_arm64", "dsh-jsonrpc-agent-pkg-macos-arm64"),
  11. }
  12. def _host_platform_tag() -> str:
  13. machine = platform.machine().lower()
  14. arch = "arm64" if machine in {"arm64", "aarch64"} else "x64" if machine in {"x86_64", "amd64"} else machine
  15. system = platform.system().lower()
  16. key = f"macos-{arch}" if system == "darwin" else f"linux-{arch}" if system == "linux" else system
  17. try:
  18. return _PLATFORMS[key][0]
  19. except KeyError as exc:
  20. raise RuntimeError(f"unsupported deepseek-harness-runtime-bin build platform: {key}") from exc
  21. class RuntimeBuildHook(BuildHookInterface):
  22. """Assign the native wheel tag and reject incomplete or mixed-platform payloads."""
  23. def initialize(self, version: str, build_data: dict[str, object]) -> None:
  24. if version == "editable":
  25. return
  26. if self.target_name == "sdist":
  27. raise RuntimeError(
  28. "deepseek-harness-runtime-bin is wheel-only; build and publish platform wheels only."
  29. )
  30. platform_tag = os.environ.get("DSH_RUNTIME_PLATFORM_TAG") or _host_platform_tag()
  31. matches = [value for value in _PLATFORMS.values() if value[0] == platform_tag]
  32. if len(matches) != 1:
  33. supported = ", ".join(value[0] for value in _PLATFORMS.values())
  34. raise RuntimeError(
  35. f"unsupported DSH_RUNTIME_PLATFORM_TAG {platform_tag!r}; expected one of {supported}"
  36. )
  37. expected_executable = matches[0][1]
  38. runtime_dir = Path(self.root) / "src" / "deepseek_harness_runtime" / "runtime"
  39. executables = sorted(runtime_dir.glob("dsh-jsonrpc-agent-pkg-*") if runtime_dir.is_dir() else [])
  40. if [path.name for path in executables] != [expected_executable]:
  41. found = ", ".join(path.name for path in executables) or "none"
  42. raise RuntimeError(
  43. f"runtime wheel {platform_tag} must contain only {expected_executable}; found {found}"
  44. )
  45. if executables[0].stat().st_mode & stat.S_IXUSR == 0:
  46. raise RuntimeError(f"runtime executable is not executable: {executables[0]}")
  47. build_data["pure_python"] = False
  48. build_data["infer_tag"] = False
  49. build_data["tag"] = f"py3-none-{platform_tag}"