command.py
Go to the documentation of this file.
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 
15 import os
16 import sys
17 
18 from grpc_tools import protoc
19 import pkg_resources
20 import setuptools
21 
22 
23 def build_package_protos(package_root, strict_mode=False):
24  proto_files = []
25  inclusion_root = os.path.abspath(package_root)
26  for root, _, files in os.walk(inclusion_root):
27  for filename in files:
28  if filename.endswith('.proto'):
29  proto_files.append(os.path.abspath(os.path.join(root,
30  filename)))
31 
32  well_known_protos_include = pkg_resources.resource_filename(
33  'grpc_tools', '_proto')
34 
35  for proto_file in proto_files:
36  command = [
37  'grpc_tools.protoc',
38  '--proto_path={}'.format(inclusion_root),
39  '--proto_path={}'.format(well_known_protos_include),
40  '--python_out={}'.format(inclusion_root),
41  '--grpc_python_out={}'.format(inclusion_root),
42  ] + [proto_file]
43  if protoc.main(command) != 0:
44  if strict_mode:
45  raise Exception('error: {} failed'.format(command))
46  else:
47  sys.stderr.write('warning: {} failed'.format(command))
48 
49 
50 class BuildPackageProtos(setuptools.Command):
51  """Command to generate project *_pb2.py modules from proto files."""
52 
53  description = 'build grpc protobuf modules'
54  user_options = [('strict-mode', 's',
55  'exit with non-zero value if the proto compiling fails.')]
56 
57  def initialize_options(self):
58  self.strict_mode = False
59 
60  def finalize_options(self):
61  pass
62 
63  def run(self):
64  # due to limitations of the proto generator, we require that only *one*
65  # directory is provided as an 'include' directory. We assume it's the '' key
66  # to `self.distribution.package_dir` (and get a key error if it's not
67  # there).
68  build_package_protos(self.distribution.package_dir[''],
69  self.strict_mode)
grpc_tools.command.BuildPackageProtos.strict_mode
strict_mode
Definition: command.py:58
http2_test_server.format
format
Definition: http2_test_server.py:118
grpc_tools.command.BuildPackageProtos.initialize_options
def initialize_options(self)
Definition: command.py:57
grpc_tools.command.BuildPackageProtos
Definition: command.py:50
grpc_tools.command.BuildPackageProtos.run
def run(self)
Definition: command.py:63
grpc_tools.command.build_package_protos
def build_package_protos(package_root, strict_mode=False)
Definition: command.py:23
grpc_tools.command.BuildPackageProtos.finalize_options
def finalize_options(self)
Definition: command.py:60


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:50