Add a simple unified importer

pull/21504/head
Richard Belleville 5 years ago
parent 08a4bbf8b6
commit 0cfe955068
  1. 3
      tools/distrib/python/grpcio_tools/grpc_tools/BUILD
  2. 31
      tools/distrib/python/grpcio_tools/grpc_tools/__init__.py
  3. 1
      tools/distrib/python/grpcio_tools/grpc_tools/protoc.py
  4. 15
      tools/distrib/python/grpcio_tools/grpc_tools/protoc_test.py
  5. 21
      tools/distrib/python/grpcio_tools/grpc_tools/simple.proto

@ -3,6 +3,8 @@ package(default_visibility = ["//visibility:public"])
load("//bazel:cython_library.bzl", "pyx_library")
# TODO: Move this build file up a directory to ensure that this
# points to '.', not a directory above the package root.
NON_BAZEL_ROOT = "../"
cc_library(
@ -38,5 +40,6 @@ py_test(
name = "protoc_test",
srcs = ["protoc_test.py"],
deps = ["//tools/distrib/python/grpcio_tools/grpc_tools:grpc_tools"],
data = ["simple.proto"],
python_version = "PY3",
)

@ -11,3 +11,34 @@
# 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 importlib
import os
from .protoc import main
# TODO: Get this thing to just give me the code via an FD.
# TODO: Figure out what to do about STDOUT pollution.
def import_protos(proto_path, project_root):
proto_basename = os.path.basename(proto_path)
proto_name, _ = os.path.splitext(proto_basename)
anchor_package = ".".join(os.path.normpath(os.path.dirname(proto_path)).split(os.sep))
original_dir = os.getcwd()
try:
os.chdir(os.path.join(original_dir, project_root))
return_value = protoc.main([
"grpc_tools.protoc",
"--proto_path=.",
"--python_out=.",
"--grpc_python_out=.",
proto_path
])
finally:
os.chdir(original_dir)
if return_value != 0:
raise RuntimeError("Protoc failed.")
print("anchor_package: {}".format(anchor_package))
protos = importlib.import_module("{}.{}_pb2".format(anchor_package, proto_name))
services = importlib.import_module("{}.{}_pb2_grpc".format(anchor_package, proto_name))
return protos, services

@ -19,7 +19,6 @@ import sys
from grpc_tools import _protoc_compiler
def main(command_arguments):
"""Run the protocol buffer compiler with the given command-line arguments.

@ -4,15 +4,24 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import importlib
import unittest
import grpc_tools
from grpc_tools import protoc
import os
class ProtocTest(unittest.TestCase):
def test_protoc(self):
pass
def test_protoc(self):
# TODO: Get this thing to just give me the code via an FD.
# TODO: Figure out what to do about STDOUT pollution.
# TODO: How do we convert protoc failure into a Python error?
protos, services = grpc_tools.import_protos("grpc_tools/simple.proto", "tools/distrib/python/grpcio_tools/")
print(dir(protos))
print(dir(services))
if __name__ == '__main__':

@ -0,0 +1,21 @@
syntax = "proto3";
message SimpleMessage {
string msg = 1;
oneof personal_or_business {
bool personal = 2;
bool business = 3;
};
};
message SimpleMessageRequest {
SimpleMessage simple_msg = 1;
};
message SimpleMessageResponse {
bool understood = 1;
};
service SimpleMessageService {
rpc Tell(SimpleMessageRequest) returns (SimpleMessageResponse);
};
Loading…
Cancel
Save