setup.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. setup.py — wheel/sdist build guard.
  3. pip/PyPI and Homebrew are no longer supported distribution methods for
  4. Hermes Agent (see website/docs/getting-started/platform-support.md). The
  5. wheel would ship without bundled assets (locales, skills, optional-mcps,
  6. web_dist, tui_dist, plugin manifests) since those are resolved at runtime
  7. via env-var overrides set by the nix wrapper or the source-checkout layout.
  8. This file overrides the ``bdist_wheel`` and ``sdist`` setuptools commands
  9. to raise an error when run outside a Nix build. The PEP 517
  10. ``build_wheel`` / ``build_sdist`` hooks in
  11. ``setuptools.build_meta`` call these commands internally, so the guard
  12. fires for ``uv build``, ``pip wheel``, ``python -m build``, and direct
  13. ``setup.py`` invocations alike.
  14. The one legitimate consumer of ``build_wheel`` is uv2nix, which calls
  15. ``setuptools.build_meta.build_wheel`` (→ ``bdist_wheel``) inside a Nix
  16. build sandbox. ``nix/python.nix`` sets ``HERMES_NIX_BUILD=1`` on the
  17. Hermes package derivation, so only that build may create an artifact.
  18. Editable installs (``uv sync``, ``pip install -e .``, ``nix develop``)
  19. use ``build_editable``, which does NOT call ``bdist_wheel`` — it calls
  20. ``build_ext`` in editable mode. So the guard does not affect development.
  21. """
  22. import os
  23. from setuptools import setup
  24. from setuptools.command.sdist import sdist
  25. _ROOT = os.path.dirname(os.path.abspath(__file__))
  26. _IN_NIX_BUILD = os.environ.get("HERMES_NIX_BUILD") == "1"
  27. _BLOCK_MESSAGE = (
  28. "Building wheels or sdists for hermes-agent is not supported.\n"
  29. "Hermes is distributed via the shell installer, Docker image, or Nix.\n"
  30. "See: https://hermes-agent.nousresearch.com/docs/getting-started/installation\n"
  31. "\n"
  32. "If you are developing, use an editable install instead:\n"
  33. " source ./activate # PowerShell: . .\\activate.ps1\n"
  34. "\n"
  35. "If you are building with Nix (uv2nix), this error should not fire —\n"
  36. "the Hermes Nix derivation sets HERMES_NIX_BUILD=1. If it does, file a bug."
  37. )
  38. class _GuardedSdist(sdist):
  39. def run(self, *args, **kwargs):
  40. if not _IN_NIX_BUILD:
  41. raise RuntimeError(_BLOCK_MESSAGE)
  42. return super().run(*args, **kwargs)
  43. cmdclass = {"sdist": _GuardedSdist}
  44. # bdist_wheel is only available when the `wheel` package is installed.
  45. # setuptools.build_meta.build_wheel() calls it internally, so the guard
  46. # fires for all PEP 517 wheel build paths. Define the subclass only when
  47. # the import succeeds — otherwise a None base class raises TypeError at
  48. # class-definition time, before the cmdclass guard can run.
  49. try:
  50. from setuptools.command.bdist_wheel import bdist_wheel
  51. class _GuardedBdistWheel(bdist_wheel):
  52. def run(self, *args, **kwargs):
  53. if not _IN_NIX_BUILD:
  54. raise RuntimeError(_BLOCK_MESSAGE)
  55. return super().run(*args, **kwargs)
  56. cmdclass["bdist_wheel"] = _GuardedBdistWheel
  57. except ImportError:
  58. pass
  59. # Root single-file modules (``run_agent``, ``hermes_state``, ``toolsets``...)
  60. # are invisible to ``packages.find``: that finder sees only directories with an
  61. # ``__init__.py``. The wheel build needs them on ``py_modules``, so derive the
  62. # list from the source tree at build time. A static list in ``pyproject.toml``
  63. # drifted each time the tree layout changed (missing modules broke installed
  64. # wheels with ``ModuleNotFoundError``), so there is no list to maintain here.
  65. # ``setup()`` kwargs merge with ``pyproject.toml``, and this file is the only
  66. # legitimate wheel/sdist builder, so the derived value is the single source.
  67. # Editable installs do not read it: ``build_editable`` never runs
  68. # ``bdist_wheel``. The filter source (``nix/lib.nix`` ``pythonSrc``) keeps
  69. # every root ``.py`` file, so the build sandbox sees the same set of files.
  70. def _root_py_modules():
  71. try:
  72. names = os.listdir(_ROOT)
  73. except OSError:
  74. return []
  75. return sorted(
  76. name[:-3]
  77. for name in names
  78. if name.endswith(".py") and name != "setup.py"
  79. )
  80. setup(cmdclass=cmdclass, py_modules=_root_py_modules())