Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 1 | # Copyright 2015, 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 | 1d17781 | 2016-01-12 09:21:57 -0800 | [diff] [blame] | 49 | |
Masood Malekghassemi | 5fec8b3 | 2016-01-25 16:16:50 -0800 | [diff] [blame] | 50 | import support |
| 51 | |
Masood Malekghassemi | 116982e | 2015-12-11 15:53:38 -0800 | [diff] [blame] | 52 | PYTHON_STEM = os.path.dirname(os.path.abspath(__file__)) |
Ken Payson | 707c9e2 | 2016-04-20 09:42:19 -0700 | [diff] [blame] | 53 | GRPC_STEM = os.path.abspath(PYTHON_STEM + '../../../../') |
| 54 | PROTO_STEM = os.path.join(GRPC_STEM, 'src', 'proto') |
| 55 | PROTO_GEN_STEM = os.path.join(GRPC_STEM, 'src', 'python', 'gens') |
Ken Payson | 7722245 | 2016-09-11 22:06:05 -0700 | [diff] [blame] | 56 | CYTHON_STEM = os.path.join(PYTHON_STEM, 'grpc', '_cython') |
Masood Malekghassemi | 116982e | 2015-12-11 15:53:38 -0800 | [diff] [blame] | 57 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 58 | CONF_PY_ADDENDUM = """ |
Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 59 | extensions.append('sphinx.ext.napoleon') |
| 60 | napoleon_google_docstring = True |
| 61 | napoleon_numpy_docstring = True |
Masood Malekghassemi | 48d07c6 | 2016-07-12 15:47:05 -0700 | [diff] [blame] | 62 | napoleon_include_special_with_doc = True |
Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 63 | |
| 64 | html_theme = 'sphinx_rtd_theme' |
Masood Malekghassemi | 25186ae | 2016-12-27 09:05:13 -0800 | [diff] [blame] | 65 | copyright = "2016, The gRPC Authors" |
Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 66 | """ |
| 67 | |
Ken Payson | f7f47a6 | 2016-07-11 11:09:27 -0700 | [diff] [blame] | 68 | API_GLOSSARY = """ |
| 69 | |
| 70 | Glossary |
| 71 | ================ |
| 72 | |
| 73 | .. glossary:: |
| 74 | |
| 75 | metadatum |
| 76 | A key-value pair included in the HTTP header. It is a |
| 77 | 2-tuple where the first entry is the key and the |
| 78 | second is the value, i.e. (key, value). The metadata key is an ASCII str, |
| 79 | and must be a valid HTTP header name. The metadata value can be |
| 80 | either a valid HTTP ASCII str, or bytes. If bytes are provided, |
| 81 | the key must end with '-bin', i.e. |
| 82 | ``('binary-metadata-bin', b'\\x00\\xFF')`` |
| 83 | |
| 84 | metadata |
| 85 | A sequence of metadatum. |
| 86 | """ |
| 87 | |
Masood Malekghassemi | fe8dc88 | 2015-07-27 15:30:33 -0700 | [diff] [blame] | 88 | |
Masood Malekghassemi | 59994bc | 2016-01-12 08:49:26 -0800 | [diff] [blame] | 89 | class CommandError(Exception): |
| 90 | """Simple exception class for GRPC custom commands.""" |
| 91 | |
| 92 | |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 93 | # TODO(atash): Remove this once PyPI has better Linux bdist support. See |
| 94 | # 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] | 95 | def _get_grpc_custom_bdist(decorated_basename, target_bdist_basename): |
| 96 | """Returns a string path to a bdist file for Linux to install. |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 97 | |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 98 | 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] | 99 | warning and builds from source. |
| 100 | """ |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 101 | # TODO(atash): somehow the name that's returned from `wheel` is different |
| 102 | # between different versions of 'wheel' (but from a compatibility standpoint, |
| 103 | # the names are compatible); we should have some way of determining name |
| 104 | # compatibility in the same way `wheel` does to avoid having to rename all of |
| 105 | # the custom wheels that we build/upload to GCS. |
| 106 | |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 107 | # 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] | 108 | # relevant package. |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 109 | from six.moves.urllib import request |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 110 | decorated_path = decorated_basename + GRPC_CUSTOM_BDIST_EXT |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 111 | try: |
Masood Malekghassemi | f751b0b | 2016-02-04 11:34:53 -0800 | [diff] [blame] | 112 | url = BINARIES_REPOSITORY + '/{target}'.format(target=decorated_path) |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 113 | bdist_data = request.urlopen(url).read() |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 114 | except IOError as error: |
| 115 | raise CommandError( |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 116 | '{}\n\nCould not find the bdist {}: {}' |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 117 | .format(traceback.format_exc(), decorated_path, error.message)) |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 118 | # Our chosen local bdist path. |
| 119 | bdist_path = target_bdist_basename + GRPC_CUSTOM_BDIST_EXT |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 120 | try: |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 121 | with open(bdist_path, 'w') as bdist_file: |
| 122 | bdist_file.write(bdist_data) |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 123 | except IOError as error: |
| 124 | raise CommandError( |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 125 | '{}\n\nCould not write grpcio bdist: {}' |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 126 | .format(traceback.format_exc(), error.message)) |
Masood Malekghassemi | 334e9e6 | 2016-02-10 20:12:59 -0800 | [diff] [blame] | 127 | return bdist_path |
Masood Malekghassemi | 154b0ee | 2016-01-25 16:45:29 -0800 | [diff] [blame] | 128 | |
| 129 | |
Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 130 | class SphinxDocumentation(setuptools.Command): |
| 131 | """Command to generate documentation via sphinx.""" |
| 132 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 133 | description = 'generate sphinx documentation' |
Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 134 | user_options = [] |
| 135 | |
| 136 | def initialize_options(self): |
| 137 | pass |
| 138 | |
| 139 | def finalize_options(self): |
| 140 | pass |
| 141 | |
| 142 | def run(self): |
| 143 | # We import here to ensure that setup.py has had a chance to install the |
| 144 | # relevant package eggs first. |
| 145 | import sphinx |
| 146 | import sphinx.apidoc |
| 147 | metadata = self.distribution.metadata |
Masood Malekghassemi | 3ee1f9b | 2016-02-22 18:37:18 -0800 | [diff] [blame] | 148 | src_dir = os.path.join(PYTHON_STEM, 'grpc') |
Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 149 | sys.path.append(src_dir) |
| 150 | sphinx.apidoc.main([ |
| 151 | '', '--force', '--full', '-H', metadata.name, '-A', metadata.author, |
| 152 | '-V', metadata.version, '-R', metadata.version, |
| 153 | '-o', os.path.join('doc', 'src'), src_dir]) |
| 154 | conf_filepath = os.path.join('doc', 'src', 'conf.py') |
| 155 | with open(conf_filepath, 'a') as conf_file: |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 156 | conf_file.write(CONF_PY_ADDENDUM) |
Ken Payson | f7f47a6 | 2016-07-11 11:09:27 -0700 | [diff] [blame] | 157 | glossary_filepath = os.path.join('doc', 'src', 'grpc.rst') |
| 158 | with open(glossary_filepath, 'a') as glossary_filepath: |
| 159 | glossary_filepath.write(API_GLOSSARY) |
Masood Malekghassemi | d65632a | 2015-07-27 14:30:09 -0700 | [diff] [blame] | 160 | sphinx.main(['', os.path.join('doc', 'src'), os.path.join('doc', 'build')]) |
| 161 | |
Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 162 | |
| 163 | class BuildProjectMetadata(setuptools.Command): |
| 164 | """Command to generate project metadata in a module.""" |
| 165 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 166 | description = 'build grpcio project metadata files' |
Masood Malekghassemi | 5c14763 | 2015-07-31 14:08:19 -0700 | [diff] [blame] | 167 | user_options = [] |
| 168 | |
| 169 | def initialize_options(self): |
| 170 | pass |
| 171 | |
| 172 | def finalize_options(self): |
| 173 | pass |
| 174 | |
| 175 | def run(self): |
Masood Malekghassemi | 116982e | 2015-12-11 15:53:38 -0800 | [diff] [blame] | 176 | 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] | 177 | module_file.write('__version__ = """{}"""'.format( |
| 178 | self.distribution.get_version())) |
| 179 | |
| 180 | |
| 181 | class BuildPy(build_py.build_py): |
| 182 | """Custom project build command.""" |
| 183 | |
| 184 | def run(self): |
| 185 | self.run_command('build_project_metadata') |
| 186 | build_py.build_py.run(self) |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 187 | |
| 188 | |
Masood Malekghassemi | fd9cc10 | 2016-07-21 14:20:43 -0700 | [diff] [blame] | 189 | def _poison_extensions(extensions, message): |
| 190 | """Includes a file that will always fail to compile in all extensions.""" |
| 191 | poison_filename = os.path.join(PYTHON_STEM, 'poison.c') |
| 192 | with open(poison_filename, 'w') as poison: |
| 193 | poison.write('#error {}'.format(message)) |
| 194 | for extension in extensions: |
| 195 | extension.sources = [poison_filename] |
| 196 | |
| 197 | def check_and_update_cythonization(extensions): |
| 198 | """Replace .pyx files with their generated counterparts and return whether or |
| 199 | not cythonization still needs to occur.""" |
| 200 | for extension in extensions: |
| 201 | generated_pyx_sources = [] |
| 202 | other_sources = [] |
| 203 | for source in extension.sources: |
| 204 | base, file_ext = os.path.splitext(source) |
| 205 | if file_ext == '.pyx': |
| 206 | generated_pyx_source = next( |
| 207 | (base + gen_ext for gen_ext in ('.c', '.cpp',) |
| 208 | if os.path.isfile(base + gen_ext)), None) |
| 209 | if generated_pyx_source: |
| 210 | generated_pyx_sources.append(generated_pyx_source) |
| 211 | else: |
| 212 | sys.stderr.write('Cython-generated files are missing...\n') |
| 213 | return False |
| 214 | else: |
| 215 | other_sources.append(source) |
| 216 | extension.sources = generated_pyx_sources + other_sources |
| 217 | sys.stderr.write('Found cython-generated files...\n') |
| 218 | return True |
| 219 | |
| 220 | def try_cythonize(extensions, linetracing=False, mandatory=True): |
| 221 | """Attempt to cythonize the extensions. |
| 222 | |
| 223 | Args: |
| 224 | extensions: A list of `distutils.extension.Extension`. |
| 225 | linetracing: A bool indicating whether or not to enable linetracing. |
| 226 | mandatory: Whether or not having Cython-generated files is mandatory. If it |
| 227 | is, extensions will be poisoned when they can't be fully generated. |
| 228 | """ |
| 229 | try: |
| 230 | # Break import style to ensure we have access to Cython post-setup_requires |
| 231 | import Cython.Build |
| 232 | except ImportError: |
| 233 | if mandatory: |
| 234 | sys.stderr.write( |
| 235 | "This package needs to generate C files with Cython but it cannot. " |
| 236 | "Poisoning extension sources to disallow extension commands...") |
| 237 | _poison_extensions( |
| 238 | extensions, |
| 239 | "Extensions have been poisoned due to missing Cython-generated code.") |
| 240 | return extensions |
| 241 | cython_compiler_directives = {} |
| 242 | if linetracing: |
| 243 | additional_define_macros = [('CYTHON_TRACE_NOGIL', '1')] |
| 244 | cython_compiler_directives['linetrace'] = True |
| 245 | return Cython.Build.cythonize( |
| 246 | extensions, |
| 247 | include_path=[ |
| 248 | include_dir for extension in extensions for include_dir in extension.include_dirs |
Ken Payson | 7722245 | 2016-09-11 22:06:05 -0700 | [diff] [blame] | 249 | ] + [CYTHON_STEM], |
Masood Malekghassemi | fd9cc10 | 2016-07-21 14:20:43 -0700 | [diff] [blame] | 250 | compiler_directives=cython_compiler_directives |
| 251 | ) |
| 252 | |
| 253 | |
Masood Malekghassemi | 14a0a93 | 2016-01-21 20:13:22 -0800 | [diff] [blame] | 254 | class BuildExt(build_ext.build_ext): |
Masood Malekghassemi | 1d17781 | 2016-01-12 09:21:57 -0800 | [diff] [blame] | 255 | """Custom build_ext command to enable compiler-specific flags.""" |
| 256 | |
| 257 | C_OPTIONS = { |
| 258 | 'unix': ('-pthread', '-std=gnu99'), |
| 259 | 'msvc': (), |
| 260 | } |
| 261 | LINK_OPTIONS = {} |
| 262 | |
| 263 | def build_extensions(self): |
| 264 | compiler = self.compiler.compiler_type |
| 265 | if compiler in BuildExt.C_OPTIONS: |
| 266 | for extension in self.extensions: |
| 267 | extension.extra_compile_args += list(BuildExt.C_OPTIONS[compiler]) |
| 268 | if compiler in BuildExt.LINK_OPTIONS: |
| 269 | for extension in self.extensions: |
| 270 | extension.extra_link_args += list(BuildExt.LINK_OPTIONS[compiler]) |
Masood Malekghassemi | fd9cc10 | 2016-07-21 14:20:43 -0700 | [diff] [blame] | 271 | if not check_and_update_cythonization(self.extensions): |
| 272 | self.extensions = try_cythonize(self.extensions) |
Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 273 | try: |
| 274 | build_ext.build_ext.build_extensions(self) |
Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 275 | except Exception as error: |
Masood Malekghassemi | 5080909 | 2016-01-30 14:26:24 -0800 | [diff] [blame] | 276 | formatted_exception = traceback.format_exc() |
| 277 | support.diagnose_build_ext_error(self, error, formatted_exception) |
| 278 | raise CommandError( |
| 279 | "Failed `build_ext` step:\n{}".format(formatted_exception)) |
Masood Malekghassemi | 1d17781 | 2016-01-12 09:21:57 -0800 | [diff] [blame] | 280 | |
| 281 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 282 | class Gather(setuptools.Command): |
| 283 | """Command to gather project dependencies.""" |
| 284 | |
| 285 | description = 'gather dependencies for grpcio' |
| 286 | user_options = [ |
| 287 | ('test', 't', 'flag indicating to gather test dependencies'), |
| 288 | ('install', 'i', 'flag indicating to gather install dependencies') |
| 289 | ] |
| 290 | |
| 291 | def initialize_options(self): |
| 292 | self.test = False |
| 293 | self.install = False |
| 294 | |
| 295 | def finalize_options(self): |
| 296 | # distutils requires this override. |
| 297 | pass |
| 298 | |
| 299 | def run(self): |
| 300 | if self.install and self.distribution.install_requires: |
| 301 | self.distribution.fetch_build_eggs(self.distribution.install_requires) |
| 302 | if self.test and self.distribution.tests_require: |
| 303 | self.distribution.fetch_build_eggs(self.distribution.tests_require) |