hatch_build.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 _spawn_helper_binary_target(header: bytes) -> str | None:
  14. if len(header) >= 8 and header[:4] == b"\xcf\xfa\xed\xfe":
  15. cpu_type = int.from_bytes(header[4:8], "little")
  16. if cpu_type == 0x01000007:
  17. return "macos-x64"
  18. if cpu_type == 0x0100000C:
  19. return "macos-arm64"
  20. return None
  21. def _validate_spawn_helper(path: Path, expected_target: str) -> None:
  22. with path.open("rb") as helper:
  23. actual_target = _spawn_helper_binary_target(helper.read(8))
  24. if actual_target != expected_target:
  25. raise RuntimeError(
  26. f"runtime spawn helper binary mismatch: expected {expected_target}, "
  27. f"found {actual_target or 'unsupported format or architecture'} at {path}"
  28. )
  29. def _host_platform_tag() -> str:
  30. machine = platform.machine().lower()
  31. arch = "arm64" if machine in {"arm64", "aarch64"} else "x64" if machine in {"x86_64", "amd64"} else machine
  32. system = platform.system().lower()
  33. key = f"macos-{arch}" if system == "darwin" else f"linux-{arch}" if system == "linux" else system
  34. try:
  35. return _PLATFORMS[key][0]
  36. except KeyError as exc:
  37. raise RuntimeError(f"unsupported deepseek-harness-runtime-bin build platform: {key}") from exc
  38. class RuntimeBuildHook(BuildHookInterface):
  39. """Assign the native wheel tag and reject incomplete or mixed-platform payloads."""
  40. def initialize(self, version: str, build_data: dict[str, object]) -> None:
  41. if version == "editable":
  42. return
  43. if self.target_name == "sdist":
  44. raise RuntimeError(
  45. "deepseek-harness-runtime-bin is wheel-only; build and publish platform wheels only."
  46. )
  47. platform_tag = os.environ.get("DSH_RUNTIME_PLATFORM_TAG") or _host_platform_tag()
  48. matches = [(key, value) for key, value in _PLATFORMS.items() if value[0] == platform_tag]
  49. if len(matches) != 1:
  50. supported = ", ".join(value[0] for value in _PLATFORMS.values())
  51. raise RuntimeError(
  52. f"unsupported DSH_RUNTIME_PLATFORM_TAG {platform_tag!r}; expected one of {supported}"
  53. )
  54. expected_target, (_, expected_executable) = matches[0]
  55. runtime_dir = Path(self.root) / "src" / "deepseek_harness_runtime" / "runtime"
  56. runtime_files = sorted(runtime_dir.glob("dsh-jsonrpc-agent-pkg-*") if runtime_dir.is_dir() else [])
  57. executables = [path for path in runtime_files if not path.name.endswith(_SPAWN_HELPER_SUFFIX)]
  58. helpers = [path for path in runtime_files if path.name.endswith(_SPAWN_HELPER_SUFFIX)]
  59. if [path.name for path in executables] != [expected_executable]:
  60. found = ", ".join(path.name for path in executables) or "none"
  61. raise RuntimeError(
  62. f"runtime wheel {platform_tag} must contain only {expected_executable}; found {found}"
  63. )
  64. expected_helper = f"{expected_executable}{_SPAWN_HELPER_SUFFIX}"
  65. expected_helpers = [expected_helper] if expected_target.startswith("macos-") else []
  66. if [path.name for path in helpers] != expected_helpers:
  67. expected = ", ".join(expected_helpers) or "none"
  68. found = ", ".join(path.name for path in helpers) or "none"
  69. raise RuntimeError(
  70. f"runtime wheel {platform_tag} helper payload mismatch: expected {expected}; found {found}"
  71. )
  72. for executable in [executables[0], *helpers]:
  73. if executable.stat().st_mode & stat.S_IXUSR == 0:
  74. raise RuntimeError(f"runtime executable is not executable: {executable}")
  75. if helpers:
  76. _validate_spawn_helper(helpers[0], expected_target)
  77. build_data["pure_python"] = False
  78. build_data["infer_tag"] = False
  79. build_data["tag"] = f"py3-none-{platform_tag}"