hatch_build.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from __future__ import annotations
  2. import json
  3. import os
  4. import platform
  5. import stat
  6. from pathlib import Path
  7. from hatchling.builders.hooks.plugin.interface import BuildHookInterface
  8. def _load_platforms() -> dict[str, tuple[str, str]]:
  9. """Load and validate the platform manifest inside an isolated wheel build."""
  10. path = Path(__file__).with_name("platforms.json")
  11. try:
  12. payload = json.loads(path.read_text())
  13. except (OSError, json.JSONDecodeError) as error:
  14. raise RuntimeError(f"could not read runtime platform manifest from {path}") from error
  15. if not isinstance(payload, dict) or not payload:
  16. raise RuntimeError(f"{path} must contain a non-empty platform object")
  17. platforms: dict[str, tuple[str, str]] = {}
  18. for name, raw in payload.items():
  19. if (
  20. not isinstance(name, str)
  21. or not isinstance(raw, dict)
  22. or set(raw) != {"tag", "executable"}
  23. or not isinstance(raw["tag"], str)
  24. or not isinstance(raw["executable"], str)
  25. ):
  26. raise RuntimeError(f"{path} platform entries must contain string tag and executable fields")
  27. platforms[name] = (raw["tag"], raw["executable"])
  28. return platforms
  29. _PLATFORMS = _load_platforms()
  30. def _host_platform_tag() -> str:
  31. machine = platform.machine().lower()
  32. arch = "arm64" if machine in {"arm64", "aarch64"} else "x64" if machine in {"x86_64", "amd64"} else machine
  33. system = platform.system().lower()
  34. key = f"macos-{arch}" if system == "darwin" else f"linux-{arch}" if system == "linux" else system
  35. try:
  36. return _PLATFORMS[key][0]
  37. except KeyError as exc:
  38. raise RuntimeError(f"unsupported deepseek-harness-runtime-bin build platform: {key}") from exc
  39. class RuntimeBuildHook(BuildHookInterface):
  40. """Assign the native wheel tag and reject incomplete or mixed-platform payloads."""
  41. def initialize(self, version: str, build_data: dict[str, object]) -> None:
  42. if version == "editable":
  43. return
  44. if self.target_name == "sdist":
  45. raise RuntimeError(
  46. "deepseek-harness-runtime-bin is wheel-only; build and publish platform wheels only."
  47. )
  48. platform_tag = os.environ.get("DSH_RUNTIME_PLATFORM_TAG") or _host_platform_tag()
  49. matches = [value for value in _PLATFORMS.values() if value[0] == platform_tag]
  50. if len(matches) != 1:
  51. supported = ", ".join(value[0] for value in _PLATFORMS.values())
  52. raise RuntimeError(
  53. f"unsupported DSH_RUNTIME_PLATFORM_TAG {platform_tag!r}; expected one of {supported}"
  54. )
  55. expected_executable = matches[0][1]
  56. runtime_dir = Path(self.root) / "src" / "deepseek_harness_runtime" / "runtime"
  57. runtime_files = sorted(runtime_dir.glob("dsh-jsonrpc-agent-pkg-*") if runtime_dir.is_dir() else [])
  58. expected_files = [expected_executable, f"{expected_executable}-rg"]
  59. if "-macos-" in expected_executable:
  60. expected_files.append(f"{expected_executable}-spawn-helper")
  61. found_files = [path.name for path in runtime_files]
  62. if found_files != expected_files:
  63. raise RuntimeError(
  64. f"runtime wheel {platform_tag} payload must be {expected_files}; found {found_files}"
  65. )
  66. for executable in runtime_files:
  67. if executable.stat().st_mode & stat.S_IXUSR == 0:
  68. raise RuntimeError(f"runtime executable is not executable: {executable}")
  69. build_data["pure_python"] = False
  70. build_data["infer_tag"] = False
  71. build_data["tag"] = f"py3-none-{platform_tag}"