1# Copyright 2016 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import os 16import pkg_resources 17import sys 18 19import setuptools 20 21from grpc_tools import protoc 22 23 24def build_package_protos(package_root): 25 proto_files = [] 26 inclusion_root = os.path.abspath(package_root) 27 for root, _, files in os.walk(inclusion_root): 28 for filename in files: 29 if filename.endswith('.proto'): 30 proto_files.append( 31 os.path.abspath(os.path.join(root, filename))) 32 33 well_known_protos_include = pkg_resources.resource_filename( 34 'grpc_tools', '_proto') 35 36 for proto_file in proto_files: 37 command = [ 38 'grpc_tools.protoc', 39 '--proto_path={}'.format(inclusion_root), 40 '--proto_path={}'.format(well_known_protos_include), 41 '--python_out={}'.format(inclusion_root), 42 '--grpc_python_out={}'.format(inclusion_root), 43 ] + [proto_file] 44 if protoc.main(command) != 0: 45 sys.stderr.write('warning: {} failed'.format(command)) 46 47 48class BuildPackageProtos(setuptools.Command): 49 """Command to generate project *_pb2.py modules from proto files.""" 50 51 description = 'build grpc protobuf modules' 52 user_options = [] 53 54 def initialize_options(self): 55 pass 56 57 def finalize_options(self): 58 pass 59 60 def run(self): 61 # due to limitations of the proto generator, we require that only *one* 62 # directory is provided as an 'include' directory. We assume it's the '' key 63 # to `self.distribution.package_dir` (and get a key error if it's not 64 # there). 65 build_package_protos(self.distribution.package_dir['']) 66