| murgatroid99 | 3466c4b | 2016-01-12 10:26:04 -0800 | [diff] [blame] | 1 | # Copyright 2015-2016, Google Inc. |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | """Provides distutils command classes for the GRPC Python setup process.""" |
| 31 | |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 32 | import distutils |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 33 | import glob |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 34 | import os |
| 35 | import os.path |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 36 | import platform |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 37 | import re |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 38 | import shutil |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 39 | import subprocess |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 40 | import sys |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 41 | import traceback |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 42 | |
| 43 | import setuptools |
| Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 44 | from setuptools.command import build_ext |
| Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 45 | from setuptools.command import build_py |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 46 | from setuptools.command import easy_install |
| 47 | from setuptools.command import install |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 48 | from setuptools.command import test |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 49 | from wheel import bdist_wheel |
| Masood Malekghassemi | 1d17781 | 2016-01-12 09:21:57 -0800 | [diff] [blame] | 50 | |
| Masood Malekghassemi | 5fec8b3 | 2016-01-25 16:16:50 -0800 | [diff] [blame] | 51 | import support |
| 52 | |
| Masood Malekghassemi | 116982e | 2015-12-11 15:53:38 -0800 | [diff] [blame] | 53 | PYTHON_STEM = os.path.dirname(os.path.abspath(__file__)) |
| 54 | |
| Masood Malekghassemi | f751b0b | 2016-02-04 11:34:53 -0800 | [diff] [blame] | 55 | BINARIES_REPOSITORY = os.environ.get( |
| 56 | 'GRPC_PYTHON_BINARIES_REPOSITORY', |
| Masood Malekghassemi | 064d37d | 2016-02-08 12:19:20 -0800 | [diff] [blame] | 57 | 'https://storage.googleapis.com/grpc-precompiled-binaries/python') |
| Masood Malekghassemi | f751b0b | 2016-02-04 11:34:53 -0800 | [diff] [blame] | 58 | |
| Masood Malekghassemi | 6598ce1 | 2016-02-08 13:31:21 -0800 | [diff] [blame] | 59 | USE_GRPC_CUSTOM_BDIST = bool(int(os.environ.get( |
| 60 | 'GRPC_PYTHON_USE_CUSTOM_BDIST', '1'))) |
| 61 | |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 62 | GRPC_CUSTOM_BDIST_EXT = '.whl' |
| 63 | |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 64 | CONF_PY_ADDENDUM = """ |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 65 | extensions.append('sphinx.ext.napoleon') |
| 66 | napoleon_google_docstring = True |
| 67 | napoleon_numpy_docstring = True |
| 68 | |
| 69 | html_theme = 'sphinx_rtd_theme' |
| 70 | """ |
| 71 | |
| Masood Malekghassemi | fe8dc88 | 2015-07-27 15:30:33 -0700 | [diff] [blame] | 72 | |
| Masood Malekghassemi | 59994bc | 2016-01-12 08:49:26 -0800 | [diff] [blame] | 73 | class CommandError(Exception): |
| 74 | """Simple exception class for GRPC custom commands.""" |
| 75 | |
| 76 | |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 77 | # TODO(atash): Remove this once PyPI has better Linux bdist support. See |
| 78 | # https://bitbucket.org/pypa/pypi/issues/120/binary-wheels-for-linux-are-not-supported |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 79 | def _get_grpc_custom_bdist(decorated_basename, target_bdist_basename): |
| 80 | """Returns a string path to a bdist file for Linux to install. |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 81 | |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 82 | If we can retrieve a pre-compiled bdist from online, uses it. Else, emits a |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 83 | warning and builds from source. |
| 84 | """ |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 85 | # TODO(atash): somehow the name that's returned from `wheel` is different |
| 86 | # between different versions of 'wheel' (but from a compatibility standpoint, |
| 87 | # the names are compatible); we should have some way of determining name |
| 88 | # compatibility in the same way `wheel` does to avoid having to rename all of |
| 89 | # the custom wheels that we build/upload to GCS. |
| 90 | |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 91 | # Break import style to ensure that setup.py has had a chance to install the |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 92 | # relevant package. |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 93 | from six.moves.urllib import request |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 94 | decorated_path = decorated_basename + GRPC_CUSTOM_BDIST_EXT |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 95 | try: |
| Masood Malekghassemi | f751b0b | 2016-02-04 11:34:53 -0800 | [diff] [blame] | 96 | url = BINARIES_REPOSITORY + '/{target}'.format(target=decorated_path) |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 97 | bdist_data = request.urlopen(url).read() |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 98 | except IOError as error: |
| 99 | raise CommandError( |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 100 | '{}\n\nCould not find the bdist {}: {}' |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 101 | .format(traceback.format_exc(), decorated_path, error.message)) |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 102 | # Our chosen local bdist path. |
| 103 | bdist_path = target_bdist_basename + GRPC_CUSTOM_BDIST_EXT |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 104 | try: |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 105 | with open(bdist_path, 'w') as bdist_file: |
| 106 | bdist_file.write(bdist_data) |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 107 | except IOError as error: |
| 108 | raise CommandError( |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 109 | '{}\n\nCould not write grpcio bdist: {}' |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 110 | .format(traceback.format_exc(), error.message)) |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 111 | return bdist_path |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 112 | |
| 113 | |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 114 | class WheelNameMixin(object): |
| 115 | """Mixin for setuptools.Command classes to enable acquiring the bdist name.""" |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 116 | |
| Masood Malekghassemi | dd6ee7a | 2016-02-11 20:08:49 -0800 | [diff] [blame^] | 117 | def wheel_custom_name(self): |
| 118 | base = self.wheel_name() |
| 119 | # Drop troublesome parts of the target tuple |
| 120 | base_split = base.split('-') |
| 121 | base = '-'.join(base_split[0:3] + base_split[4:]) |
| 122 | flavor = 'ucs2' if sys.maxunicode == 65535 else 'ucs4' |
| 123 | return '{base}-{flavor}'.format(base=base, flavor=flavor) |
| 124 | |
| 125 | def wheel_name(self): |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 126 | wheel_command = self.get_finalized_command('bdist_wheel') |
| Masood Malekghassemi | dd6ee7a | 2016-02-11 20:08:49 -0800 | [diff] [blame^] | 127 | return wheel_command.get_archive_basename() |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 128 | |
| 129 | |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 130 | class Install(install.install, WheelNameMixin): |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 131 | """Custom Install command for gRPC Python. |
| 132 | |
| 133 | This is for bdist shims and whatever else we might need a custom install |
| 134 | command for. |
| 135 | """ |
| 136 | |
| 137 | user_options = install.install.user_options + [ |
| Masood Malekghassemi | ece40b2 | 2016-01-27 15:45:35 -0800 | [diff] [blame] | 138 | # TODO(atash): remove this once PyPI has better Linux bdist support. See |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 139 | # https://bitbucket.org/pypa/pypi/issues/120/binary-wheels-for-linux-are-not-supported |
| Masood Malekghassemi | ece40b2 | 2016-01-27 15:45:35 -0800 | [diff] [blame] | 140 | ('use-grpc-custom-bdist', None, |
| 141 | 'Whether to retrieve a binary from the gRPC binary repository instead ' |
| 142 | 'of building from source.'), |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 143 | ] |
| 144 | |
| 145 | def initialize_options(self): |
| 146 | install.install.initialize_options(self) |
| Masood Malekghassemi | 6598ce1 | 2016-02-08 13:31:21 -0800 | [diff] [blame] | 147 | self.use_grpc_custom_bdist = USE_GRPC_CUSTOM_BDIST |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 148 | |
| 149 | def finalize_options(self): |
| 150 | install.install.finalize_options(self) |
| 151 | |
| 152 | def run(self): |
| Masood Malekghassemi | ece40b2 | 2016-01-27 15:45:35 -0800 | [diff] [blame] | 153 | if self.use_grpc_custom_bdist: |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 154 | try: |
| Masood Malekghassemi | 35afe4a | 2016-02-08 14:50:18 -0800 | [diff] [blame] | 155 | try: |
| Masood Malekghassemi | dd6ee7a | 2016-02-11 20:08:49 -0800 | [diff] [blame^] | 156 | bdist_path = _get_grpc_custom_bdist(self.wheel_custom_name(), |
| 157 | self.wheel_name()) |
| Masood Malekghassemi | 35afe4a | 2016-02-08 14:50:18 -0800 | [diff] [blame] | 158 | except CommandError as error: |
| 159 | sys.stderr.write( |
| 160 | '\nWARNING: Failed to acquire grpcio prebuilt binary:\n' |
| 161 | '{}.\n\n'.format(error.message)) |
| 162 | raise |
| 163 | try: |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 164 | self._run_bdist_retrieval_install(bdist_path) |
| Masood Malekghassemi | 35afe4a | 2016-02-08 14:50:18 -0800 | [diff] [blame] | 165 | except Exception as error: |
| 166 | # if anything else happens (and given how there's no way to really know |
| 167 | # what's happening in setuptools here, I mean *anything*), warn the user |
| 168 | # and fall back to building from source. |
| 169 | sys.stderr.write( |
| 170 | '{}\nWARNING: Failed to install grpcio prebuilt binary.\n\n' |
| 171 | .format(traceback.format_exc())) |
| 172 | raise |
| 173 | except Exception: |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 174 | install.install.run(self) |
| 175 | else: |
| 176 | install.install.run(self) |
| 177 | |
| 178 | # TODO(atash): Remove this once PyPI has better Linux bdist support. See |
| 179 | # https://bitbucket.org/pypa/pypi/issues/120/binary-wheels-for-linux-are-not-supported |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 180 | def _run_bdist_retrieval_install(self, bdist_path): |
| 181 | import pip |
| 182 | pip.main(['install', bdist_path]) |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 183 | |
| 184 | |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 185 | class BdistWheelCustomName(bdist_wheel.bdist_wheel, WheelNameMixin): |
| 186 | """Thin wrapper around the bdist command to build with our custom name.""" |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 187 | |
| Masood Malekghassemi | b6d3a82 | 2016-02-11 13:08:14 -0800 | [diff] [blame] | 188 | description = ("Create a gRPC custom-named wheel distribution. " |
| 189 | "Cannot be run with any other distribution-related command.") |
| 190 | |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 191 | def run(self): |
| Masood Malekghassemi | b6d3a82 | 2016-02-11 13:08:14 -0800 | [diff] [blame] | 192 | # TODO(atash): if the hack we use to support Linux binaries becomes |
| 193 | # 'supported' (i.e. |
| 194 | # https://bitbucket.org/pypa/pypi/issues/120/binary-wheels-for-linux-are-not-supported |
| 195 | # is not solved and we see users beginning to use this command, ill-advised |
| 196 | # as that may be) consider making the following capable of running with |
| 197 | # other distribution-related commands. Currently it depends on the (AFAIK |
| 198 | # undocumented, private) ordering of the distribution files. |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 199 | bdist_wheel.bdist_wheel.run(self) |
| 200 | output = self.distribution.dist_files[-1][2] |
| Masood Malekghassemi | dd6ee7a | 2016-02-11 20:08:49 -0800 | [diff] [blame^] | 201 | target = os.path.join( |
| 202 | self.dist_dir, '{}.whl'.format(self.wheel_custom_name())) |
| Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 203 | shutil.move(output, target) |
| Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 204 | |
| 205 | |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 206 | class SphinxDocumentation(setuptools.Command): |
| 207 | """Command to generate documentation via sphinx.""" |
| 208 | |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 209 | description = 'generate sphinx documentation' |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 210 | user_options = [] |
| 211 | |
| 212 | def initialize_options(self): |
| 213 | pass |
| 214 | |
| 215 | def finalize_options(self): |
| 216 | pass |
| 217 | |
| 218 | def run(self): |
| 219 | # We import here to ensure that setup.py has had a chance to install the |
| 220 | # relevant package eggs first. |
| 221 | import sphinx |
| 222 | import sphinx.apidoc |
| 223 | metadata = self.distribution.metadata |
| 224 | src_dir = os.path.join( |
| Masood Malekghassemi | 116982e | 2015-12-11 15:53:38 -0800 | [diff] [blame] | 225 | PYTHON_STEM, self.distribution.package_dir[''], 'grpc') |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 226 | sys.path.append(src_dir) |
| 227 | sphinx.apidoc.main([ |
| 228 | '', '--force', '--full', '-H', metadata.name, '-A', metadata.author, |
| 229 | '-V', metadata.version, '-R', metadata.version, |
| 230 | '-o', os.path.join('doc', 'src'), src_dir]) |
| 231 | conf_filepath = os.path.join('doc', 'src', 'conf.py') |
| 232 | with open(conf_filepath, 'a') as conf_file: |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 233 | conf_file.write(CONF_PY_ADDENDUM) |
| Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 234 | sphinx.main(['', os.path.join('doc', 'src'), os.path.join('doc', 'build')]) |
| 235 | |
| Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 236 | |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 237 | class BuildProtoModules(setuptools.Command): |
| 238 | """Command to generate project *_pb2.py modules from proto files.""" |
| 239 | |
| 240 | description = 'build protobuf modules' |
| 241 | user_options = [ |
| 242 | ('include=', None, 'path patterns to include in protobuf generation'), |
| 243 | ('exclude=', None, 'path patterns to exclude from protobuf generation') |
| 244 | ] |
| 245 | |
| 246 | def initialize_options(self): |
| 247 | self.exclude = None |
| 248 | self.include = r'.*\.proto$' |
| 249 | self.protoc_command = None |
| 250 | self.grpc_python_plugin_command = None |
| 251 | |
| 252 | def finalize_options(self): |
| 253 | self.protoc_command = distutils.spawn.find_executable('protoc') |
| 254 | self.grpc_python_plugin_command = distutils.spawn.find_executable( |
| 255 | 'grpc_python_plugin') |
| 256 | |
| 257 | def run(self): |
| Masood Malekghassemi | 0467295 | 2015-12-21 12:16:50 -0800 | [diff] [blame] | 258 | if not self.protoc_command: |
| Masood Malekghassemi | 59994bc | 2016-01-12 08:49:26 -0800 | [diff] [blame] | 259 | raise CommandError('could not find protoc') |
| Masood Malekghassemi | 0467295 | 2015-12-21 12:16:50 -0800 | [diff] [blame] | 260 | if not self.grpc_python_plugin_command: |
| Masood Malekghassemi | 59994bc | 2016-01-12 08:49:26 -0800 | [diff] [blame] | 261 | raise CommandError('could not find grpc_python_plugin ' |
| 262 | '(protoc plugin for GRPC Python)') |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 263 | include_regex = re.compile(self.include) |
| 264 | exclude_regex = re.compile(self.exclude) if self.exclude else None |
| 265 | paths = [] |
| Masood Malekghassemi | 116982e | 2015-12-11 15:53:38 -0800 | [diff] [blame] | 266 | root_directory = PYTHON_STEM |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 267 | for walk_root, directories, filenames in os.walk(root_directory): |
| 268 | for filename in filenames: |
| 269 | path = os.path.join(walk_root, filename) |
| 270 | if include_regex.match(path) and not ( |
| 271 | exclude_regex and exclude_regex.match(path)): |
| 272 | paths.append(path) |
| 273 | command = [ |
| 274 | self.protoc_command, |
| 275 | '--plugin=protoc-gen-python-grpc={}'.format( |
| 276 | self.grpc_python_plugin_command), |
| 277 | '-I {}'.format(root_directory), |
| 278 | '--python_out={}'.format(root_directory), |
| 279 | '--python-grpc_out={}'.format(root_directory), |
| 280 | ] + paths |
| 281 | try: |
| 282 | subprocess.check_output(' '.join(command), cwd=root_directory, shell=True, |
| 283 | stderr=subprocess.STDOUT) |
| 284 | except subprocess.CalledProcessError as e: |
| Masood Malekghassemi | 59994bc | 2016-01-12 08:49:26 -0800 | [diff] [blame] | 285 | raise CommandError('Command:\n{}\nMessage:\n{}\nOutput:\n{}'.format( |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 286 | command, e.message, e.output)) |
| 287 | |
| 288 | |
| Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 289 | class BuildProjectMetadata(setuptools.Command): |
| 290 | """Command to generate project metadata in a module.""" |
| 291 | |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 292 | description = 'build grpcio project metadata files' |
| Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 293 | user_options = [] |
| 294 | |
| 295 | def initialize_options(self): |
| 296 | pass |
| 297 | |
| 298 | def finalize_options(self): |
| 299 | pass |
| 300 | |
| 301 | def run(self): |
| Masood Malekghassemi | 116982e | 2015-12-11 15:53:38 -0800 | [diff] [blame] | 302 | with open(os.path.join(PYTHON_STEM, 'grpc/_grpcio_metadata.py'), 'w') as module_file: |
| Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 303 | module_file.write('__version__ = """{}"""'.format( |
| 304 | self.distribution.get_version())) |
| 305 | |
| 306 | |
| 307 | class BuildPy(build_py.build_py): |
| 308 | """Custom project build command.""" |
| 309 | |
| 310 | def run(self): |
| Masood Malekghassemi | 59994bc | 2016-01-12 08:49:26 -0800 | [diff] [blame] | 311 | try: |
| 312 | self.run_command('build_proto_modules') |
| 313 | except CommandError as error: |
| 314 | sys.stderr.write('warning: %s\n' % error.message) |
| Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 315 | self.run_command('build_project_metadata') |
| 316 | build_py.build_py.run(self) |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 317 | |
| 318 | |
| Masood Malekghassemi | 14a0a93 | 2016-01-21 20:13:22 -0800 | [diff] [blame] | 319 | class BuildExt(build_ext.build_ext): |
| Masood Malekghassemi | 1d17781 | 2016-01-12 09:21:57 -0800 | [diff] [blame] | 320 | """Custom build_ext command to enable compiler-specific flags.""" |
| 321 | |
| 322 | C_OPTIONS = { |
| 323 | 'unix': ('-pthread', '-std=gnu99'), |
| 324 | 'msvc': (), |
| 325 | } |
| 326 | LINK_OPTIONS = {} |
| 327 | |
| 328 | def build_extensions(self): |
| 329 | compiler = self.compiler.compiler_type |
| 330 | if compiler in BuildExt.C_OPTIONS: |
| 331 | for extension in self.extensions: |
| 332 | extension.extra_compile_args += list(BuildExt.C_OPTIONS[compiler]) |
| 333 | if compiler in BuildExt.LINK_OPTIONS: |
| 334 | for extension in self.extensions: |
| 335 | extension.extra_link_args += list(BuildExt.LINK_OPTIONS[compiler]) |
| Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 336 | try: |
| 337 | build_ext.build_ext.build_extensions(self) |
| Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 338 | except Exception as error: |
| Masood Malekghassemi | 5080909 | 2016-01-30 14:26:24 -0800 | [diff] [blame] | 339 | formatted_exception = traceback.format_exc() |
| 340 | support.diagnose_build_ext_error(self, error, formatted_exception) |
| 341 | raise CommandError( |
| 342 | "Failed `build_ext` step:\n{}".format(formatted_exception)) |
| Masood Malekghassemi | 1d17781 | 2016-01-12 09:21:57 -0800 | [diff] [blame] | 343 | |
| 344 | |
| Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 345 | class Gather(setuptools.Command): |
| 346 | """Command to gather project dependencies.""" |
| 347 | |
| 348 | description = 'gather dependencies for grpcio' |
| 349 | user_options = [ |
| 350 | ('test', 't', 'flag indicating to gather test dependencies'), |
| 351 | ('install', 'i', 'flag indicating to gather install dependencies') |
| 352 | ] |
| 353 | |
| 354 | def initialize_options(self): |
| 355 | self.test = False |
| 356 | self.install = False |
| 357 | |
| 358 | def finalize_options(self): |
| 359 | # distutils requires this override. |
| 360 | pass |
| 361 | |
| 362 | def run(self): |
| 363 | if self.install and self.distribution.install_requires: |
| 364 | self.distribution.fetch_build_eggs(self.distribution.install_requires) |
| 365 | if self.test and self.distribution.tests_require: |
| 366 | self.distribution.fetch_build_eggs(self.distribution.tests_require) |
| 367 | |
| 368 | |
| 369 | class RunInterop(test.test): |
| 370 | |
| 371 | description = 'run interop test client/server' |
| 372 | user_options = [ |
| 373 | ('args=', 'a', 'pass-thru arguments for the client/server'), |
| 374 | ('client', 'c', 'flag indicating to run the client'), |
| 375 | ('server', 's', 'flag indicating to run the server') |
| 376 | ] |
| 377 | |
| 378 | def initialize_options(self): |
| 379 | self.args = '' |
| 380 | self.client = False |
| 381 | self.server = False |
| 382 | |
| 383 | def finalize_options(self): |
| 384 | if self.client and self.server: |
| 385 | raise DistutilsOptionError('you may only specify one of client or server') |
| 386 | |
| 387 | def run(self): |
| 388 | if self.distribution.install_requires: |
| 389 | self.distribution.fetch_build_eggs(self.distribution.install_requires) |
| 390 | if self.distribution.tests_require: |
| 391 | self.distribution.fetch_build_eggs(self.distribution.tests_require) |
| 392 | if self.client: |
| 393 | self.run_client() |
| 394 | elif self.server: |
| 395 | self.run_server() |
| 396 | |
| 397 | def run_server(self): |
| 398 | # We import here to ensure that our setuptools parent has had a chance to |
| 399 | # edit the Python system path. |
| 400 | from tests.interop import server |
| 401 | sys.argv[1:] = self.args.split() |
| 402 | server.serve() |
| 403 | |
| 404 | def run_client(self): |
| 405 | # We import here to ensure that our setuptools parent has had a chance to |
| 406 | # edit the Python system path. |
| 407 | from tests.interop import client |
| 408 | sys.argv[1:] = self.args.split() |
| 409 | client.test_interoperability() |