From c2c5057e9d53dcbe9ee23c9010fbdfdc7d9b4118 Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Fri, 17 Jul 2020 15:55:39 -0700 Subject: [PATCH] Fix up examples --- examples/python/auth/BUILD.bazel | 13 ++++--- .../python/auth/customized_auth_client.py | 13 ++++--- .../python/auth/customized_auth_server.py | 17 +++++---- examples/python/cancellation/BUILD.bazel | 35 +++++++------------ examples/python/cancellation/client.py | 12 +++---- examples/python/cancellation/search.py | 8 +++-- examples/python/cancellation/server.py | 9 +++-- examples/python/compression/BUILD.bazel | 12 ++++--- examples/python/compression/client.py | 11 +++--- examples/python/compression/server.py | 13 ++++--- examples/python/data_transmission/BUILD | 14 ++++---- .../python/data_transmission/alts_client.py | 5 +-- .../python/data_transmission/alts_server.py | 4 +-- examples/python/debug/BUILD.bazel | 18 ++++++---- examples/python/debug/debug_server.py | 13 ++++--- examples/python/debug/send_message.py | 15 +++++--- examples/python/errors/BUILD.bazel | 16 ++++++--- examples/python/errors/client.py | 11 +++--- examples/python/errors/server.py | 12 ++++--- .../test/_error_handling_example_test.py | 10 ++++-- .../default_value/greeter_client.py | 7 ++-- .../interceptors/headers/greeter_client.py | 8 +++-- .../interceptors/headers/greeter_server.py | 7 ++-- examples/python/metadata/metadata_client.py | 6 ++-- examples/python/metadata/metadata_server.py | 6 ++-- examples/python/multiplex/multiplex_client.py | 10 +++--- examples/python/multiplex/multiplex_server.py | 9 ++--- examples/python/multiprocessing/BUILD | 29 +++++---------- examples/python/multiprocessing/client.py | 7 ++-- examples/python/multiprocessing/server.py | 9 +++-- .../python/route_guide/route_guide_client.py | 7 ++-- .../python/route_guide/route_guide_server.py | 8 +++-- examples/python/wait_for_ready/BUILD.bazel | 6 ++-- .../wait_for_ready/wait_for_ready_example.py | 17 +++++---- examples/python/xds/requirements.txt | 1 + tools/distrib/python/grpcio_tools/BUILD.bazel | 2 ++ 36 files changed, 233 insertions(+), 167 deletions(-) diff --git a/examples/python/auth/BUILD.bazel b/examples/python/auth/BUILD.bazel index 3838b39edab..dedae87a49e 100644 --- a/examples/python/auth/BUILD.bazel +++ b/examples/python/auth/BUILD.bazel @@ -34,11 +34,13 @@ py_binary( testonly = 1, srcs = ["customized_auth_client.py"], python_version = "PY3", + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ ":_credentials", - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) @@ -47,11 +49,13 @@ py_binary( testonly = 1, srcs = ["customized_auth_server.py"], python_version = "PY3", + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ ":_credentials", - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) @@ -63,7 +67,6 @@ py_test( ":_credentials", ":customized_auth_client", ":customized_auth_server", - "//examples:helloworld_py_pb2", "//src/python/grpcio/grpc:grpcio", ], ) diff --git a/examples/python/auth/customized_auth_client.py b/examples/python/auth/customized_auth_client.py index 49eb25bca99..8e20149bf76 100644 --- a/examples/python/auth/customized_auth_client.py +++ b/examples/python/auth/customized_auth_client.py @@ -20,10 +20,15 @@ from __future__ import print_function import argparse import contextlib import logging +import os +import sys import grpc -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) + +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") + from examples.python.auth import _credentials _LOGGER = logging.getLogger(__name__) @@ -72,8 +77,8 @@ def create_client_channel(addr): def send_rpc(channel): - stub = helloworld_pb2_grpc.GreeterStub(channel) - request = helloworld_pb2.HelloRequest(name='you') + stub = services.GreeterStub(channel) + request = protos.HelloRequest(name='you') try: response = stub.SayHello(request) except grpc.RpcError as rpc_error: diff --git a/examples/python/auth/customized_auth_server.py b/examples/python/auth/customized_auth_server.py index ecc73e36198..61fd2a2d5b9 100644 --- a/examples/python/auth/customized_auth_server.py +++ b/examples/python/auth/customized_auth_server.py @@ -18,13 +18,18 @@ from __future__ import division from __future__ import print_function import argparse +from concurrent import futures import contextlib import logging -from concurrent import futures +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) import grpc -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc + +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") + from examples.python.auth import _credentials _LOGGER = logging.getLogger(__name__) @@ -56,10 +61,10 @@ class SignatureValidationInterceptor(grpc.ServerInterceptor): return self._abortion -class SimpleGreeter(helloworld_pb2_grpc.GreeterServicer): +class SimpleGreeter(services.GreeterServicer): def SayHello(self, request, unused_context): - return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) + return protos.HelloReply(message='Hello, %s!' % request.name) @contextlib.contextmanager @@ -67,7 +72,7 @@ def run_server(port): # Bind interceptor to server server = grpc.server(futures.ThreadPoolExecutor(), interceptors=(SignatureValidationInterceptor(),)) - helloworld_pb2_grpc.add_GreeterServicer_to_server(SimpleGreeter(), server) + services.add_GreeterServicer_to_server(SimpleGreeter(), server) # Loading credentials server_credentials = grpc.ssl_server_credentials((( diff --git a/examples/python/cancellation/BUILD.bazel b/examples/python/cancellation/BUILD.bazel index 0426cf0b943..aece20b7136 100644 --- a/examples/python/cancellation/BUILD.bazel +++ b/examples/python/cancellation/BUILD.bazel @@ -15,36 +15,20 @@ # limitations under the License. load("@grpc_python_dependencies//:requirements.bzl", "requirement") -load("@rules_proto//proto:defs.bzl", "proto_library") -load("//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library") package(default_testonly = 1) -proto_library( - name = "hash_name_proto", - srcs = ["hash_name.proto"], -) - -py_proto_library( - name = "hash_name_py_pb2", - deps = [":hash_name_proto"], -) - -py_grpc_library( - name = "hash_name_py_pb2_grpc", - srcs = [":hash_name_proto"], - deps = [":hash_name_py_pb2"], -) - py_binary( name = "client", srcs = ["client.py"], python_version = "PY3", srcs_version = "PY2AND3", + data = [ + "hash_name.proto", + ], deps = [ - ":hash_name_py_pb2", - ":hash_name_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", "@six", ], ) @@ -53,8 +37,12 @@ py_library( name = "search", srcs = ["search.py"], srcs_version = "PY2AND3", + data = [ + "hash_name.proto", + ], deps = [ - ":hash_name_py_pb2", + "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) @@ -63,9 +51,12 @@ py_binary( srcs = ["server.py"], python_version = "PY3", srcs_version = "PY2AND3", + data = [ + "hash_name.proto", + ], deps = [ "//src/python/grpcio/grpc:grpcio", - ":hash_name_py_pb2", + "//tools/distrib/python/grpcio_tools:grpc_tools", ":search", ] + select({ "//conditions:default": ["@futures//:futures"], diff --git a/examples/python/cancellation/client.py b/examples/python/cancellation/client.py index c61b3c3a6da..e408cace968 100644 --- a/examples/python/cancellation/client.py +++ b/examples/python/cancellation/client.py @@ -19,13 +19,13 @@ from __future__ import print_function import argparse import logging +import os import signal import sys import grpc -from examples.python.cancellation import hash_name_pb2 -from examples.python.cancellation import hash_name_pb2_grpc +protos, services = grpc.protos_and_services("hash_name.proto") _DESCRIPTION = "A client for finding hashes similar to names." _LOGGER = logging.getLogger(__name__) @@ -33,8 +33,8 @@ _LOGGER = logging.getLogger(__name__) def run_unary_client(server_target, name, ideal_distance): with grpc.insecure_channel(server_target) as channel: - stub = hash_name_pb2_grpc.HashFinderStub(channel) - future = stub.Find.future(hash_name_pb2.HashNameRequest( + stub = services.HashFinderStub(channel) + future = stub.Find.future(protos.HashNameRequest( desired_name=name, ideal_hamming_distance=ideal_distance), wait_for_ready=True) @@ -50,8 +50,8 @@ def run_unary_client(server_target, name, ideal_distance): def run_streaming_client(server_target, name, ideal_distance, interesting_distance): with grpc.insecure_channel(server_target) as channel: - stub = hash_name_pb2_grpc.HashFinderStub(channel) - result_generator = stub.FindRange(hash_name_pb2.HashNameRequest( + stub = services.HashFinderStub(channel) + result_generator = stub.FindRange(protos.HashNameRequest( desired_name=name, ideal_hamming_distance=ideal_distance, interesting_hamming_distance=interesting_distance), diff --git a/examples/python/cancellation/search.py b/examples/python/cancellation/search.py index 9d2331af1bb..b5249f64515 100644 --- a/examples/python/cancellation/search.py +++ b/examples/python/cancellation/search.py @@ -23,7 +23,9 @@ import itertools import logging import struct -from examples.python.cancellation import hash_name_pb2 +import grpc + +protos, services = grpc.protos_and_services("hash_name.proto") _LOGGER = logging.getLogger(__name__) _BYTE_MAX = 255 @@ -132,13 +134,13 @@ def search(target, distance = _get_substring_hamming_distance(candidate_hash, target) if interesting_hamming_distance is not None and distance <= interesting_hamming_distance: # Surface interesting candidates, but don't stop. - yield hash_name_pb2.HashNameResponse( + yield protos.HashNameResponse( secret=base64.b64encode(secret), hashed_name=candidate_hash, hamming_distance=distance) elif distance <= ideal_distance: # Yield ideal candidate and end the stream. - yield hash_name_pb2.HashNameResponse( + yield protos.HashNameResponse( secret=base64.b64encode(secret), hashed_name=candidate_hash, hamming_distance=distance) diff --git a/examples/python/cancellation/server.py b/examples/python/cancellation/server.py index 4116fd1df8d..feec242e817 100644 --- a/examples/python/cancellation/server.py +++ b/examples/python/cancellation/server.py @@ -25,8 +25,7 @@ import threading import grpc import search -from examples.python.cancellation import hash_name_pb2 -from examples.python.cancellation import hash_name_pb2_grpc +protos, services = grpc.protos_and_services("hash_name.proto") _LOGGER = logging.getLogger(__name__) _SERVER_HOST = 'localhost' @@ -34,7 +33,7 @@ _SERVER_HOST = 'localhost' _DESCRIPTION = "A server for finding hashes similar to names." -class HashFinder(hash_name_pb2_grpc.HashFinderServicer): +class HashFinder(services.HashFinderServicer): def __init__(self, maximum_hashes): super(HashFinder, self).__init__() @@ -59,7 +58,7 @@ class HashFinder(hash_name_pb2_grpc.HashFinderServicer): context.cancel() _LOGGER.debug("Servicer thread returning.") if not candidates: - return hash_name_pb2.HashNameResponse() + return protos.HashNameResponse() return candidates[-1] def FindRange(self, request, context): @@ -91,7 +90,7 @@ def _running_server(port, maximum_hashes): # threads. server = grpc.server(futures.ThreadPoolExecutor(max_workers=1), maximum_concurrent_rpcs=1) - hash_name_pb2_grpc.add_HashFinderServicer_to_server( + services.add_HashFinderServicer_to_server( HashFinder(maximum_hashes), server) address = '{}:{}'.format(_SERVER_HOST, port) actual_port = server.add_insecure_port(address) diff --git a/examples/python/compression/BUILD.bazel b/examples/python/compression/BUILD.bazel index 1ab1d5a2a37..bbe1419c67f 100644 --- a/examples/python/compression/BUILD.bazel +++ b/examples/python/compression/BUILD.bazel @@ -17,10 +17,12 @@ py_binary( srcs = ["server.py"], python_version = "PY3", srcs_version = "PY2AND3", + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) @@ -29,10 +31,12 @@ py_binary( srcs = ["client.py"], python_version = "PY3", srcs_version = "PY2AND3", + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) diff --git a/examples/python/compression/client.py b/examples/python/compression/client.py index 5d7a2aeec52..8f126bd643f 100644 --- a/examples/python/compression/client.py +++ b/examples/python/compression/client.py @@ -19,10 +19,13 @@ from __future__ import print_function import argparse import logging +import os +import sys + import grpc -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") _DESCRIPTION = 'A client capable of compression.' _COMPRESSION_OPTIONS = { @@ -37,8 +40,8 @@ _LOGGER = logging.getLogger(__name__) def run_client(channel_compression, call_compression, target): with grpc.insecure_channel(target, compression=channel_compression) as channel: - stub = helloworld_pb2_grpc.GreeterStub(channel) - response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), + stub = services.GreeterStub(channel) + response = stub.SayHello(protos.HelloRequest(name='you'), compression=call_compression, wait_for_ready=True) print("Response: {}".format(response)) diff --git a/examples/python/compression/server.py b/examples/python/compression/server.py index 15fb0d0f56f..a448788bf7c 100644 --- a/examples/python/compression/server.py +++ b/examples/python/compression/server.py @@ -20,11 +20,14 @@ from __future__ import print_function from concurrent import futures import argparse import logging +import os import threading +import sys + import grpc -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") _DESCRIPTION = 'A server capable of compression.' _COMPRESSION_OPTIONS = { @@ -37,7 +40,7 @@ _LOGGER = logging.getLogger(__name__) _SERVER_HOST = 'localhost' -class Greeter(helloworld_pb2_grpc.GreeterServicer): +class Greeter(services.GreeterServicer): def __init__(self, no_compress_every_n): super(Greeter, self).__init__() @@ -56,14 +59,14 @@ class Greeter(helloworld_pb2_grpc.GreeterServicer): def SayHello(self, request, context): if self._should_suppress_compression(): context.set_response_compression(grpc.Compression.NoCompression) - return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) + return protos.HelloReply(message='Hello, %s!' % request.name) def run_server(server_compression, no_compress_every_n, port): server = grpc.server(futures.ThreadPoolExecutor(), compression=server_compression, options=(('grpc.so_reuseport', 1),)) - helloworld_pb2_grpc.add_GreeterServicer_to_server( + services.add_GreeterServicer_to_server( Greeter(no_compress_every_n), server) address = '{}:{}'.format(_SERVER_HOST, port) server.add_insecure_port(address) diff --git a/examples/python/data_transmission/BUILD b/examples/python/data_transmission/BUILD index 2eefb7a3ed2..561e66f5d96 100644 --- a/examples/python/data_transmission/BUILD +++ b/examples/python/data_transmission/BUILD @@ -14,21 +14,21 @@ licenses(["notice"]) # 3-clause BSD -load("@grpc_python_dependencies//:requirements.bzl", "requirement") - py_binary( name = "alts_server", srcs = [ "alts_server.py", - "demo_pb2.py", - "demo_pb2_grpc.py", "server.py", ], + data = [ + "demo.proto" + ], main = "alts_server.py", python_version = "PY3", srcs_version = "PY2AND3", deps = [ "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) @@ -37,13 +37,15 @@ py_binary( srcs = [ "alts_client.py", "client.py", - "demo_pb2.py", - "demo_pb2_grpc.py", + ], + data = [ + "demo.proto" ], main = "alts_client.py", python_version = "PY3", srcs_version = "PY2AND3", deps = [ "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) diff --git a/examples/python/data_transmission/alts_client.py b/examples/python/data_transmission/alts_client.py index eb328667cd6..79033381187 100644 --- a/examples/python/data_transmission/alts_client.py +++ b/examples/python/data_transmission/alts_client.py @@ -17,7 +17,8 @@ The example would only successfully run in GCP environment.""" import grpc -import demo_pb2_grpc +services = grpc.services("demo.proto") + from client import (bidirectional_streaming_method, client_streaming_method, server_streaming_method, simple_method) @@ -28,7 +29,7 @@ def main(): with grpc.secure_channel( SERVER_ADDRESS, credentials=grpc.alts_channel_credentials()) as channel: - stub = demo_pb2_grpc.GRPCDemoStub(channel) + stub = services.GRPCDemoStub(channel) simple_method(stub) client_streaming_method(stub) server_streaming_method(stub) diff --git a/examples/python/data_transmission/alts_server.py b/examples/python/data_transmission/alts_server.py index 4e8747ce7a9..20c6a17aa36 100644 --- a/examples/python/data_transmission/alts_server.py +++ b/examples/python/data_transmission/alts_server.py @@ -19,7 +19,7 @@ from concurrent import futures import grpc -import demo_pb2_grpc +services = grpc.services("demo.proto") from server import DemoServer SERVER_ADDRESS = 'localhost:23333' @@ -27,7 +27,7 @@ SERVER_ADDRESS = 'localhost:23333' def main(): svr = grpc.server(futures.ThreadPoolExecutor()) - demo_pb2_grpc.add_GRPCDemoServicer_to_server(DemoServer(), svr) + services.add_GRPCDemoServicer_to_server(DemoServer(), svr) svr.add_secure_port(SERVER_ADDRESS, server_credentials=grpc.alts_server_credentials()) print("------------------start Python GRPC server with ALTS encryption") diff --git a/examples/python/debug/BUILD.bazel b/examples/python/debug/BUILD.bazel index 029f78d551a..5174e55a349 100644 --- a/examples/python/debug/BUILD.bazel +++ b/examples/python/debug/BUILD.bazel @@ -18,10 +18,12 @@ py_binary( name = "debug_server", testonly = 1, srcs = ["debug_server.py"], + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", "//src/python/grpcio_channelz/grpc_channelz/v1:grpc_channelz", ], ) @@ -31,10 +33,12 @@ py_binary( testonly = 1, srcs = ["send_message.py"], python_version = "PY3", + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) @@ -53,13 +57,15 @@ py_test( name = "_debug_example_test", srcs = ["test/_debug_example_test.py"], python_version = "PY3", + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ ":debug_server", ":get_stats", ":send_message", - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", "//src/python/grpcio_channelz/grpc_channelz/v1:grpc_channelz", ], ) diff --git a/examples/python/debug/debug_server.py b/examples/python/debug/debug_server.py index ce78fe33d8d..cf8a88b67ba 100644 --- a/examples/python/debug/debug_server.py +++ b/examples/python/debug/debug_server.py @@ -20,13 +20,16 @@ from __future__ import print_function import argparse import logging from concurrent import futures +import os import random +import sys import grpc from grpc_channelz.v1 import channelz -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) + +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") _LOGGER = logging.getLogger(__name__) _LOGGER.setLevel(logging.INFO) @@ -34,7 +37,7 @@ _LOGGER.setLevel(logging.INFO) _RANDOM_FAILURE_RATE = 0.3 -class FaultInjectGreeter(helloworld_pb2_grpc.GreeterServicer): +class FaultInjectGreeter(services.GreeterServicer): def __init__(self, failure_rate): self._failure_rate = failure_rate @@ -43,12 +46,12 @@ class FaultInjectGreeter(helloworld_pb2_grpc.GreeterServicer): if random.random() < self._failure_rate: context.abort(grpc.StatusCode.UNAVAILABLE, 'Randomly injected failure.') - return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) + return protos.HelloReply(message='Hello, %s!' % request.name) def create_server(addr, failure_rate): server = grpc.server(futures.ThreadPoolExecutor()) - helloworld_pb2_grpc.add_GreeterServicer_to_server( + services.add_GreeterServicer_to_server( FaultInjectGreeter(failure_rate), server) # Add Channelz Servicer to the gRPC server diff --git a/examples/python/debug/send_message.py b/examples/python/debug/send_message.py index 63cc60a572f..568e7476687 100644 --- a/examples/python/debug/send_message.py +++ b/examples/python/debug/send_message.py @@ -17,11 +17,16 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import logging import argparse +import logging +import os +import sys + import grpc -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) + +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") def process(stub, request): @@ -35,8 +40,8 @@ def process(stub, request): def run(addr, n): with grpc.insecure_channel(addr) as channel: - stub = helloworld_pb2_grpc.GreeterStub(channel) - request = helloworld_pb2.HelloRequest(name='you') + stub = services.GreeterStub(channel) + request = protos.HelloRequest(name='you') for _ in range(n): process(stub, request) diff --git a/examples/python/errors/BUILD.bazel b/examples/python/errors/BUILD.bazel index a6aacd75a8f..a53f407bc13 100644 --- a/examples/python/errors/BUILD.bazel +++ b/examples/python/errors/BUILD.bazel @@ -18,10 +18,12 @@ py_library( name = "client", testonly = 1, srcs = ["client.py"], + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", "//src/python/grpcio_status/grpc_status", requirement("googleapis-common-protos"), ], @@ -31,11 +33,13 @@ py_library( name = "server", testonly = 1, srcs = ["server.py"], + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", "//src/python/grpcio_status/grpc_status:grpc_status", - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", ] + select({ "//conditions:default": [requirement("futures")], "//:python3": [], @@ -51,9 +55,13 @@ py_test( "../../../src/python/grpcio_tests", ], python_version = "PY3", + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ ":client", ":server", + "//tools/distrib/python/grpcio_tools:grpc_tools", "//src/python/grpcio_tests/tests:bazel_namespace_package_hack", ], ) diff --git a/examples/python/errors/client.py b/examples/python/errors/client.py index c762d798dc2..cbd96915d3d 100644 --- a/examples/python/errors/client.py +++ b/examples/python/errors/client.py @@ -14,21 +14,24 @@ """This example handles rich error status in client-side.""" from __future__ import print_function + import logging +import os +import sys import grpc from grpc_status import rpc_status from google.rpc import error_details_pb2 -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") _LOGGER = logging.getLogger(__name__) def process(stub): try: - response = stub.SayHello(helloworld_pb2.HelloRequest(name='Alice')) + response = stub.SayHello(protos.HelloRequest(name='Alice')) _LOGGER.info('Call success: %s', response.message) except grpc.RpcError as rpc_error: _LOGGER.error('Call failure: %s', rpc_error) @@ -47,7 +50,7 @@ def main(): # used in circumstances in which the with statement does not fit the needs # of the code. with grpc.insecure_channel('localhost:50051') as channel: - stub = helloworld_pb2_grpc.GreeterStub(channel) + stub = services.GreeterStub(channel) process(stub) diff --git a/examples/python/errors/server.py b/examples/python/errors/server.py index 096d2170db5..3e10ec22a7c 100644 --- a/examples/python/errors/server.py +++ b/examples/python/errors/server.py @@ -15,7 +15,9 @@ from concurrent import futures import logging +import os import threading +import sys import grpc from grpc_status import rpc_status @@ -23,8 +25,8 @@ from grpc_status import rpc_status from google.protobuf import any_pb2 from google.rpc import code_pb2, status_pb2, error_details_pb2 -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") def create_greet_limit_exceed_error_status(name): @@ -43,7 +45,7 @@ def create_greet_limit_exceed_error_status(name): ) -class LimitedGreeter(helloworld_pb2_grpc.GreeterServicer): +class LimitedGreeter(services.GreeterServicer): def __init__(self): self._lock = threading.RLock() @@ -57,12 +59,12 @@ class LimitedGreeter(helloworld_pb2_grpc.GreeterServicer): context.abort_with_status(rpc_status.to_status(rich_status)) else: self._greeted.add(request.name) - return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) + return protos.HelloReply(message='Hello, %s!' % request.name) def create_server(server_address): server = grpc.server(futures.ThreadPoolExecutor()) - helloworld_pb2_grpc.add_GreeterServicer_to_server(LimitedGreeter(), server) + services.add_GreeterServicer_to_server(LimitedGreeter(), server) port = server.add_insecure_port(server_address) return server, port diff --git a/examples/python/errors/test/_error_handling_example_test.py b/examples/python/errors/test/_error_handling_example_test.py index 9eb81ba3742..473fca934c0 100644 --- a/examples/python/errors/test/_error_handling_example_test.py +++ b/examples/python/errors/test/_error_handling_example_test.py @@ -21,12 +21,16 @@ try: except ImportError: pass -import unittest import logging +import os +import sys +import unittest import grpc -from examples import helloworld_pb2_grpc +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +services = grpc.services("examples/protos/helloworld.proto") + from examples.python.errors import client as error_handling_client from examples.python.errors import server as error_handling_server @@ -43,7 +47,7 @@ class ErrorHandlingExampleTest(unittest.TestCase): self._server.stop(None) def test_error_handling_example(self): - stub = helloworld_pb2_grpc.GreeterStub(self._channel) + stub = services.GreeterStub(self._channel) error_handling_client.process(stub) error_handling_client.process(stub) # No unhandled exception raised, test passed! diff --git a/examples/python/interceptors/default_value/greeter_client.py b/examples/python/interceptors/default_value/greeter_client.py index f3ca960aeff..0b2fa3c364d 100644 --- a/examples/python/interceptors/default_value/greeter_client.py +++ b/examples/python/interceptors/default_value/greeter_client.py @@ -15,11 +15,14 @@ from __future__ import print_function import logging +import os +import sys import grpc -protos, services = grpc.protos_and_services("protos/helloworld.proto", - include_paths=["../../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +protos, services = grpc.protos_and_services("protos/helloworld.proto") + import default_value_client_interceptor diff --git a/examples/python/interceptors/headers/greeter_client.py b/examples/python/interceptors/headers/greeter_client.py index bfe0bdb5929..7280edab575 100644 --- a/examples/python/interceptors/headers/greeter_client.py +++ b/examples/python/interceptors/headers/greeter_client.py @@ -14,12 +14,16 @@ """The Python implementation of the GRPC helloworld.Greeter client.""" from __future__ import print_function + import logging +import os +import sys import grpc -protos, services = grpc.protos_and_services("protos/helloworld.protos", - include_paths=["../../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +protos, services = grpc.protos_and_services("protos/helloworld.protos") + import header_manipulator_client_interceptor diff --git a/examples/python/interceptors/headers/greeter_server.py b/examples/python/interceptors/headers/greeter_server.py index ca612dafd1d..8106c8bf045 100644 --- a/examples/python/interceptors/headers/greeter_server.py +++ b/examples/python/interceptors/headers/greeter_server.py @@ -15,11 +15,14 @@ from concurrent import futures import logging +import os +import sys import grpc -protos, services = grpc.protos_and_services("protos/helloworld.protos", - include_paths=["../../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) +protos, services = grpc.protos_and_services("protos/helloworld.protos") + from request_header_validator_interceptor import RequestHeaderValidatorInterceptor diff --git a/examples/python/metadata/metadata_client.py b/examples/python/metadata/metadata_client.py index f1d3753f065..f7e5fb90466 100644 --- a/examples/python/metadata/metadata_client.py +++ b/examples/python/metadata/metadata_client.py @@ -15,11 +15,13 @@ from __future__ import print_function import logging +import os +import sys import grpc -protos, services = grpc.protos_and_services("protos/helloworld.proto", - include_paths=["../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../..")) +protos, services = grpc.protos_and_services("protos/helloworld.proto") def run(): diff --git a/examples/python/metadata/metadata_server.py b/examples/python/metadata/metadata_server.py index ad28f483e5b..b74e439b4cf 100644 --- a/examples/python/metadata/metadata_server.py +++ b/examples/python/metadata/metadata_server.py @@ -16,11 +16,13 @@ from __future__ import print_function from concurrent import futures import logging +import os +import sys import grpc -protos, services = grpc.protos_and_services("protos/helloworld.proto", - include_paths=["../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../..")) +protos, services = grpc.protos_and_services("protos/helloworld.proto") class Greeter(services.GreeterServicer): diff --git a/examples/python/multiplex/multiplex_client.py b/examples/python/multiplex/multiplex_client.py index 690d9a85888..08777a5523d 100644 --- a/examples/python/multiplex/multiplex_client.py +++ b/examples/python/multiplex/multiplex_client.py @@ -18,13 +18,15 @@ from __future__ import print_function import random import time import logging +import os +import sys import grpc -hw_protos, hw_services = grpc.protos_and_services("protos/helloworld.proto", - include_paths=["../.."]) -rg_protos, rg_services = grpc.protos_and_services("protos/route_guide.proto", - include_paths=["../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../..")) +hw_protos, hw_services = grpc.protos_and_services("protos/helloworld.proto") +rg_protos, rg_services = grpc.protos_and_services("protos/route_guide.proto") + import route_guide_resources diff --git a/examples/python/multiplex/multiplex_server.py b/examples/python/multiplex/multiplex_server.py index 7a6e800dc10..5190a22892e 100644 --- a/examples/python/multiplex/multiplex_server.py +++ b/examples/python/multiplex/multiplex_server.py @@ -17,13 +17,14 @@ from concurrent import futures import time import math import logging +import os +import sys import grpc -hw_protos, hw_services = grpc.protos_and_services("protos/helloworld.proto", - include_paths=["../.."]) -rg_protos, rg_services = grpc.protos_and_services("protos/route_guide.proto", - include_paths=["../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../..")) +hw_protos, hw_services = grpc.protos_and_services("protos/helloworld.proto") +rg_protos, rg_services = grpc.protos_and_services("protos/route_guide.proto") import route_guide_resources diff --git a/examples/python/multiprocessing/BUILD b/examples/python/multiprocessing/BUILD index 1d831e729b4..62b7fb07b2c 100644 --- a/examples/python/multiprocessing/BUILD +++ b/examples/python/multiprocessing/BUILD @@ -15,23 +15,6 @@ # limitations under the License. load("@rules_proto//proto:defs.bzl", "proto_library") -load("//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library") - -proto_library( - name = "prime_proto", - srcs = ["prime.proto"], -) - -py_proto_library( - name = "prime_proto_pb2", - deps = [":prime_proto"], -) - -py_grpc_library( - name = "prime_proto_pb2_grpc", - srcs = [":prime_proto"], - deps = [":prime_proto_pb2"], -) py_binary( name = "client", @@ -40,10 +23,12 @@ py_binary( imports = ["."], python_version = "PY3", srcs_version = "PY3", + data = [ + "prime.proto" + ], deps = [ - ":prime_proto_pb2", - ":prime_proto_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) @@ -54,10 +39,12 @@ py_binary( imports = ["."], python_version = "PY3", srcs_version = "PY3", + data = [ + "prime.proto" + ], deps = [ "//src/python/grpcio/grpc:grpcio", - ":prime_proto_pb2", - ":prime_proto_pb2_grpc", + "//tools/distrib/python/grpcio_tools:grpc_tools", ] + select({ "//conditions:default": ["@futures//:futures"], "//:python3": [], diff --git a/examples/python/multiprocessing/client.py b/examples/python/multiprocessing/client.py index 7676bd4ec88..eec990d6856 100644 --- a/examples/python/multiprocessing/client.py +++ b/examples/python/multiprocessing/client.py @@ -26,8 +26,7 @@ import sys import grpc -import prime_pb2 -import prime_pb2_grpc +protos, services = grpc.protos_and_services("prime.proto") _PROCESS_COUNT = 8 _MAXIMUM_CANDIDATE = 10000 @@ -52,7 +51,7 @@ def _initialize_worker(server_address): global _worker_stub_singleton # pylint: disable=global-statement _LOGGER.info('Initializing worker process.') _worker_channel_singleton = grpc.insecure_channel(server_address) - _worker_stub_singleton = prime_pb2_grpc.PrimeCheckerStub( + _worker_stub_singleton = services.PrimeCheckerStub( _worker_channel_singleton) atexit.register(_shutdown_worker) @@ -60,7 +59,7 @@ def _initialize_worker(server_address): def _run_worker_query(primality_candidate): _LOGGER.info('Checking primality of %s.', primality_candidate) return _worker_stub_singleton.check( - prime_pb2.PrimeCandidate(candidate=primality_candidate)) + protos.PrimeCandidate(candidate=primality_candidate)) def _calculate_primes(server_address): diff --git a/examples/python/multiprocessing/server.py b/examples/python/multiprocessing/server.py index a5ee00755e6..2bd420278f1 100644 --- a/examples/python/multiprocessing/server.py +++ b/examples/python/multiprocessing/server.py @@ -29,8 +29,7 @@ import sys import grpc -import prime_pb2 -import prime_pb2_grpc +protos, services = grpc.protos_and_services("prime.proto") _LOGGER = logging.getLogger(__name__) @@ -47,11 +46,11 @@ def is_prime(n): return True -class PrimeChecker(prime_pb2_grpc.PrimeCheckerServicer): +class PrimeChecker(services.PrimeCheckerServicer): def check(self, request, context): _LOGGER.info('Determining primality of %s', request.candidate) - return prime_pb2.Primality(isPrime=is_prime(request.candidate)) + return protos.Primality(isPrime=is_prime(request.candidate)) def _wait_forever(server): @@ -70,7 +69,7 @@ def _run_server(bind_address): server = grpc.server(futures.ThreadPoolExecutor( max_workers=_THREAD_CONCURRENCY,), options=options) - prime_pb2_grpc.add_PrimeCheckerServicer_to_server(PrimeChecker(), server) + services.add_PrimeCheckerServicer_to_server(PrimeChecker(), server) server.add_insecure_port(bind_address) server.start() _wait_forever(server) diff --git a/examples/python/route_guide/route_guide_client.py b/examples/python/route_guide/route_guide_client.py index 2c2c4d1f0a6..216dcca1913 100644 --- a/examples/python/route_guide/route_guide_client.py +++ b/examples/python/route_guide/route_guide_client.py @@ -17,11 +17,14 @@ from __future__ import print_function import random import logging +import os +import sys import grpc -protos, services = grpc.protos_and_services("protos/route_guide.proto", - include_paths=["../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../..")) + +protos, services = grpc.protos_and_services("protos/route_guide.proto") import route_guide_resources diff --git a/examples/python/route_guide/route_guide_server.py b/examples/python/route_guide/route_guide_server.py index 1e8d7e3d928..3c6d70e0d74 100644 --- a/examples/python/route_guide/route_guide_server.py +++ b/examples/python/route_guide/route_guide_server.py @@ -17,11 +17,15 @@ from concurrent import futures import time import math import logging +import os +import sys import grpc -protos, services = grpc.protos_and_services("protos/route_guide.proto", - include_paths=["../.."]) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../..")) + +protos, services = grpc.protos_and_services("protos/route_guide.proto") + import route_guide_resources diff --git a/examples/python/wait_for_ready/BUILD.bazel b/examples/python/wait_for_ready/BUILD.bazel index 0892b36790e..26409d57246 100644 --- a/examples/python/wait_for_ready/BUILD.bazel +++ b/examples/python/wait_for_ready/BUILD.bazel @@ -18,10 +18,12 @@ py_library( name = "wait_for_ready_example", testonly = 1, srcs = ["wait_for_ready_example.py"], + data = [ + "//examples:protos/helloworld.proto", + ], deps = [ - "//examples:helloworld_py_pb2", - "//examples:helloworld_py_pb2_grpc", "//src/python/grpcio/grpc:grpcio", + "//tools/distrib/python/grpcio_tools:grpc_tools", ], ) diff --git a/examples/python/wait_for_ready/wait_for_ready_example.py b/examples/python/wait_for_ready/wait_for_ready_example.py index db2aeaf6665..354a1e0e422 100644 --- a/examples/python/wait_for_ready/wait_for_ready_example.py +++ b/examples/python/wait_for_ready/wait_for_ready_example.py @@ -17,13 +17,16 @@ from __future__ import print_function import logging from concurrent import futures from contextlib import contextmanager +import os import socket +import sys import threading import grpc -from examples import helloworld_pb2 -from examples import helloworld_pb2_grpc +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../..")) + +protos, services = grpc.protos_and_services("examples/protos/helloworld.proto") _LOGGER = logging.getLogger(__name__) _LOGGER.setLevel(logging.INFO) @@ -41,15 +44,15 @@ def get_free_loopback_tcp_port(): tcp_socket.close() -class Greeter(helloworld_pb2_grpc.GreeterServicer): +class Greeter(services.GreeterServicer): def SayHello(self, request, unused_context): - return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) + return protos.HelloReply(message='Hello, %s!' % request.name) def create_server(server_address): server = grpc.server(futures.ThreadPoolExecutor()) - helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) + services.add_GreeterServicer_to_server(Greeter(), server) bound_port = server.add_insecure_port(server_address) assert bound_port == int(server_address.split(':')[-1]) return server @@ -57,7 +60,7 @@ def create_server(server_address): def process(stub, wait_for_ready=None): try: - response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), + response = stub.SayHello(protos.HelloRequest(name='you'), wait_for_ready=wait_for_ready) message = response.message except grpc.RpcError as rpc_error: @@ -84,7 +87,7 @@ def main(): # Create gRPC channel channel = grpc.insecure_channel(server_address) channel.subscribe(wait_for_transient_failure) - stub = helloworld_pb2_grpc.GreeterStub(channel) + stub = services.GreeterStub(channel) # Fire an RPC without wait_for_ready thread_disabled_wait_for_ready = threading.Thread(target=process, diff --git a/examples/python/xds/requirements.txt b/examples/python/xds/requirements.txt index 7ba651e351a..807b3e1fea9 100644 --- a/examples/python/xds/requirements.txt +++ b/examples/python/xds/requirements.txt @@ -1,4 +1,5 @@ grpcio>=1.28.1 +grpcio-tools protobuf grpcio-reflection grpcio-health-checking diff --git a/tools/distrib/python/grpcio_tools/BUILD.bazel b/tools/distrib/python/grpcio_tools/BUILD.bazel index c842a415c60..8a3f6d1e296 100644 --- a/tools/distrib/python/grpcio_tools/BUILD.bazel +++ b/tools/distrib/python/grpcio_tools/BUILD.bazel @@ -15,6 +15,7 @@ package(default_visibility = [ "//src/python:__subpackages__", "//tools/distrib/python/grpcio_tools:__subpackages__", + "//examples/python:__subpackages__", ]) load("//bazel:cython_library.bzl", "pyx_library") @@ -48,4 +49,5 @@ py_library( "//src/python/grpcio/grpc:grpcio", "@com_google_protobuf//:protobuf_python", ], + imports=["."], )