hatch_build.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 = [(key, value) for key, value in _PLATFORMS.items() 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_target, (_, expected_executable) = matches[0]
  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. expected_helpers = [expected_helper] if expected_target.startswith("macos-") else []
  50. if [path.name for path in helpers] != expected_helpers:
  51. expected = ", ".join(expected_helpers) or "none"
  52. found = ", ".join(path.name for path in helpers) or "none"
  53. raise RuntimeError(
  54. f"runtime wheel {platform_tag} helper payload mismatch: expected {expected}; found {found}"
  55. )
  56. for executable in [executables[0], *helpers]:
  57. if executable.stat().st_mode & stat.S_IXUSR == 0:
  58. raise RuntimeError(f"runtime executable is not executable: {executable}")
  59. build_data["pure_python"] = False
  60. build_data["infer_tag"] = False
  61. build_data["tag"] = f"py3-none-{platform_tag}"