-
Notifications
You must be signed in to change notification settings - Fork 1
/
hatch_build.py
38 lines (31 loc) · 1.17 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
import os
import subprocess
from grpc_tools import protoc
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class CustomBuildHook(BuildHookInterface):
def initialize(self, version, build_data):
compile_proto()
def compile_proto():
bakerstreet = "bakerstreet"
proto_file = f"{bakerstreet}/{bakerstreet}.proto"
# Remove existing generated files
go_files = [f for f in os.listdir(bakerstreet) if f.endswith('.go') and f.startswith('bakerstreet')]
py_files = [f for f in os.listdir(bakerstreet) if f.endswith('.py') and f.startswith('bakerstreet')]
for file in go_files + py_files:
os.remove(os.path.join(bakerstreet, file))
# Compile Go protobuf and gRPC files
go_compile_cmd = [
"protoc",
"-I", ".",
"--go_out=.",
"--go_opt=paths=source_relative",
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
proto_file
]
# Execute commands
subprocess.run(go_compile_cmd, check=True)
# Compile Python protobuf and gRPC files
protoc.main(
("-I .", "--python_out=.", "--grpc_python_out=.", "{:s}".format(proto_file))
)