From 81cdf1e34e8501e3e0e50182dd6d13499b473868 Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Thu, 20 Oct 2016 14:42:31 -0700 Subject: [PATCH 1/4] Fix Python tests' module loading behavior --- src/python/grpcio_tests/tests/_loader.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/python/grpcio_tests/tests/_loader.py b/src/python/grpcio_tests/tests/_loader.py index c2f097f6c61..621bedc7bb8 100644 --- a/src/python/grpcio_tests/tests/_loader.py +++ b/src/python/grpcio_tests/tests/_loader.py @@ -84,11 +84,9 @@ class Loader(object): along. """ for importer, module_name, is_package in ( - pkgutil.iter_modules(package_paths)): + pkgutil.walk_packages(package_paths)): module = importer.find_module(module_name).load_module(module_name) self.visit_module(module) - if is_package: - self.walk_packages(module.__path__) def visit_module(self, module): """Visits the module, adding discovered tests to the test suite. From d953959e2bafd645b6ed674861a310daba5f80ae Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Thu, 20 Oct 2016 14:47:14 -0700 Subject: [PATCH 2/4] Enable split code generation To support magical internal build processes, the pb2 files need to be split into pure-proto and gRPC parts. This performs that split and further fixes bad module names in the test harness that interfered with the intended test implementation. An unfortunate side effect, due to limitations of protoc and holdover behavior we must support in major version 1.x, is that trash files are generated in split generation. This shouldn't be a problem in normal protoc plugin use. --- src/compiler/python_generator.cc | 386 +++++++++++------- src/compiler/python_generator.h | 4 - src/python/grpcio_health_checking/.gitignore | 1 + src/python/grpcio_tests/.gitignore | 1 + src/python/grpcio_tests/setup.py | 10 +- .../protoc_plugin/_split_definitions_test.py | 304 ++++++++++++++ .../protos/invocation_testing/__init__.py | 30 ++ .../protos/invocation_testing/same.proto | 39 ++ .../split_messages/__init__.py | 30 ++ .../split_messages/messages.proto | 35 ++ .../split_services/__init__.py | 30 ++ .../split_services/services.proto | 38 ++ src/python/grpcio_tests/tests/tests.json | 90 ++-- 13 files changed, 812 insertions(+), 186 deletions(-) create mode 100644 src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py create mode 100644 src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/__init__.py create mode 100644 src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/same.proto create mode 100644 src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/__init__.py create mode 100644 src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/messages.proto create mode 100644 src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/__init__.py create mode 100644 src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/services.proto diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index 6830f49931d..270291ac33e 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include #include @@ -66,66 +68,7 @@ using std::vector; namespace grpc_python_generator { -GeneratorConfiguration::GeneratorConfiguration() - : grpc_package_root("grpc"), beta_package_root("grpc.beta") {} - -PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config) - : config_(config) {} - -PythonGrpcGenerator::~PythonGrpcGenerator() {} - -bool PythonGrpcGenerator::Generate(const FileDescriptor* file, - const grpc::string& parameter, - GeneratorContext* context, - grpc::string* error) const { - // Get output file name. - grpc::string file_name; - static const int proto_suffix_length = strlen(".proto"); - if (file->name().size() > static_cast(proto_suffix_length) && - file->name().find_last_of(".proto") == file->name().size() - 1) { - file_name = - file->name().substr(0, file->name().size() - proto_suffix_length) + - "_pb2.py"; - } else { - *error = "Invalid proto file name. Proto file must end with .proto"; - return false; - } - - std::unique_ptr output( - context->OpenForInsert(file_name, "module_scope")); - CodedOutputStream coded_out(output.get()); - bool success = false; - grpc::string code = ""; - tie(success, code) = grpc_python_generator::GetServices(file, config_); - if (success) { - coded_out.WriteRaw(code.data(), code.size()); - return true; - } else { - return false; - } -} - namespace { -////////////////////////////////// -// BEGIN FORMATTING BOILERPLATE // -////////////////////////////////// - -// Converts an initializer list of the form { key0, value0, key1, value1, ... } -// into a map of key* to value*. Is merely a readability helper for later code. -map ListToDict( - const initializer_list& values) { - assert(values.size() % 2 == 0); - map value_map; - auto value_iter = values.begin(); - for (unsigned i = 0; i < values.size() / 2; ++i) { - grpc::string key = *value_iter; - ++value_iter; - grpc::string value = *value_iter; - value_map[key] = value; - ++value_iter; - } - return value_map; -} // Provides RAII indentation handling. Use as: // { @@ -146,10 +89,6 @@ class IndentScope { Printer* printer_; }; -//////////////////////////////// -// END FORMATTING BOILERPLATE // -//////////////////////////////// - // TODO(https://github.com/google/protobuf/issues/888): // Export `ModuleName` from protobuf's // `src/google/protobuf/compiler/python/python_generator.cc` file. @@ -173,9 +112,61 @@ grpc::string ModuleAlias(const grpc::string& filename) { return module_name; } -bool GetModuleAndMessagePath(const Descriptor* type, - const ServiceDescriptor* service, - grpc::string* out) { +// Tucks all generator state in an anonymous namespace away from +// PythonGrpcGenerator and the header file, mostly to encourage future changes +// to not require updates to the grpcio-tools C++ code part. Assumes that it is +// only ever used from a single thread. +struct PrivateGenerator { + const GeneratorConfiguration& config; + const FileDescriptor *file; + + bool generate_in_pb2_grpc; + + Printer *out; + + PrivateGenerator(const GeneratorConfiguration& config, + const FileDescriptor *file); + + std::pair GetGrpcServices(); + + private: + bool PrintPreamble(); + bool PrintBetaPreamble(); + bool PrintGAServices(); + bool PrintBetaServices(); + + bool PrintAddServicerToServer( + const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service); + bool PrintServicer(const ServiceDescriptor* service); + bool PrintStub(const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service); + + bool PrintBetaServicer(const ServiceDescriptor* service); + bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service); + bool PrintBetaStub(const ServiceDescriptor* service); + bool PrintBetaStubFactory( + const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service); + + // Get all comments (leading, leading_detached, trailing) and print them as a + // docstring. Any leading space of a line will be removed, but the line + // wrapping will not be changed. + template + void PrintAllComments(const DescriptorType* descriptor); + + bool GetModuleAndMessagePath(const Descriptor* type, + grpc::string* out); +}; + + +PrivateGenerator::PrivateGenerator(const GeneratorConfiguration& config, + const FileDescriptor *file) + : config(config), file(file) {} + +bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, + grpc::string* out) { const Descriptor* path_elem_type = type; vector message_path; do { @@ -188,9 +179,13 @@ bool GetModuleAndMessagePath(const Descriptor* type, file_name.find_last_of(".proto") == file_name.size() - 1)) { return false; } - grpc::string service_file_name = service->file()->name(); - grpc::string module = - service_file_name == file_name ? "" : ModuleAlias(file_name) + "."; + grpc::string generator_file_name = file->name(); + grpc::string module; + if (generator_file_name != file_name || generate_in_pb2_grpc) { + module = ModuleAlias(file_name) + "."; + } else { + module = ""; + } grpc::string message_type; for (auto path_iter = message_path.rbegin(); path_iter != message_path.rend(); ++path_iter) { @@ -202,33 +197,30 @@ bool GetModuleAndMessagePath(const Descriptor* type, return true; } -// Get all comments (leading, leading_detached, trailing) and print them as a -// docstring. Any leading space of a line will be removed, but the line wrapping -// will not be changed. template -static void PrintAllComments(const DescriptorType* desc, Printer* printer) { - std::vector comments; - grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, +void PrivateGenerator::PrintAllComments(const DescriptorType* descriptor) { + vector comments; + grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &comments); - grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING, + grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING, &comments); - grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING, + grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_TRAILING, &comments); if (comments.empty()) { return; } - printer->Print("\"\"\""); + out->Print("\"\"\""); for (auto it = comments.begin(); it != comments.end(); ++it) { size_t start_pos = it->find_first_not_of(' '); if (start_pos != grpc::string::npos) { - printer->Print(it->c_str() + start_pos); + out->Print(it->c_str() + start_pos); } - printer->Print("\n"); + out->Print("\n"); } - printer->Print("\"\"\"\n"); + out->Print("\"\"\"\n"); } -bool PrintBetaServicer(const ServiceDescriptor* service, Printer* out) { +bool PrivateGenerator::PrintBetaServicer(const ServiceDescriptor* service) { out->Print("\n\n"); out->Print("class Beta$Service$Servicer(object):\n", "Service", service->name()); @@ -241,7 +233,7 @@ bool PrintBetaServicer(const ServiceDescriptor* service, Printer* out) { "generated\n" "only to ease transition from grpcio<0.15.0 to " "grpcio>=0.15.0.\"\"\"\n"); - PrintAllComments(service, out); + PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { auto meth = service->method(i); grpc::string arg_name = @@ -250,7 +242,7 @@ bool PrintBetaServicer(const ServiceDescriptor* service, Printer* out) { meth->name(), "ArgName", arg_name); { IndentScope raii_method_indent(out); - PrintAllComments(meth, out); + PrintAllComments(meth); out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); } } @@ -258,7 +250,7 @@ bool PrintBetaServicer(const ServiceDescriptor* service, Printer* out) { return true; } -bool PrintBetaStub(const ServiceDescriptor* service, Printer* out) { +bool PrivateGenerator::PrintBetaStub(const ServiceDescriptor* service) { out->Print("\n\n"); out->Print("class Beta$Service$Stub(object):\n", "Service", service->name()); { @@ -270,18 +262,20 @@ bool PrintBetaStub(const ServiceDescriptor* service, Printer* out) { "generated\n" "only to ease transition from grpcio<0.15.0 to " "grpcio>=0.15.0.\"\"\"\n"); - PrintAllComments(service, out); + PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { const MethodDescriptor* meth = service->method(i); grpc::string arg_name = meth->client_streaming() ? "request_iterator" : "request"; - auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name}); + auto methdict; + methdict["Method"] = meth->name(); + methdict["ArgName"] = arg_name; out->Print(methdict, "def $Method$(self, $ArgName$, timeout, metadata=None, " "with_call=False, protocol_options=None):\n"); { IndentScope raii_method_indent(out); - PrintAllComments(meth, out); + PrintAllComments(meth); out->Print("raise NotImplementedError()\n"); } if (!meth->server_streaming()) { @@ -292,8 +286,9 @@ bool PrintBetaStub(const ServiceDescriptor* service, Printer* out) { return true; } -bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, - const ServiceDescriptor* service, Printer* out) { +bool PrivateGenerator::PrintBetaServerFactory( + const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service) { out->Print("\n\n"); out->Print( "def beta_create_$Service$_server(servicer, pool=None, " @@ -317,12 +312,12 @@ bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, grpc::string(method->server_streaming() ? "stream_" : "unary_") + "inline"; grpc::string input_message_module_and_class; - if (!GetModuleAndMessagePath(method->input_type(), service, + if (!GetModuleAndMessagePath(method->input_type(), &input_message_module_and_class)) { return false; } grpc::string output_message_module_and_class; - if (!GetModuleAndMessagePath(method->output_type(), service, + if (!GetModuleAndMessagePath(method->output_type(), &output_message_module_and_class)) { return false; } @@ -395,11 +390,11 @@ bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, return true; } -bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, - const ServiceDescriptor* service, Printer* out) { - map dict = ListToDict({ - "Service", service->name(), - }); +bool PrivateGenerator::PrintBetaStubFactory( + const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service) { + map dict; + dict["Service"] = service->name(); out->Print("\n\n"); out->Print(dict, "def beta_create_$Service$_stub(channel, host=None," @@ -421,12 +416,12 @@ bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, grpc::string(method->client_streaming() ? "STREAM" : "UNARY") + "_" + grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); grpc::string input_message_module_and_class; - if (!GetModuleAndMessagePath(method->input_type(), service, + if (!GetModuleAndMessagePath(method->input_type(), &input_message_module_and_class)) { return false; } grpc::string output_message_module_and_class; - if (!GetModuleAndMessagePath(method->output_type(), service, + if (!GetModuleAndMessagePath(method->output_type(), &output_message_module_and_class)) { return false; } @@ -493,13 +488,13 @@ bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, return true; } -bool PrintStub(const grpc::string& package_qualified_service_name, - const ServiceDescriptor* service, Printer* out) { +bool PrivateGenerator::PrintStub(const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service) { out->Print("\n\n"); out->Print("class $Service$Stub(object):\n", "Service", service->name()); { IndentScope raii_class_indent(out); - PrintAllComments(service, out); + PrintAllComments(service); out->Print("\n"); out->Print("def __init__(self, channel):\n"); { @@ -518,12 +513,12 @@ bool PrintStub(const grpc::string& package_qualified_service_name, grpc::string(method->client_streaming() ? "stream" : "unary") + "_" + grpc::string(method->server_streaming() ? "stream" : "unary"); grpc::string request_module_and_class; - if (!GetModuleAndMessagePath(method->input_type(), service, + if (!GetModuleAndMessagePath(method->input_type(), &request_module_and_class)) { return false; } grpc::string response_module_and_class; - if (!GetModuleAndMessagePath(method->output_type(), service, + if (!GetModuleAndMessagePath(method->output_type(), &response_module_and_class)) { return false; } @@ -550,12 +545,12 @@ bool PrintStub(const grpc::string& package_qualified_service_name, return true; } -bool PrintServicer(const ServiceDescriptor* service, Printer* out) { +bool PrivateGenerator::PrintServicer(const ServiceDescriptor* service) { out->Print("\n\n"); out->Print("class $Service$Servicer(object):\n", "Service", service->name()); { IndentScope raii_class_indent(out); - PrintAllComments(service, out); + PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); grpc::string arg_name = @@ -565,7 +560,7 @@ bool PrintServicer(const ServiceDescriptor* service, Printer* out) { method->name(), "ArgName", arg_name); { IndentScope raii_method_indent(out); - PrintAllComments(method, out); + PrintAllComments(method); out->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n"); out->Print("context.set_details('Method not implemented!')\n"); out->Print("raise NotImplementedError('Method not implemented!')\n"); @@ -575,9 +570,9 @@ bool PrintServicer(const ServiceDescriptor* service, Printer* out) { return true; } -bool PrintAddServicerToServer( +bool PrivateGenerator::PrintAddServicerToServer( const grpc::string& package_qualified_service_name, - const ServiceDescriptor* service, Printer* out) { + const ServiceDescriptor* service) { out->Print("\n\n"); out->Print("def add_$Service$Servicer_to_server(servicer, server):\n", "Service", service->name()); @@ -595,12 +590,12 @@ bool PrintAddServicerToServer( grpc::string(method->server_streaming() ? "stream" : "unary") + "_rpc_method_handler"; grpc::string request_module_and_class; - if (!GetModuleAndMessagePath(method->input_type(), service, + if (!GetModuleAndMessagePath(method->input_type(), &request_module_and_class)) { return false; } grpc::string response_module_and_class; - if (!GetModuleAndMessagePath(method->output_type(), service, + if (!GetModuleAndMessagePath(method->output_type(), &response_module_and_class)) { return false; } @@ -635,53 +630,170 @@ bool PrintAddServicerToServer( return true; } -bool PrintPreamble(const FileDescriptor* file, - const GeneratorConfiguration& config, Printer* out) { - out->Print("import $Package$\n", "Package", config.grpc_package_root); +bool PrivateGenerator::PrintBetaPreamble() { out->Print("from $Package$ import implementations as beta_implementations\n", "Package", config.beta_package_root); out->Print("from $Package$ import interfaces as beta_interfaces\n", "Package", config.beta_package_root); + return true; +} + +bool PrivateGenerator::PrintPreamble() { + out->Print("import $Package$\n", "Package", config.grpc_package_root); out->Print("from grpc.framework.common import cardinality\n"); out->Print( "from grpc.framework.interfaces.face import utilities as " "face_utilities\n"); + if (generate_in_pb2_grpc) { + out->Print("\n"); + for (int i = 0; i < file->service_count(); ++i) { + const ServiceDescriptor *service = file->service(i); + for (int j = 0; j < service->method_count(); ++j) { + auto method = service->method(j); + const Descriptor *types[2] = {method->input_type(), method->output_type()}; + for (int k = 0; k < 2; ++k) { + const Descriptor *type = types[k]; + grpc::string type_file_name = type->file()->name(); + grpc::string module_name = ModuleName(type_file_name); + grpc::string module_alias = ModuleAlias(type_file_name); + out->Print("import $ModuleName$ as $ModuleAlias$\n", "ModuleName", module_name, "ModuleAlias", module_alias); + } + } + } + } return true; } -} // namespace +bool PrivateGenerator::PrintGAServices() { + grpc::string package = file->package(); + if (!package.empty()) { + package = package.append("."); + } + for (int i = 0; i < file->service_count(); ++i) { + const ServiceDescriptor *service = file->service(i); + grpc::string package_qualified_service_name = package + service->name(); + if (!(PrintStub(package_qualified_service_name, service) && + PrintServicer(service) && + PrintAddServicerToServer(package_qualified_service_name, service))) { + return false; + } + } + return true; +} + +bool PrivateGenerator::PrintBetaServices() { + grpc::string package = file->package(); + if (!package.empty()) { + package = package.append("."); + } + for (int i = 0; i < file->service_count(); ++i) { + const ServiceDescriptor *service = file->service(i); + grpc::string package_qualified_service_name = package + service->name(); + if (!(PrintBetaServicer(service) && PrintBetaStub(service) && + PrintBetaServerFactory(package_qualified_service_name, service) && + PrintBetaStubFactory(package_qualified_service_name, service))) { + return false; + } + } + return true; +} -pair GetServices(const FileDescriptor* file, - const GeneratorConfiguration& config) { +pair PrivateGenerator::GetGrpcServices() { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. StringOutputStream output_stream(&output); - Printer out(&output_stream, '$'); - if (!PrintPreamble(file, config, &out)) { - return make_pair(false, ""); - } - auto package = file->package(); - if (!package.empty()) { - package = package.append("."); - } - for (int i = 0; i < file->service_count(); ++i) { - auto service = file->service(i); - auto package_qualified_service_name = package + service->name(); - if (!(PrintStub(package_qualified_service_name, service, &out) && - PrintServicer(service, &out) && - PrintAddServicerToServer(package_qualified_service_name, service, - &out) && - PrintBetaServicer(service, &out) && PrintBetaStub(service, &out) && - PrintBetaServerFactory(package_qualified_service_name, service, - &out) && - PrintBetaStubFactory(package_qualified_service_name, service, - &out))) { + Printer out_printer(&output_stream, '$'); + out = &out_printer; + + if (generate_in_pb2_grpc) { + if (!PrintPreamble()) { return make_pair(false, ""); } + if (!PrintGAServices()) { + return make_pair(false, ""); + } + } else { + out->Print("try:\n"); + { + IndentScope raii_dict_try_indent(out); + out->Print("# THESE ELEMENTS WILL BE DEPRECATED.\n" + "# Please use the generated *_pb2_grpc.py files instead.\n"); + if (!PrintPreamble()) { + return make_pair(false, ""); + } + if (!PrintBetaPreamble()) { + return make_pair(false, ""); + } + if (!PrintGAServices()) { + return make_pair(false, ""); + } + if (!PrintBetaServices()) { + return make_pair(false, ""); + } + } + out->Print("except ImportError:\n"); + { + IndentScope raii_dict_except_indent(out); + out->Print("pass"); + } } } return make_pair(true, std::move(output)); } +} // namespace + +GeneratorConfiguration::GeneratorConfiguration() + : grpc_package_root("grpc"), beta_package_root("grpc.beta") {} + +PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config) + : config_(config) {} + +PythonGrpcGenerator::~PythonGrpcGenerator() {} + +bool PythonGrpcGenerator::Generate(const FileDescriptor* file, + const grpc::string& parameter, + GeneratorContext* context, + grpc::string* error) const { + // Get output file name. + grpc::string pb2_file_name; + grpc::string pb2_grpc_file_name; + static const int proto_suffix_length = strlen(".proto"); + if (file->name().size() > static_cast(proto_suffix_length) && + file->name().find_last_of(".proto") == file->name().size() - 1) { + grpc::string base = file->name().substr( + 0, file->name().size() - proto_suffix_length); + pb2_file_name = base + "_pb2.py"; + pb2_grpc_file_name = base + "_pb2_grpc.py"; + } else { + *error = "Invalid proto file name. Proto file must end with .proto"; + return false; + } + + PrivateGenerator generator(config_, file); + + std::unique_ptr pb2_output( + context->OpenForAppend(pb2_file_name)); + std::unique_ptr grpc_output( + context->Open(pb2_grpc_file_name)); + CodedOutputStream pb2_coded_out(pb2_output.get()); + CodedOutputStream grpc_coded_out(grpc_output.get()); + bool success = false; + grpc::string pb2_code; + grpc::string grpc_code; + generator.generate_in_pb2_grpc = false; + tie(success, pb2_code) = generator.GetGrpcServices(); + if (success) { + generator.generate_in_pb2_grpc = true; + tie(success, grpc_code) = generator.GetGrpcServices(); + if (success) { + pb2_coded_out.WriteRaw(pb2_code.data(), pb2_code.size()); + grpc_coded_out.WriteRaw(grpc_code.data(), grpc_code.size()); + return true; + } + } + return false; +} + } // namespace grpc_python_generator diff --git a/src/compiler/python_generator.h b/src/compiler/python_generator.h index 9bbb83bca6e..6a95255d40e 100644 --- a/src/compiler/python_generator.h +++ b/src/compiler/python_generator.h @@ -62,10 +62,6 @@ class PythonGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { GeneratorConfiguration config_; }; -std::pair GetServices( - const grpc::protobuf::FileDescriptor* file, - const GeneratorConfiguration& config); - } // namespace grpc_python_generator #endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_H diff --git a/src/python/grpcio_health_checking/.gitignore b/src/python/grpcio_health_checking/.gitignore index 85af4668866..432c3194f04 100644 --- a/src/python/grpcio_health_checking/.gitignore +++ b/src/python/grpcio_health_checking/.gitignore @@ -1,5 +1,6 @@ *.proto *_pb2.py +*_pb2_grpc.py build/ grpcio_health_checking.egg-info/ dist/ diff --git a/src/python/grpcio_tests/.gitignore b/src/python/grpcio_tests/.gitignore index fc620135dc7..dcba283a8ca 100644 --- a/src/python/grpcio_tests/.gitignore +++ b/src/python/grpcio_tests/.gitignore @@ -1,4 +1,5 @@ proto/ src/ *_pb2.py +*_pb2_grpc.py *.egg-info/ diff --git a/src/python/grpcio_tests/setup.py b/src/python/grpcio_tests/setup.py index 73842066020..01d5fa875b9 100644 --- a/src/python/grpcio_tests/setup.py +++ b/src/python/grpcio_tests/setup.py @@ -80,8 +80,14 @@ PACKAGE_DATA = { 'credentials/server1.key', 'credentials/server1.pem', ], - 'tests.protoc_plugin': [ - 'protoc_plugin_test.proto', + 'tests.protoc_plugin.protos.invocation_testing': [ + 'same.proto', + ], + 'tests.protoc_plugin.protos.invocation_testing.split_messages': [ + 'messages.proto', + ], + 'tests.protoc_plugin.protos.invocation_testing.split_services': [ + 'services.proto', ], 'tests.unit': [ 'credentials/ca.pem', diff --git a/src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py b/src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py new file mode 100644 index 00000000000..089366a8c72 --- /dev/null +++ b/src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py @@ -0,0 +1,304 @@ +# Copyright 2016, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import collections +from concurrent import futures +import contextlib +import distutils.spawn +import errno +import importlib +import os +import os.path +import pkgutil +import shutil +import subprocess +import sys +import tempfile +import threading +import unittest + +import grpc +from grpc.tools import protoc +from tests.unit.framework.common import test_constants + +_MESSAGES_IMPORT = b'import "messages.proto";' + +@contextlib.contextmanager +def _system_path(path): + old_system_path = sys.path[:] + sys.path = sys.path[0:1] + path + sys.path[1:] + yield + sys.path = old_system_path + + +class DummySplitServicer(object): + + def __init__(self, request_class, response_class): + self.request_class = request_class + self.response_class = response_class + + def Call(self, request, context): + return self.response_class() + + +class SeparateTestMixin(object): + + def testImportAttributes(self): + with _system_path([self.python_out_directory]): + pb2 = importlib.import_module(self.pb2_import) + pb2.Request + pb2.Response + if self.should_find_services_in_pb2: + pb2.TestServiceServicer + else: + with self.assertRaises(AttributeError): + pb2.TestServiceServicer + + with _system_path([self.grpc_python_out_directory]): + pb2_grpc = importlib.import_module(self.pb2_grpc_import) + pb2_grpc.TestServiceServicer + with self.assertRaises(AttributeError): + pb2_grpc.Request + with self.assertRaises(AttributeError): + pb2_grpc.Response + + def testCall(self): + with _system_path([self.python_out_directory]): + pb2 = importlib.import_module(self.pb2_import) + with _system_path([self.grpc_python_out_directory]): + pb2_grpc = importlib.import_module(self.pb2_grpc_import) + server = grpc.server( + futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE)) + pb2_grpc.add_TestServiceServicer_to_server( + DummySplitServicer( + pb2.Request, pb2.Response), server) + port = server.add_insecure_port('[::]:0') + server.start() + channel = grpc.insecure_channel('localhost:{}'.format(port)) + stub = pb2_grpc.TestServiceStub(channel) + request = pb2.Request() + expected_response = pb2.Response() + response = stub.Call(request) + self.assertEqual(expected_response, response) + + +class CommonTestMixin(object): + + def testImportAttributes(self): + with _system_path([self.python_out_directory]): + pb2 = importlib.import_module(self.pb2_import) + pb2.Request + pb2.Response + if self.should_find_services_in_pb2: + pb2.TestServiceServicer + else: + with self.assertRaises(AttributeError): + pb2.TestServiceServicer + + with _system_path([self.grpc_python_out_directory]): + pb2_grpc = importlib.import_module(self.pb2_grpc_import) + pb2_grpc.TestServiceServicer + with self.assertRaises(AttributeError): + pb2_grpc.Request + with self.assertRaises(AttributeError): + pb2_grpc.Response + + def testCall(self): + with _system_path([self.python_out_directory]): + pb2 = importlib.import_module(self.pb2_import) + with _system_path([self.grpc_python_out_directory]): + pb2_grpc = importlib.import_module(self.pb2_grpc_import) + server = grpc.server( + futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE)) + pb2_grpc.add_TestServiceServicer_to_server( + DummySplitServicer( + pb2.Request, pb2.Response), server) + port = server.add_insecure_port('[::]:0') + server.start() + channel = grpc.insecure_channel('localhost:{}'.format(port)) + stub = pb2_grpc.TestServiceStub(channel) + request = pb2.Request() + expected_response = pb2.Response() + response = stub.Call(request) + self.assertEqual(expected_response, response) + + +class SameSeparateTest(unittest.TestCase, SeparateTestMixin): + + def setUp(self): + same_proto_contents = pkgutil.get_data( + 'tests.protoc_plugin.protos.invocation_testing', 'same.proto') + self.directory = tempfile.mkdtemp(suffix='same_separate', dir='.') + self.proto_directory = os.path.join(self.directory, 'proto_path') + self.python_out_directory = os.path.join(self.directory, 'python_out') + self.grpc_python_out_directory = os.path.join(self.directory, 'grpc_python_out') + os.makedirs(self.proto_directory) + os.makedirs(self.python_out_directory) + os.makedirs(self.grpc_python_out_directory) + same_proto_file = os.path.join(self.proto_directory, 'same_separate.proto') + open(same_proto_file, 'wb').write(same_proto_contents) + protoc_result = protoc.main([ + '', + '--proto_path={}'.format(self.proto_directory), + '--python_out={}'.format(self.python_out_directory), + '--grpc_python_out={}'.format(self.grpc_python_out_directory), + same_proto_file, + ]) + if protoc_result != 0: + raise Exception("unexpected protoc error") + open(os.path.join(self.grpc_python_out_directory, '__init__.py'), 'w').write('') + open(os.path.join(self.python_out_directory, '__init__.py'), 'w').write('') + self.pb2_import = 'same_separate_pb2' + self.pb2_grpc_import = 'same_separate_pb2_grpc' + self.should_find_services_in_pb2 = False + + def tearDown(self): + shutil.rmtree(self.directory) + + +class SameCommonTest(unittest.TestCase, CommonTestMixin): + + def setUp(self): + same_proto_contents = pkgutil.get_data( + 'tests.protoc_plugin.protos.invocation_testing', 'same.proto') + self.directory = tempfile.mkdtemp(suffix='same_common', dir='.') + self.proto_directory = os.path.join(self.directory, 'proto_path') + self.python_out_directory = os.path.join(self.directory, 'python_out') + self.grpc_python_out_directory = self.python_out_directory + os.makedirs(self.proto_directory) + os.makedirs(self.python_out_directory) + same_proto_file = os.path.join(self.proto_directory, 'same_common.proto') + open(same_proto_file, 'wb').write(same_proto_contents) + protoc_result = protoc.main([ + '', + '--proto_path={}'.format(self.proto_directory), + '--python_out={}'.format(self.python_out_directory), + '--grpc_python_out={}'.format(self.grpc_python_out_directory), + same_proto_file, + ]) + if protoc_result != 0: + raise Exception("unexpected protoc error") + open(os.path.join(self.python_out_directory, '__init__.py'), 'w').write('') + self.pb2_import = 'same_common_pb2' + self.pb2_grpc_import = 'same_common_pb2_grpc' + self.should_find_services_in_pb2 = True + + def tearDown(self): + shutil.rmtree(self.directory) + + +class SplitCommonTest(unittest.TestCase, CommonTestMixin): + + def setUp(self): + services_proto_contents = pkgutil.get_data( + 'tests.protoc_plugin.protos.invocation_testing.split_services', + 'services.proto') + messages_proto_contents = pkgutil.get_data( + 'tests.protoc_plugin.protos.invocation_testing.split_messages', + 'messages.proto') + self.directory = tempfile.mkdtemp(suffix='split_common', dir='.') + self.proto_directory = os.path.join(self.directory, 'proto_path') + self.python_out_directory = os.path.join(self.directory, 'python_out') + self.grpc_python_out_directory = self.python_out_directory + os.makedirs(self.proto_directory) + os.makedirs(self.python_out_directory) + services_proto_file = os.path.join(self.proto_directory, + 'split_common_services.proto') + messages_proto_file = os.path.join(self.proto_directory, + 'split_common_messages.proto') + open(services_proto_file, 'wb').write(services_proto_contents.replace( + _MESSAGES_IMPORT, + b'import "split_common_messages.proto";' + )) + open(messages_proto_file, 'wb').write(messages_proto_contents) + protoc_result = protoc.main([ + '', + '--proto_path={}'.format(self.proto_directory), + '--python_out={}'.format(self.python_out_directory), + '--grpc_python_out={}'.format(self.python_out_directory), + services_proto_file, + messages_proto_file, + ]) + if protoc_result != 0: + raise Exception("unexpected protoc error") + open(os.path.join(self.python_out_directory, '__init__.py'), 'w').write('') + self.pb2_import = 'split_common_messages_pb2' + self.pb2_grpc_import = 'split_common_services_pb2_grpc' + self.should_find_services_in_pb2 = False + + def tearDown(self): + shutil.rmtree(self.directory) + + +class SplitSeparateTest(unittest.TestCase, SeparateTestMixin): + + def setUp(self): + services_proto_contents = pkgutil.get_data( + 'tests.protoc_plugin.protos.invocation_testing.split_services', + 'services.proto') + messages_proto_contents = pkgutil.get_data( + 'tests.protoc_plugin.protos.invocation_testing.split_messages', + 'messages.proto') + self.directory = tempfile.mkdtemp(suffix='split_separate', dir='.') + self.proto_directory = os.path.join(self.directory, 'proto_path') + self.python_out_directory = os.path.join(self.directory, 'python_out') + self.grpc_python_out_directory = os.path.join(self.directory, 'grpc_python_out') + os.makedirs(self.proto_directory) + os.makedirs(self.python_out_directory) + os.makedirs(self.grpc_python_out_directory) + services_proto_file = os.path.join(self.proto_directory, + 'split_separate_services.proto') + messages_proto_file = os.path.join(self.proto_directory, + 'split_separate_messages.proto') + open(services_proto_file, 'wb').write(services_proto_contents.replace( + _MESSAGES_IMPORT, + b'import "split_separate_messages.proto";' + )) + open(messages_proto_file, 'wb').write(messages_proto_contents) + protoc_result = protoc.main([ + '', + '--proto_path={}'.format(self.proto_directory), + '--python_out={}'.format(self.python_out_directory), + '--grpc_python_out={}'.format(self.grpc_python_out_directory), + services_proto_file, + messages_proto_file, + ]) + if protoc_result != 0: + raise Exception("unexpected protoc error") + open(os.path.join(self.python_out_directory, '__init__.py'), 'w').write('') + self.pb2_import = 'split_separate_messages_pb2' + self.pb2_grpc_import = 'split_separate_services_pb2_grpc' + self.should_find_services_in_pb2 = False + + def tearDown(self): + shutil.rmtree(self.directory) + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/__init__.py b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/__init__.py new file mode 100644 index 00000000000..2f88fa04122 --- /dev/null +++ b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/__init__.py @@ -0,0 +1,30 @@ +# Copyright 2016, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + diff --git a/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/same.proto b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/same.proto new file mode 100644 index 00000000000..269e2fd2c7e --- /dev/null +++ b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/same.proto @@ -0,0 +1,39 @@ +// Copyright 2016, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package grpc_protoc_plugin.invocation_testing; + +message Request {} +message Response {} + +service TestService { + rpc Call(Request) returns (Response); +} diff --git a/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/__init__.py b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/__init__.py new file mode 100644 index 00000000000..2f88fa04122 --- /dev/null +++ b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/__init__.py @@ -0,0 +1,30 @@ +# Copyright 2016, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + diff --git a/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/messages.proto b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/messages.proto new file mode 100644 index 00000000000..de22dae0492 --- /dev/null +++ b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_messages/messages.proto @@ -0,0 +1,35 @@ +// Copyright 2016, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package grpc_protoc_plugin.invocation_testing.split; + +message Request {} +message Response {} diff --git a/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/__init__.py b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/__init__.py new file mode 100644 index 00000000000..2f88fa04122 --- /dev/null +++ b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/__init__.py @@ -0,0 +1,30 @@ +# Copyright 2016, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + diff --git a/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/services.proto b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/services.proto new file mode 100644 index 00000000000..af999cd48db --- /dev/null +++ b/src/python/grpcio_tests/tests/protoc_plugin/protos/invocation_testing/split_services/services.proto @@ -0,0 +1,38 @@ +// Copyright 2016, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +import "messages.proto"; + +package grpc_protoc_plugin.invocation_testing.split; + +service TestService { + rpc Call(Request) returns (Response); +} diff --git a/src/python/grpcio_tests/tests/tests.json b/src/python/grpcio_tests/tests/tests.json index 2e85f1f397b..dd4a0257f54 100644 --- a/src/python/grpcio_tests/tests/tests.json +++ b/src/python/grpcio_tests/tests/tests.json @@ -1,45 +1,49 @@ [ - "_api_test.AllTest", - "_api_test.ChannelConnectivityTest", - "_api_test.ChannelTest", - "_auth_test.AccessTokenCallCredentialsTest", - "_auth_test.GoogleCallCredentialsTest", - "_beta_features_test.BetaFeaturesTest", - "_beta_features_test.ContextManagementAndLifecycleTest", - "_cancel_many_calls_test.CancelManyCallsTest", - "_channel_args_test.ChannelArgsTest", - "_channel_connectivity_test.ChannelConnectivityTest", - "_channel_ready_future_test.ChannelReadyFutureTest", - "_channel_test.ChannelTest", - "_compression_test.CompressionTest", - "_connectivity_channel_test.ConnectivityStatesTest", - "_credentials_test.CredentialsTest", - "_empty_message_test.EmptyMessageTest", - "_exit_test.ExitTest", - "_face_interface_test.DynamicInvokerBlockingInvocationInlineServiceTest", - "_face_interface_test.DynamicInvokerFutureInvocationAsynchronousEventServiceTest", - "_face_interface_test.GenericInvokerBlockingInvocationInlineServiceTest", - "_face_interface_test.GenericInvokerFutureInvocationAsynchronousEventServiceTest", - "_face_interface_test.MultiCallableInvokerBlockingInvocationInlineServiceTest", - "_face_interface_test.MultiCallableInvokerFutureInvocationAsynchronousEventServiceTest", - "_health_servicer_test.HealthServicerTest", - "_implementations_test.CallCredentialsTest", - "_implementations_test.ChannelCredentialsTest", - "_insecure_interop_test.InsecureInteropTest", - "_logging_pool_test.LoggingPoolTest", - "_metadata_code_details_test.MetadataCodeDetailsTest", - "_metadata_test.MetadataTest", - "_not_found_test.NotFoundTest", - "_python_plugin_test.PythonPluginTest", - "_read_some_but_not_all_responses_test.ReadSomeButNotAllResponsesTest", - "_reflection_servicer_test.ReflectionServicerTest", - "_rpc_test.RPCTest", - "_sanity_test.Sanity", - "_secure_interop_test.SecureInteropTest", - "_thread_cleanup_test.CleanupThreadTest", - "_utilities_test.ChannelConnectivityTest", - "beta_python_plugin_test.PythonPluginTest", - "cygrpc_test.InsecureServerInsecureClient", - "cygrpc_test.SecureServerSecureClient", - "cygrpc_test.TypeSmokeTest" + "health_check._health_servicer_test.HealthServicerTest", + "interop._insecure_interop_test.InsecureInteropTest", + "interop._secure_interop_test.SecureInteropTest", + "protoc_plugin._python_plugin_test.PythonPluginTest", + "protoc_plugin._split_definitions_test.SameCommonTest", + "protoc_plugin._split_definitions_test.SameSeparateTest", + "protoc_plugin._split_definitions_test.SplitCommonTest", + "protoc_plugin._split_definitions_test.SplitSeparateTest", + "protoc_plugin.beta_python_plugin_test.PythonPluginTest", + "reflection._reflection_servicer_test.ReflectionServicerTest", + "unit._api_test.AllTest", + "unit._api_test.ChannelConnectivityTest", + "unit._api_test.ChannelTest", + "unit._auth_test.AccessTokenCallCredentialsTest", + "unit._auth_test.GoogleCallCredentialsTest", + "unit._channel_args_test.ChannelArgsTest", + "unit._channel_connectivity_test.ChannelConnectivityTest", + "unit._channel_ready_future_test.ChannelReadyFutureTest", + "unit._compression_test.CompressionTest", + "unit._credentials_test.CredentialsTest", + "unit._cython._cancel_many_calls_test.CancelManyCallsTest", + "unit._cython._channel_test.ChannelTest", + "unit._cython._read_some_but_not_all_responses_test.ReadSomeButNotAllResponsesTest", + "unit._cython.cygrpc_test.InsecureServerInsecureClient", + "unit._cython.cygrpc_test.SecureServerSecureClient", + "unit._cython.cygrpc_test.TypeSmokeTest", + "unit._empty_message_test.EmptyMessageTest", + "unit._exit_test.ExitTest", + "unit._metadata_code_details_test.MetadataCodeDetailsTest", + "unit._metadata_test.MetadataTest", + "unit._rpc_test.RPCTest", + "unit._sanity._sanity_test.Sanity", + "unit._thread_cleanup_test.CleanupThreadTest", + "unit.beta._beta_features_test.BetaFeaturesTest", + "unit.beta._beta_features_test.ContextManagementAndLifecycleTest", + "unit.beta._connectivity_channel_test.ConnectivityStatesTest", + "unit.beta._face_interface_test.DynamicInvokerBlockingInvocationInlineServiceTest", + "unit.beta._face_interface_test.DynamicInvokerFutureInvocationAsynchronousEventServiceTest", + "unit.beta._face_interface_test.GenericInvokerBlockingInvocationInlineServiceTest", + "unit.beta._face_interface_test.GenericInvokerFutureInvocationAsynchronousEventServiceTest", + "unit.beta._face_interface_test.MultiCallableInvokerBlockingInvocationInlineServiceTest", + "unit.beta._face_interface_test.MultiCallableInvokerFutureInvocationAsynchronousEventServiceTest", + "unit.beta._implementations_test.CallCredentialsTest", + "unit.beta._implementations_test.ChannelCredentialsTest", + "unit.beta._not_found_test.NotFoundTest", + "unit.beta._utilities_test.ChannelConnectivityTest", + "unit.framework.foundation._logging_pool_test.LoggingPoolTest" ] From 4a051ccca6629ce59af589594a36c046e7d4bbd7 Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Thu, 20 Oct 2016 14:50:49 -0700 Subject: [PATCH 3/4] Clean-up types in Python generator --- src/compiler/python_generator.cc | 77 +++++++++++++++++--------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index 270291ac33e..9480bcc97b9 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -70,6 +70,10 @@ namespace grpc_python_generator { namespace { +typedef vector DescriptorVector; +typedef map StringMap; +typedef vector StringVector; + // Provides RAII indentation handling. Use as: // { // IndentScope raii_my_indent_var_name_here(my_py_printer); @@ -168,7 +172,7 @@ PrivateGenerator::PrivateGenerator(const GeneratorConfiguration& config, bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, grpc::string* out) { const Descriptor* path_elem_type = type; - vector message_path; + DescriptorVector message_path; do { message_path.push_back(path_elem_type); path_elem_type = path_elem_type->containing_type(); @@ -187,7 +191,8 @@ bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, module = ""; } grpc::string message_type; - for (auto path_iter = message_path.rbegin(); path_iter != message_path.rend(); + for (DescriptorVector::reverse_iterator path_iter = message_path.rbegin(); + path_iter != message_path.rend(); ++path_iter) { message_type += (*path_iter)->name() + "."; } @@ -199,7 +204,7 @@ bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, template void PrivateGenerator::PrintAllComments(const DescriptorType* descriptor) { - vector comments; + StringVector comments; grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &comments); grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING, @@ -210,7 +215,7 @@ void PrivateGenerator::PrintAllComments(const DescriptorType* descriptor) { return; } out->Print("\"\"\""); - for (auto it = comments.begin(); it != comments.end(); ++it) { + for (StringVector::iterator it = comments.begin(); it != comments.end(); ++it) { size_t start_pos = it->find_first_not_of(' '); if (start_pos != grpc::string::npos) { out->Print(it->c_str() + start_pos); @@ -235,14 +240,14 @@ bool PrivateGenerator::PrintBetaServicer(const ServiceDescriptor* service) { "grpcio>=0.15.0.\"\"\"\n"); PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { - auto meth = service->method(i); + const MethodDescriptor *method = service->method(i); grpc::string arg_name = - meth->client_streaming() ? "request_iterator" : "request"; + method->client_streaming() ? "request_iterator" : "request"; out->Print("def $Method$(self, $ArgName$, context):\n", "Method", - meth->name(), "ArgName", arg_name); + method->name(), "ArgName", arg_name); { IndentScope raii_method_indent(out); - PrintAllComments(meth); + PrintAllComments(method); out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); } } @@ -264,22 +269,22 @@ bool PrivateGenerator::PrintBetaStub(const ServiceDescriptor* service) { "grpcio>=0.15.0.\"\"\"\n"); PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { - const MethodDescriptor* meth = service->method(i); + const MethodDescriptor* method = service->method(i); grpc::string arg_name = - meth->client_streaming() ? "request_iterator" : "request"; - auto methdict; - methdict["Method"] = meth->name(); - methdict["ArgName"] = arg_name; - out->Print(methdict, + method->client_streaming() ? "request_iterator" : "request"; + StringMap method_dict; + method_dict["Method"] = method->name(); + method_dict["ArgName"] = arg_name; + out->Print(method_dict, "def $Method$(self, $ArgName$, timeout, metadata=None, " "with_call=False, protocol_options=None):\n"); { IndentScope raii_method_indent(out); - PrintAllComments(meth); + PrintAllComments(method); out->Print("raise NotImplementedError()\n"); } - if (!meth->server_streaming()) { - out->Print(methdict, "$Method$.future = None\n"); + if (!method->server_streaming()) { + out->Print(method_dict, "$Method$.future = None\n"); } } } @@ -302,9 +307,9 @@ bool PrivateGenerator::PrintBetaServerFactory( "file not marked beta) for all further purposes. This function was\n" "generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0" "\"\"\"\n"); - map method_implementation_constructors; - map input_message_modules_and_classes; - map output_message_modules_and_classes; + StringMap method_implementation_constructors; + StringMap input_message_modules_and_classes; + StringMap output_message_modules_and_classes; for (int i = 0; i < service->method_count(); ++i) { const MethodDescriptor* method = service->method(i); const grpc::string method_implementation_constructor = @@ -329,7 +334,7 @@ bool PrivateGenerator::PrintBetaServerFactory( make_pair(method->name(), output_message_module_and_class)); } out->Print("request_deserializers = {\n"); - for (auto name_and_input_module_class_pair = + for (StringMap::iterator name_and_input_module_class_pair = input_message_modules_and_classes.begin(); name_and_input_module_class_pair != input_message_modules_and_classes.end(); @@ -344,7 +349,7 @@ bool PrivateGenerator::PrintBetaServerFactory( } out->Print("}\n"); out->Print("response_serializers = {\n"); - for (auto name_and_output_module_class_pair = + for (StringMap::iterator name_and_output_module_class_pair = output_message_modules_and_classes.begin(); name_and_output_module_class_pair != output_message_modules_and_classes.end(); @@ -360,7 +365,7 @@ bool PrivateGenerator::PrintBetaServerFactory( } out->Print("}\n"); out->Print("method_implementations = {\n"); - for (auto name_and_implementation_constructor = + for (StringMap::iterator name_and_implementation_constructor = method_implementation_constructors.begin(); name_and_implementation_constructor != method_implementation_constructors.end(); @@ -393,7 +398,7 @@ bool PrivateGenerator::PrintBetaServerFactory( bool PrivateGenerator::PrintBetaStubFactory( const grpc::string& package_qualified_service_name, const ServiceDescriptor* service) { - map dict; + StringMap dict; dict["Service"] = service->name(); out->Print("\n\n"); out->Print(dict, @@ -407,9 +412,9 @@ bool PrivateGenerator::PrintBetaStubFactory( "file not marked beta) for all further purposes. This function was\n" "generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0" "\"\"\"\n"); - map method_cardinalities; - map input_message_modules_and_classes; - map output_message_modules_and_classes; + StringMap method_cardinalities; + StringMap input_message_modules_and_classes; + StringMap output_message_modules_and_classes; for (int i = 0; i < service->method_count(); ++i) { const MethodDescriptor* method = service->method(i); const grpc::string method_cardinality = @@ -433,7 +438,7 @@ bool PrivateGenerator::PrintBetaStubFactory( make_pair(method->name(), output_message_module_and_class)); } out->Print("request_serializers = {\n"); - for (auto name_and_input_module_class_pair = + for (StringMap::iterator name_and_input_module_class_pair = input_message_modules_and_classes.begin(); name_and_input_module_class_pair != input_message_modules_and_classes.end(); @@ -448,7 +453,7 @@ bool PrivateGenerator::PrintBetaStubFactory( } out->Print("}\n"); out->Print("response_deserializers = {\n"); - for (auto name_and_output_module_class_pair = + for (StringMap::iterator name_and_output_module_class_pair = output_message_modules_and_classes.begin(); name_and_output_module_class_pair != output_message_modules_and_classes.end(); @@ -464,7 +469,7 @@ bool PrivateGenerator::PrintBetaStubFactory( } out->Print("}\n"); out->Print("cardinalities = {\n"); - for (auto name_and_cardinality = method_cardinalities.begin(); + for (StringMap::iterator name_and_cardinality = method_cardinalities.begin(); name_and_cardinality != method_cardinalities.end(); name_and_cardinality++) { IndentScope raii_descriptions_indent(out); @@ -508,8 +513,8 @@ bool PrivateGenerator::PrintStub(const grpc::string& package_qualified_service_n } out->Print("\"\"\"\n"); for (int i = 0; i < service->method_count(); ++i) { - auto method = service->method(i); - auto multi_callable_constructor = + const MethodDescriptor *method = service->method(i); + grpc::string multi_callable_constructor = grpc::string(method->client_streaming() ? "stream" : "unary") + "_" + grpc::string(method->server_streaming() ? "stream" : "unary"); grpc::string request_module_and_class; @@ -552,7 +557,7 @@ bool PrivateGenerator::PrintServicer(const ServiceDescriptor* service) { IndentScope raii_class_indent(out); PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { - auto method = service->method(i); + const MethodDescriptor *method = service->method(i); grpc::string arg_name = method->client_streaming() ? "request_iterator" : "request"; out->Print("\n"); @@ -583,8 +588,8 @@ bool PrivateGenerator::PrintAddServicerToServer( IndentScope raii_dict_first_indent(out); IndentScope raii_dict_second_indent(out); for (int i = 0; i < service->method_count(); ++i) { - auto method = service->method(i); - auto method_handler_constructor = + const MethodDescriptor *method = service->method(i); + grpc::string method_handler_constructor = grpc::string(method->client_streaming() ? "stream" : "unary") + "_" + grpc::string(method->server_streaming() ? "stream" : "unary") + @@ -649,7 +654,7 @@ bool PrivateGenerator::PrintPreamble() { for (int i = 0; i < file->service_count(); ++i) { const ServiceDescriptor *service = file->service(i); for (int j = 0; j < service->method_count(); ++j) { - auto method = service->method(j); + const MethodDescriptor *method = service->method(j); const Descriptor *types[2] = {method->input_type(), method->output_type()}; for (int k = 0; k < 2; ++k) { const Descriptor *type = types[k]; From 7780cc018aaf5b01bdb8836903249308b9c3d142 Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Mon, 24 Oct 2016 15:30:05 -0700 Subject: [PATCH 4/4] Clang-format python_generator.cc --- src/compiler/python_generator.cc | 77 +++++++++++++++++--------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index 9480bcc97b9..febaf135b6f 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -122,14 +122,14 @@ grpc::string ModuleAlias(const grpc::string& filename) { // only ever used from a single thread. struct PrivateGenerator { const GeneratorConfiguration& config; - const FileDescriptor *file; + const FileDescriptor* file; bool generate_in_pb2_grpc; - Printer *out; + Printer* out; PrivateGenerator(const GeneratorConfiguration& config, - const FileDescriptor *file); + const FileDescriptor* file); std::pair GetGrpcServices(); @@ -140,19 +140,19 @@ struct PrivateGenerator { bool PrintBetaServices(); bool PrintAddServicerToServer( - const grpc::string& package_qualified_service_name, - const ServiceDescriptor* service); + const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service); bool PrintServicer(const ServiceDescriptor* service); bool PrintStub(const grpc::string& package_qualified_service_name, const ServiceDescriptor* service); bool PrintBetaServicer(const ServiceDescriptor* service); - bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, - const ServiceDescriptor* service); - bool PrintBetaStub(const ServiceDescriptor* service); - bool PrintBetaStubFactory( + bool PrintBetaServerFactory( const grpc::string& package_qualified_service_name, const ServiceDescriptor* service); + bool PrintBetaStub(const ServiceDescriptor* service); + bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service); // Get all comments (leading, leading_detached, trailing) and print them as a // docstring. Any leading space of a line will be removed, but the line @@ -160,14 +160,12 @@ struct PrivateGenerator { template void PrintAllComments(const DescriptorType* descriptor); - bool GetModuleAndMessagePath(const Descriptor* type, - grpc::string* out); + bool GetModuleAndMessagePath(const Descriptor* type, grpc::string* out); }; - PrivateGenerator::PrivateGenerator(const GeneratorConfiguration& config, - const FileDescriptor *file) - : config(config), file(file) {} + const FileDescriptor* file) + : config(config), file(file) {} bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, grpc::string* out) { @@ -192,8 +190,7 @@ bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, } grpc::string message_type; for (DescriptorVector::reverse_iterator path_iter = message_path.rbegin(); - path_iter != message_path.rend(); - ++path_iter) { + path_iter != message_path.rend(); ++path_iter) { message_type += (*path_iter)->name() + "."; } // no pop_back prior to C++11 @@ -205,8 +202,8 @@ bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, template void PrivateGenerator::PrintAllComments(const DescriptorType* descriptor) { StringVector comments; - grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING_DETACHED, - &comments); + grpc_generator::GetComment( + descriptor, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &comments); grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING, &comments); grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_TRAILING, @@ -215,7 +212,8 @@ void PrivateGenerator::PrintAllComments(const DescriptorType* descriptor) { return; } out->Print("\"\"\""); - for (StringVector::iterator it = comments.begin(); it != comments.end(); ++it) { + for (StringVector::iterator it = comments.begin(); it != comments.end(); + ++it) { size_t start_pos = it->find_first_not_of(' '); if (start_pos != grpc::string::npos) { out->Print(it->c_str() + start_pos); @@ -240,7 +238,7 @@ bool PrivateGenerator::PrintBetaServicer(const ServiceDescriptor* service) { "grpcio>=0.15.0.\"\"\"\n"); PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { - const MethodDescriptor *method = service->method(i); + const MethodDescriptor* method = service->method(i); grpc::string arg_name = method->client_streaming() ? "request_iterator" : "request"; out->Print("def $Method$(self, $ArgName$, context):\n", "Method", @@ -469,7 +467,8 @@ bool PrivateGenerator::PrintBetaStubFactory( } out->Print("}\n"); out->Print("cardinalities = {\n"); - for (StringMap::iterator name_and_cardinality = method_cardinalities.begin(); + for (StringMap::iterator name_and_cardinality = + method_cardinalities.begin(); name_and_cardinality != method_cardinalities.end(); name_and_cardinality++) { IndentScope raii_descriptions_indent(out); @@ -493,8 +492,9 @@ bool PrivateGenerator::PrintBetaStubFactory( return true; } -bool PrivateGenerator::PrintStub(const grpc::string& package_qualified_service_name, - const ServiceDescriptor* service) { +bool PrivateGenerator::PrintStub( + const grpc::string& package_qualified_service_name, + const ServiceDescriptor* service) { out->Print("\n\n"); out->Print("class $Service$Stub(object):\n", "Service", service->name()); { @@ -513,7 +513,7 @@ bool PrivateGenerator::PrintStub(const grpc::string& package_qualified_service_n } out->Print("\"\"\"\n"); for (int i = 0; i < service->method_count(); ++i) { - const MethodDescriptor *method = service->method(i); + const MethodDescriptor* method = service->method(i); grpc::string multi_callable_constructor = grpc::string(method->client_streaming() ? "stream" : "unary") + "_" + grpc::string(method->server_streaming() ? "stream" : "unary"); @@ -557,7 +557,7 @@ bool PrivateGenerator::PrintServicer(const ServiceDescriptor* service) { IndentScope raii_class_indent(out); PrintAllComments(service); for (int i = 0; i < service->method_count(); ++i) { - const MethodDescriptor *method = service->method(i); + const MethodDescriptor* method = service->method(i); grpc::string arg_name = method->client_streaming() ? "request_iterator" : "request"; out->Print("\n"); @@ -588,7 +588,7 @@ bool PrivateGenerator::PrintAddServicerToServer( IndentScope raii_dict_first_indent(out); IndentScope raii_dict_second_indent(out); for (int i = 0; i < service->method_count(); ++i) { - const MethodDescriptor *method = service->method(i); + const MethodDescriptor* method = service->method(i); grpc::string method_handler_constructor = grpc::string(method->client_streaming() ? "stream" : "unary") + "_" + @@ -652,16 +652,18 @@ bool PrivateGenerator::PrintPreamble() { if (generate_in_pb2_grpc) { out->Print("\n"); for (int i = 0; i < file->service_count(); ++i) { - const ServiceDescriptor *service = file->service(i); + const ServiceDescriptor* service = file->service(i); for (int j = 0; j < service->method_count(); ++j) { - const MethodDescriptor *method = service->method(j); - const Descriptor *types[2] = {method->input_type(), method->output_type()}; + const MethodDescriptor* method = service->method(j); + const Descriptor* types[2] = {method->input_type(), + method->output_type()}; for (int k = 0; k < 2; ++k) { - const Descriptor *type = types[k]; + const Descriptor* type = types[k]; grpc::string type_file_name = type->file()->name(); grpc::string module_name = ModuleName(type_file_name); grpc::string module_alias = ModuleAlias(type_file_name); - out->Print("import $ModuleName$ as $ModuleAlias$\n", "ModuleName", module_name, "ModuleAlias", module_alias); + out->Print("import $ModuleName$ as $ModuleAlias$\n", "ModuleName", + module_name, "ModuleAlias", module_alias); } } } @@ -675,7 +677,7 @@ bool PrivateGenerator::PrintGAServices() { package = package.append("."); } for (int i = 0; i < file->service_count(); ++i) { - const ServiceDescriptor *service = file->service(i); + const ServiceDescriptor* service = file->service(i); grpc::string package_qualified_service_name = package + service->name(); if (!(PrintStub(package_qualified_service_name, service) && PrintServicer(service) && @@ -692,7 +694,7 @@ bool PrivateGenerator::PrintBetaServices() { package = package.append("."); } for (int i = 0; i < file->service_count(); ++i) { - const ServiceDescriptor *service = file->service(i); + const ServiceDescriptor* service = file->service(i); grpc::string package_qualified_service_name = package + service->name(); if (!(PrintBetaServicer(service) && PrintBetaStub(service) && PrintBetaServerFactory(package_qualified_service_name, service) && @@ -722,8 +724,9 @@ pair PrivateGenerator::GetGrpcServices() { out->Print("try:\n"); { IndentScope raii_dict_try_indent(out); - out->Print("# THESE ELEMENTS WILL BE DEPRECATED.\n" - "# Please use the generated *_pb2_grpc.py files instead.\n"); + out->Print( + "# THESE ELEMENTS WILL BE DEPRECATED.\n" + "# Please use the generated *_pb2_grpc.py files instead.\n"); if (!PrintPreamble()) { return make_pair(false, ""); } @@ -767,8 +770,8 @@ bool PythonGrpcGenerator::Generate(const FileDescriptor* file, static const int proto_suffix_length = strlen(".proto"); if (file->name().size() > static_cast(proto_suffix_length) && file->name().find_last_of(".proto") == file->name().size() - 1) { - grpc::string base = file->name().substr( - 0, file->name().size() - proto_suffix_length); + grpc::string base = + file->name().substr(0, file->name().size() - proto_suffix_length); pb2_file_name = base + "_pb2.py"; pb2_grpc_file_name = base + "_pb2_grpc.py"; } else {