-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompile_protos.py
executable file
·72 lines (59 loc) · 2.13 KB
/
compile_protos.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
#!/usr/bin/env python3
import inspect
from typing import Any
from importlib import resources
from typing import Any
try:
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
except ModuleNotFoundError:
if __name__ != "__main__":
# If we are not being run interactively, then that is an error
raise
BuildHookInterface = object
def run_protoc():
# Here because during the build process, CustomBuildHook will be imported
# *before* knowing the dependencies of the hook itself.
import grpc_tools.protoc
grpc_tools_proto = (resources.files("grpc_tools") / "_proto").resolve()
grpc_tools.protoc.main(
[
"grpc_tools.protoc",
"--proto_path=dataclay-common",
"--python_out=src",
"--grpc_python_out=src",
"dataclay-common/dataclay/proto/common/common.proto",
"dataclay-common/dataclay/proto/backend/backend.proto",
"dataclay-common/dataclay/proto/metadata/metadata.proto",
f"-I{grpc_tools_proto}",
]
)
def find_config_settings_in_hatchling() -> dict[str, Any]:
# Terrible workaround (their words, not mine) given by @virtuald
# https://github.com/pypa/hatch/issues/1072#issuecomment-2448985229
# Hopefully this will be fixed in the future
for frame_info in inspect.stack():
frame = frame_info.frame
module = inspect.getmodule(frame)
if (
module
and module.__name__.startswith("hatchling.build")
and "config_settings" in frame.f_locals
):
return frame.f_locals["config_settings"] or {}
return {}
class CustomBuildHook(BuildHookInterface):
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
run_protoc()
def dependencies(self):
if find_config_settings_in_hatchling().get("LEGACY_DEPS", "False").lower() in (
"true",
"on",
"1",
"y",
"yes",
):
return ["grpcio-tools==1.48.2"]
else:
return ["grpcio-tools==1.67.1"]
if __name__ == "__main__":
run_protoc()