Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "protobuf", version = "29.3")
bazel_dep(name = "rules_java", version = "8.6.3")
bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "rules_proto_grpc", version = "5.0.1")
bazel_dep(name = "rules_proto_grpc_python", version = "5.0.1")
bazel_dep(name = "rules_python", version = "1.2.0-rc0")
bazel_dep(name = "rules_rust", version = "0.59.1")
bazel_dep(name = "rules_rust_prost", version = "0.59.1")
Expand Down Expand Up @@ -36,8 +38,6 @@ protoc.toolchain(
)
use_repo(protoc, "com_google_protobuf", "toolchains_protoc_hub")

register_toolchains("@toolchains_protoc_hub//:all")

# NB: the `:all` here is critical, because `proto_lang_toolchain` expands into two targets:
# - proto_lang_toolchain rule [name]
# - toolchain rule [name]_toolchain
Expand All @@ -47,6 +47,8 @@ register_toolchains("@toolchains_protoc_hub//:all")
# Declared toolchains should be created with the 'toolchain' rule and should not have dependencies that themselves require toolchains.
register_toolchains("//tools/toolchains:all")

register_toolchains("@toolchains_protoc_hub//:all")

####### PYTHON ##########
# Shows how a typical Python user fetches all the dependencies of their app, including the protobuf runtime
dev_pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
Expand Down
5 changes: 5 additions & 0 deletions examples/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_rust_prost//:defs.bzl", "rust_prost_library")
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
load("//python:python_grpc_library.bzl", "python_grpc_library")

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

Expand All @@ -17,6 +18,10 @@ py_proto_library(
deps = [":greeter_proto"],
)

python_grpc_library(
name = "greeter_py_grpc",
protos = [":greeter_proto"],
)
# Broken by https://github.com/protocolbuffers/protobuf/pull/19679
# which causes building C++ code from source.
# TODO: re-enable once protobuf honors the toolchain
Expand Down
7 changes: 0 additions & 7 deletions examples/python/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
load("@rules_python//python:defs.bzl", "py_test")

py_test(
name = "message_test",
srcs = ["message_test.py"],
deps = ["//proto:greeter_py_proto"],
)
12 changes: 12 additions & 0 deletions examples/python/client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@aspect_rules_py//py:defs.bzl", "py_binary")

py_binary(
name = "client",
srcs = ["__main__.py"],
main = "__main__.py",
deps = [
"//proto:greeter_py_grpc",
"@pypi//grpcio",
"@pypi//protobuf",
],
)
36 changes: 36 additions & 0 deletions examples/python/client/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import logging

import grpc
from proto import greeter_pb2
from proto.greeter_pb2_grpc import GreeterStub
from google.protobuf.any_pb2 import Any


def run():
# Create a channel to connect to the server
with grpc.insecure_channel('[::1]:5042') as channel:
# Create a stub (client)
stub = GreeterStub(channel)

# Create a detail message using Any
detail = Any()
detail.type_url = "type.googleapis.com/mypackage.MyMessage"
detail.value = b"details"

# Create the request
request = greeter_pb2.HelloRequest(
name="Python Client",
details=[detail]
)

# Make the call
try:
response = stub.SayHello(request)
print(f"Response: {response.message}")
except grpc.RpcError as e:
print(f"RPC failed: {e}")


if __name__ == "__main__":
logging.basicConfig()
run()
15 changes: 0 additions & 15 deletions examples/python/message_test.py

This file was deleted.

