-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrun_protoc.py
More file actions
64 lines (50 loc) · 1.7 KB
/
run_protoc.py
File metadata and controls
64 lines (50 loc) · 1.7 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
53
54
55
56
57
58
59
60
61
62
63
64
#
# Copyright (c) 2019-2024 Roke Manor Research Ltd
#
import argparse
from distutils.file_util import copy_file
from itertools import chain
from pathlib import Path
from typing import Final
import pkg_resources
from grpc.tools import protoc
LATEST_VERSION: Final[Path] = Path(__file__).parent / "sapient_msg" / "bsi_flex_335_v2_0"
def generate(protos: list[str]):
protoc_args = [
"protoc", # Dummy arg[0]
"--python_out=.",
"--pyi_out=.",
"-I" + pkg_resources.resource_filename("grpc.tools", "_proto"),
"-I.",
] + protos
return protoc.main(protoc_args)
def main():
protos = sorted(
[str(p) for p in Path().rglob("sap*/**/*.proto")]
+ [str(p) for p in Path().rglob("tests/**/*.proto")]
)
print("Processing Proto files: \n" + "\n".join(protos))
parser = argparse.ArgumentParser(description="Generate sapient protobuf to python bindings.")
parser.add_argument(
"files",
metavar="FILES",
type=str,
nargs="*",
help="Files against which to run. Defaults to all proto files in repo.",
default=protos,
)
args = parser.parse_args()
files = sorted(args.files)
if ret := generate(files):
print(f"Failed to generate proto files {ret}")
exit(ret)
copy_to_latest()
def copy_to_latest():
for path in chain(LATEST_VERSION.glob("**/*_pb2.py"), LATEST_VERSION.glob("**/*_pb2.pyi")):
out_path = (
Path(__file__).parent / "sapient_msg" / "latest" / path.relative_to(LATEST_VERSION)
)
out_path.parent.mkdir(parents=True, exist_ok=True)
copy_file(str(path), str(out_path), update=True)
if __name__ == "__main__":
main()