diff options
Diffstat (limited to 'grpc/tools/distrib/python/grpcio_tools/grpc_tools')
6 files changed, 199 insertions, 0 deletions
diff --git a/grpc/tools/distrib/python/grpcio_tools/grpc_tools/__init__.py b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/__init__.py new file mode 100644 index 00000000..5772620b --- /dev/null +++ b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2016 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/grpc/tools/distrib/python/grpcio_tools/grpc_tools/_protoc_compiler.pyx b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/_protoc_compiler.pyx new file mode 100644 index 00000000..481b5a4f --- /dev/null +++ b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/_protoc_compiler.pyx @@ -0,0 +1,24 @@ +# Copyright 2016 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from libc cimport stdlib + +cdef extern from "grpc_tools/main.h": + int protoc_main(int argc, char *argv[]) + +def run_main(list args not None): + cdef char **argv = <char **>stdlib.malloc(len(args)*sizeof(char *)) + for i in range(len(args)): + argv[i] = args[i] + return protoc_main(len(args), argv) diff --git a/grpc/tools/distrib/python/grpcio_tools/grpc_tools/command.py b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/command.py new file mode 100644 index 00000000..b1669db3 --- /dev/null +++ b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/command.py @@ -0,0 +1,70 @@ +# Copyright 2016 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import pkg_resources +import sys + +import setuptools + +from grpc_tools import protoc + + +def build_package_protos(package_root, strict_mode=False): + proto_files = [] + inclusion_root = os.path.abspath(package_root) + for root, _, files in os.walk(inclusion_root): + for filename in files: + if filename.endswith('.proto'): + proto_files.append(os.path.abspath(os.path.join(root, + filename))) + + well_known_protos_include = pkg_resources.resource_filename( + 'grpc_tools', '_proto') + + for proto_file in proto_files: + command = [ + 'grpc_tools.protoc', + '--proto_path={}'.format(inclusion_root), + '--proto_path={}'.format(well_known_protos_include), + '--python_out={}'.format(inclusion_root), + '--grpc_python_out={}'.format(inclusion_root), + ] + [proto_file] + if protoc.main(command) != 0: + if strict_mode: + raise Exception('error: {} failed'.format(command)) + else: + sys.stderr.write('warning: {} failed'.format(command)) + + +class BuildPackageProtos(setuptools.Command): + """Command to generate project *_pb2.py modules from proto files.""" + + description = 'build grpc protobuf modules' + user_options = [('strict-mode', 's', + 'exit with non-zero value if the proto compiling fails.')] + + def initialize_options(self): + self.strict_mode = False + + def finalize_options(self): + pass + + def run(self): + # due to limitations of the proto generator, we require that only *one* + # directory is provided as an 'include' directory. We assume it's the '' key + # to `self.distribution.package_dir` (and get a key error if it's not + # there). + build_package_protos(self.distribution.package_dir[''], + self.strict_mode) diff --git a/grpc/tools/distrib/python/grpcio_tools/grpc_tools/main.cc b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/main.cc new file mode 100644 index 00000000..65933865 --- /dev/null +++ b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/main.cc @@ -0,0 +1,38 @@ +// Copyright 2016 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include <google/protobuf/compiler/command_line_interface.h> +#include <google/protobuf/compiler/python/python_generator.h> + +#include "src/compiler/python_generator.h" + +#include "grpc_tools/main.h" + +int protoc_main(int argc, char* argv[]) { + google::protobuf::compiler::CommandLineInterface cli; + cli.AllowPlugins("protoc-"); + + // Proto2 Python + google::protobuf::compiler::python::Generator py_generator; + cli.RegisterGenerator("--python_out", &py_generator, + "Generate Python source file."); + + // gRPC Python + grpc_python_generator::GeneratorConfiguration grpc_py_config; + grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config); + cli.RegisterGenerator("--grpc_python_out", &grpc_py_generator, + "Generate Python source file."); + + return cli.Run(argc, argv); +} diff --git a/grpc/tools/distrib/python/grpcio_tools/grpc_tools/main.h b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/main.h new file mode 100644 index 00000000..9a1df202 --- /dev/null +++ b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/main.h @@ -0,0 +1,18 @@ +// Copyright 2016 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// We declare `protoc_main` here since we want access to it from Cython as an +// extern but *without* triggering a dllimport declspec when on Windows. +int protoc_main(int argc, char *argv[]); diff --git a/grpc/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py new file mode 100644 index 00000000..582cba0e --- /dev/null +++ b/grpc/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +# Copyright 2016 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pkg_resources +import sys + +from grpc_tools import _protoc_compiler + + +def main(command_arguments): + """Run the protocol buffer compiler with the given command-line arguments. + + Args: + command_arguments: a list of strings representing command line arguments to + `protoc`. + """ + command_arguments = [argument.encode() for argument in command_arguments] + return _protoc_compiler.run_main(command_arguments) + + +if __name__ == '__main__': + proto_include = pkg_resources.resource_filename('grpc_tools', '_proto') + sys.exit(main(sys.argv + ['-I{}'.format(proto_include)])) |