-
Notifications
You must be signed in to change notification settings - Fork 0
/
hatch_build.py
81 lines (69 loc) · 2.76 KB
/
hatch_build.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import logging
from collections.abc import Generator
from importlib import resources
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any, final, override
from grpc_tools import protoc
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
from protoletariat.fdsetgen import Raw
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
@final
class BuildYaakIdlProtosHook(BuildHookInterface): # pyright: ignore[reportMissingTypeArgument]
PLUGIN_NAME = "build-yaak-idl-protos"
YAAK_IDL_PROTO_PATH = (
Path(__file__).resolve().parent
/ "src"
/ "rbyte"
/ "io"
/ "yaak"
/ "idl-repo"
/ "intercom"
/ "proto"
)
YAAK_IDL_PYTHON_OUT = (
Path(__file__).resolve().parent / "src" / "rbyte" / "io" / "yaak" / "proto"
)
YAAK_IDL_PROTOS = ("can.proto", "sensor.proto")
@override
def clean(self, versions: list[str]) -> None:
for path in self._get_yaak_idl_proto_paths():
if path.exists():
logger.warning("removing %s", path)
path.unlink()
@override
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
self._build_yaak_idl_protos()
@classmethod
def _get_yaak_idl_proto_paths(cls) -> Generator[Path, Any, None]:
for proto in cls.YAAK_IDL_PROTOS:
name, *_ = proto.split(".", maxsplit=1)
for ext in (".py", ".pyi"):
yield (cls.YAAK_IDL_PYTHON_OUT / f"{name}_pb2").with_suffix(ext)
@classmethod
def _build_yaak_idl_protos(cls) -> None:
with NamedTemporaryFile() as descriptor_set_out:
protoc_cmd = [
"grpc_tools.protoc",
f"--proto_path={resources.files('grpc_tools') / '_proto'}",
f"--proto_path={cls.YAAK_IDL_PROTO_PATH}",
f"--python_out={cls.YAAK_IDL_PYTHON_OUT}",
f"--pyi_out={cls.YAAK_IDL_PYTHON_OUT}",
f"--descriptor_set_out={descriptor_set_out.name}",
*cls.YAAK_IDL_PROTOS,
]
if protoc.main(protoc_cmd) != 0: # pyright: ignore[reportUnknownMemberType]
msg = f"error: {protoc_cmd} failed"
raise RuntimeError(msg)
Raw(descriptor_set_out.read()).fix_imports(
python_out=cls.YAAK_IDL_PYTHON_OUT,
create_package=False,
overwrite_callback=cls._overwrite_callback,
module_suffixes=["_pb2.py", "_pb2.pyi"],
exclude_imports_glob=["google/protobuf/*"],
)
@staticmethod
def _overwrite_callback(file: Path, text: str) -> None:
logger.warning("overwriting %s", file)
_ = file.write_text(text)