hatch_build.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = (
  35. f"macos-{arch}"
  36. if system == "darwin"
  37. else f"linux-{arch}"
  38. if system == "linux"
  39. else f"win-{arch}"
  40. if system == "windows"
  41. else system
  42. )
  43. try:
  44. return _PLATFORMS[key][0]
  45. except KeyError as exc:
  46. raise RuntimeError(f"unsupported deepseek-harness-runtime-bin build platform: {key}") from exc
  47. class RuntimeBuildHook(BuildHookInterface):
  48. """Assign the native wheel tag and reject incomplete or mixed-platform payloads."""
  49. def initialize(self, version: str, build_data: dict[str, object]) -> None:
  50. if version == "editable":
  51. return
  52. if self.target_name == "sdist":
  53. raise RuntimeError(
  54. "deepseek-harness-runtime-bin is wheel-only; build and publish platform wheels only."
  55. )
  56. platform_tag = os.environ.get("DSH_RUNTIME_PLATFORM_TAG") or _host_platform_tag()
  57. matches = [value for value in _PLATFORMS.values() if value[0] == platform_tag]
  58. if len(matches) != 1:
  59. supported = ", ".join(value[0] for value in _PLATFORMS.values())
  60. raise RuntimeError(
  61. f"unsupported DSH_RUNTIME_PLATFORM_TAG {platform_tag!r}; expected one of {supported}"
  62. )
  63. expected_executable = matches[0][1]
  64. runtime_dir = Path(self.root) / "src" / "deepseek_harness_runtime" / "runtime"
  65. runtime_files = sorted(
  66. runtime_dir.glob("deepseek-harness-sdk-runtime-*") if runtime_dir.is_dir() else []
  67. )
  68. expected_files = (
  69. [expected_executable, f"{expected_executable.removesuffix('.exe')}-rg.exe"]
  70. if expected_executable.endswith(".exe")
  71. else [expected_executable, f"{expected_executable}-rg"]
  72. )
  73. if "-macos-" in expected_executable:
  74. expected_files.append(f"{expected_executable}-spawn-helper")
  75. expected_files.sort()
  76. found_files = [path.name for path in runtime_files]
  77. if found_files != expected_files:
  78. raise RuntimeError(
  79. f"runtime wheel {platform_tag} payload must be {expected_files}; found {found_files}"
  80. )
  81. for executable in runtime_files:
  82. if platform_tag != "win_amd64" and executable.stat().st_mode & stat.S_IXUSR == 0:
  83. raise RuntimeError(f"runtime executable is not executable: {executable}")
  84. build_data["pure_python"] = False
  85. build_data["infer_tag"] = False
  86. build_data["tag"] = f"py3-none-{platform_tag}"