Skip to content

Commit b9b08d9

Browse files
chrisirhcalexeagle
authored andcommitted
make example just work
1 parent 33c30c1 commit b9b08d9

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

examples/proto/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("@rules_go//proto:def.bzl", "go_proto_library")
33
load("@rules_proto//proto:defs.bzl", "proto_library")
44
load("@rules_rust_prost//:defs.bzl", "rust_prost_library")
55
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
6-
load("//python:python_grpc_compile.bzl", "python_grpc_compile")
6+
load("//python:python_grpc_library.bzl", "python_grpc_library")
77

88
package(default_visibility = ["//visibility:public"])
99

@@ -18,7 +18,7 @@ py_proto_library(
1818
deps = [":greeter_proto"],
1919
)
2020

21-
python_grpc_compile(
21+
python_grpc_library(
2222
name = "greeter_py_grpc",
2323
protos = [":greeter_proto"],
2424
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Generated definition of python_grpc_library."""
2+
3+
load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "proto_compile_attrs")
4+
load("@pypi//:requirements.bzl", "requirement")
5+
load("@rules_python//python:defs.bzl", "py_library")
6+
load(":python_grpc_compile.bzl", "python_grpc_compile")
7+
8+
def python_grpc_library(name, generate_pyi = False, **kwargs):
9+
"""
10+
python_grpc_library generates Python code from proto and gRPC, and creates a py_library for them.
11+
12+
Args:
13+
name: the name of the target.
14+
generate_pyi: flag to specify whether .pyi files should be created.
15+
**kwargs: common Bazel attributes will be passed to both python_grpc_compile and py_library;
16+
python_grpc_compile attributes will be passed to python_grpc_compile only.
17+
"""
18+
19+
# Compile protos
20+
name_pb = name + "_pb"
21+
python_grpc_compile(
22+
name = name_pb,
23+
**{
24+
k: v
25+
for (k, v) in kwargs.items()
26+
if k in proto_compile_attrs.keys() or
27+
k in bazel_build_rule_common_attrs
28+
} # Forward args
29+
)
30+
31+
# For other code to import generated code with prefix_path if it's given
32+
output_mode = kwargs.get("output_mode", "PREFIXED")
33+
if output_mode == "PREFIXED":
34+
imports = [name_pb]
35+
else:
36+
imports = ["."]
37+
38+
# for pb2_grpc.py to import pb2.py
39+
prefix_path = kwargs.get("prefix_path", None)
40+
if prefix_path:
41+
imports.append(imports[0] + "/" + prefix_path)
42+
43+
# Create python library
44+
py_library(
45+
name = name,
46+
srcs = [name_pb],
47+
deps = GRPC_DEPS + kwargs.get("deps", []),
48+
data = kwargs.get("data", []), # See https://github.com/rules-proto-grpc/rules_proto_grpc/issues/257 for use case
49+
imports = imports,
50+
**{
51+
k: v
52+
for (k, v) in kwargs.items()
53+
if k in bazel_build_rule_common_attrs
54+
} # Forward Bazel common args
55+
)
56+
57+
GRPC_DEPS = [
58+
Label(requirement("grpcio")),
59+
Label(requirement("protobuf")),
60+
]

0 commit comments

Comments
 (0)