|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 |
|
17 | | -import os |
| 17 | +from pathlib import Path |
18 | 18 | import sys |
19 | 19 | import grpc_tools |
20 | 20 | import grpc_tools.protoc as protoc |
21 | 21 | from setuptools import setup |
22 | 22 |
|
23 | | -def get_version(): |
| 23 | +def get_version() -> str: |
24 | 24 | return "0.15.0" |
25 | 25 |
|
26 | | -def build_ingest_rpc(name): |
27 | | - |
28 | | - proto_dir = os.path.split(os.path.abspath(sys.argv[0]))[0] + "/rpc/src/main/proto/" |
29 | | - protos = filter(lambda x: x.endswith(".proto"), os.listdir(proto_dir)) |
30 | | - protos = list(map(lambda x: proto_dir + "/" + x, protos)) |
| 26 | +def build_ingest_rpc(name: str): |
| 27 | + # Anchor all paths to the project root (the folder that contains this setup.py) |
| 28 | + PROJECT_ROOT = Path(__file__).resolve().parent |
| 29 | + PROTO_DIR = PROJECT_ROOT / "rpc" / "src" / "main" / "proto" |
| 30 | + TARGET_DIR = PROJECT_ROOT / "target" |
31 | 31 |
|
32 | | - packages = [] |
33 | | - imports = "" |
34 | | - |
35 | | - os.makedirs("./target", exist_ok=True) |
36 | | - |
37 | | - |
| 32 | + if not PROTO_DIR.is_dir(): |
| 33 | + raise FileNotFoundError( |
| 34 | + f"Expected proto dir at {PROTO_DIR} but it does not exist. " |
| 35 | + "Make sure you run the build from the project root and that " |
| 36 | + "rpc/src/main/proto is present in the sdist." |
| 37 | + ) |
| 38 | + |
| 39 | + protos = sorted(str(p) for p in PROTO_DIR.glob("*.proto")) |
| 40 | + if not protos: |
| 41 | + raise FileNotFoundError(f"No .proto files found in {PROTO_DIR}") |
| 42 | + |
| 43 | + TARGET_DIR.mkdir(exist_ok=True) |
| 44 | + |
| 45 | + # Build protoc args |
38 | 46 | args = [ |
39 | 47 | "grpc_tools.protoc", |
40 | | - "--proto_path=%s" % (proto_dir, )] |
41 | | - |
42 | | - for item in grpc_tools.__spec__.submodule_search_locations: |
43 | | - args.append("--proto_path=%s" % (item + "/_proto")) |
44 | | - |
45 | | - args += ["--python_out=./target", "--grpc_python_out=./target"] + protos |
46 | | - |
47 | | - protoc.main(args) |
| 48 | + f"--proto_path={PROTO_DIR}", |
| 49 | + ] |
48 | 50 |
|
49 | | - for proto in protos: |
50 | | - proto_name = proto.split("/")[-1].split(".")[0] |
51 | | - packages += [proto_name + "_pb2", proto_name + "_pb2_grpc"] |
| 51 | + # Add grpc_tools’ bundled well-known types include path |
| 52 | + for item in getattr(grpc_tools.__spec__, "submodule_search_locations", []) or []: |
| 53 | + args.append(f"--proto_path={Path(item) / '_proto'}") |
52 | 54 |
|
53 | | - return packages |
| 55 | + args += [ |
| 56 | + f"--python_out={TARGET_DIR}", |
| 57 | + f"--grpc_python_out={TARGET_DIR}", |
| 58 | + *protos, |
| 59 | + ] |
| 60 | + |
| 61 | + # Run generation |
| 62 | + code = protoc.main(args) |
| 63 | + if code not in (0, None): |
| 64 | + raise RuntimeError(f"protoc failed with exit code {code}") |
| 65 | + |
| 66 | + # Return the generated module names so setuptools can package them |
| 67 | + modules = [] |
| 68 | + for proto in protos: |
| 69 | + base = Path(proto).stem |
| 70 | + modules += [f"{base}_pb2", f"{base}_pb2_grpc"] |
| 71 | + return modules |
54 | 72 |
|
55 | | - |
56 | 73 | to_build = {"ingest_grpc": build_ingest_rpc} |
57 | 74 | modules = [] |
58 | | - |
59 | 75 | for name, build in to_build.items(): |
60 | | - result = build(name) |
61 | | - modules += result |
| 76 | + modules += build(name) |
62 | 77 |
|
63 | | -setup(name = 'proxima', |
64 | | - version = get_version(), |
65 | | - description = 'Proxima python bindings', |
66 | | - package_dir = {'': "target"}, |
67 | | - py_modules = modules) |
| 78 | +setup( |
| 79 | + name="proxima", |
| 80 | + version=get_version(), |
| 81 | + description="Proxima python bindings", |
| 82 | + package_dir={"": "target"}, |
| 83 | + py_modules=modules, |
| 84 | +) |
0 commit comments