Skip to content

Commit 5b85326

Browse files
authored
Merge pull request #1002: [py] fix python build
2 parents 49ec6ab + 4a1c062 commit 5b85326

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
recursive-include rpc/src/main/proto *.proto

setup.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,71 @@
1414
# limitations under the License.
1515
#
1616

17-
import os
17+
from pathlib import Path
1818
import sys
1919
import grpc_tools
2020
import grpc_tools.protoc as protoc
2121
from setuptools import setup
2222

23-
def get_version():
23+
def get_version() -> str:
2424
return "0.15.0"
2525

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"
3131

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
3846
args = [
3947
"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+
]
4850

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'}")
5254

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
5472

55-
5673
to_build = {"ingest_grpc": build_ingest_rpc}
5774
modules = []
58-
5975
for name, build in to_build.items():
60-
result = build(name)
61-
modules += result
76+
modules += build(name)
6277

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

Comments
 (0)