-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompile_protos.py
More file actions
executable file
·52 lines (40 loc) · 1.61 KB
/
compile_protos.py
File metadata and controls
executable file
·52 lines (40 loc) · 1.61 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import os.path
import tempfile
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(output_dir: str = "src") -> None:
# 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",
f"--python_out={output_dir}",
f"--grpc_python_out={output_dir}",
"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}",
]
)
class CustomBuildHook(BuildHookInterface):
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
self._temp_directory = tempfile.TemporaryDirectory()
run_protoc(self._temp_directory.name)
compiled_protos = os.path.join(self._temp_directory.name, "dataclay", "proto")
build_data["force_include"][compiled_protos] = "dataclay/proto"
def dependencies(self):
return ["grpcio-tools==1.62.3"]
if __name__ == "__main__":
run_protoc()