hatch_build.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. _SPAWN_HELPER_SUFFIX = "-spawn-helper"
  13. def _host_platform_tag() -> str:
  14. machine = platform.machine().lower()
  15. arch = "arm64" if machine in {"arm64", "aarch64"} else "x64" if machine in {"x86_64", "amd64"} else machine
  16. system = platform.system().lower()
  17. key = f"macos-{arch}" if system == "darwin" else f"linux-{arch}" if system == "linux" else system
  18. try:
  19. return _PLATFORMS[key][0]
  20. except KeyError as exc:
  21. raise RuntimeError(f"unsupported deepseek-harness-runtime-bin build platform: {key}") from exc
  22. class RuntimeBuildHook(BuildHookInterface):
  23. """Assign the native wheel tag and reject incomplete or mixed-platform payloads."""
  24. def initialize(self, version: str, build_data: dict[str, object]) -> None:
  25. if version == "editable":
  26. return
  27. if self.target_name == "sdist":
  28. raise RuntimeError(
  29. "deepseek-harness-runtime-bin is wheel-only; build and publish platform wheels only."
  30. )
  31. platform_tag = os.environ.get("DSH_RUNTIME_PLATFORM_TAG") or _host_platform_tag()
  32. matches = [value for value in _PLATFORMS.values() if value[0] == platform_tag]
  33. if len(matches) != 1:
  34. supported = ", ".join(value[0] for value in _PLATFORMS.values())
  35. raise RuntimeError(
  36. f"unsupported DSH_RUNTIME_PLATFORM_TAG {platform_tag!r}; expected one of {supported}"
  37. )
  38. expected_executable = matches[0][1]
  39. runtime_dir = Path(self.root) / "src" / "deepseek_harness_runtime" / "runtime"
  40. runtime_files = sorted(runtime_dir.glob("dsh-jsonrpc-agent-pkg-*") if runtime_dir.is_dir() else [])
  41. executables = [path for path in runtime_files if not path.name.endswith(_SPAWN_HELPER_SUFFIX)]
  42. helpers = [path for path in runtime_files if path.name.endswith(_SPAWN_HELPER_SUFFIX)]
  43. if [path.name for path in executables] != [expected_executable]:
  44. found = ", ".join(path.name for path in executables) or "none"
  45. raise RuntimeError(
  46. f"runtime wheel {platform_tag} must contain only {expected_executable}; found {found}"
  47. )
  48. expected_helper = f"{expected_executable}{_SPAWN_HELPER_SUFFIX}"
  49. if [path.name for path in helpers] != [expected_helper]:
  50. found = ", ".join(path.name for path in helpers) or "none"
  51. raise RuntimeError(
  52. f"runtime wheel {platform_tag} must contain only {expected_helper}; found {found}"
  53. )
  54. for executable in [executables[0], helpers[0]]:
  55. if executable.stat().st_mode & stat.S_IXUSR == 0:
  56. raise RuntimeError(f"runtime executable is not executable: {executable}")
  57. build_data["pure_python"] = False
  58. build_data["infer_tag"] = False
  59. build_data["tag"] = f"py3-none-{platform_tag}"