30 changes: 30 additions & 0 deletions examples/python/python_grpc_compile.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Generated definition of python_grpc_compile.
Modified from https://github.com/rules-proto-grpc/rules_proto_grpc/blob/d17b5b16c8b12143c6f1b78dabd6bbc228e89b58/modules/python/python_grpc_compile.bzl
"""

load(
"@rules_proto_grpc//:defs.bzl",
"ProtoPluginInfo",
"proto_compile_attrs",
"proto_compile_impl",
"proto_compile_toolchains",
)

# Create compile rule
python_grpc_compile = rule(
implementation = proto_compile_impl,
attrs = dict(
proto_compile_attrs,
_plugins = attr.label_list(
providers = [ProtoPluginInfo],
default = [
Label("//tools/toolchains:proto_plugin"),
Label("//tools/toolchains:grpc_plugin"),
],
cfg = "exec",
doc = "List of protoc plugins to apply",
),
),
toolchains = proto_compile_toolchains,
)
60 changes: 60 additions & 0 deletions examples/python/python_grpc_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Generated definition of python_grpc_library."""

load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "proto_compile_attrs")
load("@pypi//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_library")
load(":python_grpc_compile.bzl", "python_grpc_compile")

def python_grpc_library(name, generate_pyi = False, **kwargs):
"""
python_grpc_library generates Python code from proto and gRPC, and creates a py_library for them.

Args:
name: the name of the target.
generate_pyi: flag to specify whether .pyi files should be created.
**kwargs: common Bazel attributes will be passed to both python_grpc_compile and py_library;
python_grpc_compile attributes will be passed to python_grpc_compile only.
"""

# Compile protos
name_pb = name + "_pb"
python_grpc_compile(
name = name_pb,
**{
k: v
for (k, v) in kwargs.items()
if k in proto_compile_attrs.keys() or
k in bazel_build_rule_common_attrs
} # Forward args
)

# For other code to import generated code with prefix_path if it's given
output_mode = kwargs.get("output_mode", "PREFIXED")
if output_mode == "PREFIXED":
imports = [name_pb]
else:
imports = ["."]

# for pb2_grpc.py to import pb2.py
prefix_path = kwargs.get("prefix_path", None)
if prefix_path:
imports.append(imports[0] + "/" + prefix_path)

# Create python library
py_library(
name = name,
srcs = [name_pb],
deps = GRPC_DEPS + kwargs.get("deps", []),
data = kwargs.get("data", []), # See https://github.com/rules-proto-grpc/rules_proto_grpc/issues/257 for use case
imports = imports,
**{
k: v
for (k, v) in kwargs.items()
if k in bazel_build_rule_common_attrs
} # Forward Bazel common args
)

GRPC_DEPS = [
Label(requirement("grpcio")),
Label(requirement("protobuf")),
]
11 changes: 11 additions & 0 deletions examples/python/server/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@aspect_rules_py//py:defs.bzl", "py_binary")

py_binary(
name = "server",
srcs = ["__main__.py"],
main = "__main__.py",
deps = [
"//proto:greeter_py_grpc",
"@pypi//grpcio",
],
)
21 changes: 21 additions & 0 deletions examples/python/server/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from concurrent import futures
import logging

import grpc
from proto import greeter_pb2
from proto.greeter_pb2_grpc import GreeterServicer, add_GreeterServicer_to_server

class Greeter(GreeterServicer):
def SayHello(self, request, context):
return greeter_pb2.HelloReply(message=f'Hello {request.name}, I am Python server')

def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port("[::1]:5042")
server.start()
server.wait_for_termination()

if __name__ == "__main__":
logging.basicConfig()
serve()
2 changes: 1 addition & 1 deletion examples/rust/server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Greeter for MyGreeter {
println!("Got a request from {:?}", request.remote_addr());

let reply = HelloReply {
message: format!("Hello {}!", request.into_inner().name),
message: format!("Hello {}, I am Rust server", request.into_inner().name),
};
Ok(Response::new(reply))
}
Expand Down
11 changes: 11 additions & 0 deletions examples/tools/grpcio_tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
name = "protoc",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//visibility:public"],
deps = [
"@pypi//grpcio_tools",
],
)
3 changes: 3 additions & 0 deletions examples/tools/grpcio_tools/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import runpy

runpy.run_module('grpc_tools.protoc', run_name='__main__')
4 changes: 3 additions & 1 deletion examples/tools/requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
protobuf==5.28.0
protobuf==5.29.1
grpcio
grpcio-tools
Loading