-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhatch_build_hooks.py
More file actions
38 lines (27 loc) · 1.11 KB
/
hatch_build_hooks.py
File metadata and controls
38 lines (27 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Custom Hatch build hook that regenerates grammar types before builds.
Configured via:
[tool.hatch.build.hooks.custom]
path = "hatch_build_hooks.py"
This executes within the Hatch build environment.
"""
from __future__ import annotations
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class CustomBuildHook(BuildHookInterface):
"""Run the grammar generator so types.py is up to date."""
def initialize(self, version: str, build_data: dict) -> None: # noqa: D401
# Ensure the project source root is importable while building
import sys # noqa: PLC0415
if self.root not in sys.path:
sys.path.insert(0, self.root)
# Execute the generator module by path to avoid importing
# the grammar package (which would expect types.py to exist).
import os # noqa: PLC0415
import runpy # noqa: PLC0415
gen_path = os.path.join(
self.root,
"imas_standard_names",
"grammar_codegen",
"generate.py",
)
ctx = runpy.run_path(gen_path)
ctx["main"]()