Masood Malekghassemi | f747409 | 2016-02-12 12:04:53 -0800 | [diff] [blame] | 1 | # Copyright 2015-2016, Google Inc. |
| 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 | import os |
| 31 | import platform |
| 32 | import shutil |
| 33 | import sys |
Masood Malekghassemi | e5e0131 | 2016-02-17 13:06:01 -0800 | [diff] [blame] | 34 | import sysconfig |
Masood Malekghassemi | f747409 | 2016-02-12 12:04:53 -0800 | [diff] [blame] | 35 | |
| 36 | import setuptools |
| 37 | |
| 38 | import commands |
| 39 | import grpc_version |
| 40 | |
| 41 | try: |
| 42 | from urllib2 import urlopen |
| 43 | except ImportError: |
| 44 | from urllib.request import urlopen |
| 45 | |
| 46 | PYTHON_STEM = os.path.dirname(os.path.abspath(__file__)) |
| 47 | BINARIES_REPOSITORY = os.environ.get( |
| 48 | 'GRPC_PYTHON_BINARIES_REPOSITORY', |
| 49 | 'https://storage.googleapis.com/grpc-precompiled-binaries/python') |
| 50 | USE_PRECOMPILED_BINARIES = bool(int(os.environ.get( |
| 51 | 'GRPC_PYTHON_USE_PRECOMPILED_BINARIES', '1'))) |
| 52 | |
| 53 | def _tagged_ext_name(base): |
| 54 | uname = platform.uname() |
Masood Malekghassemi | e5e0131 | 2016-02-17 13:06:01 -0800 | [diff] [blame] | 55 | tags = ( |
| 56 | grpc_version.VERSION, |
| 57 | 'py{}'.format(sysconfig.get_python_version()), |
| 58 | uname[0], |
| 59 | uname[4], |
| 60 | ) |
| 61 | ucs = 'ucs{}'.format(sysconfig.get_config_var('Py_UNICODE_SIZE')) |
| 62 | return '{base}-{tags}-{ucs}'.format( |
| 63 | base=base, tags='-'.join(tags), ucs=ucs) |
Masood Malekghassemi | f747409 | 2016-02-12 12:04:53 -0800 | [diff] [blame] | 64 | |
| 65 | |
| 66 | class BuildTaggedExt(setuptools.Command): |
| 67 | |
| 68 | description = 'build the gRPC tagged extensions' |
| 69 | user_options = [] |
| 70 | |
| 71 | def initialize_options(self): |
| 72 | # distutils requires this override. |
| 73 | pass |
| 74 | |
| 75 | def finalize_options(self): |
| 76 | # distutils requires this override. |
| 77 | pass |
| 78 | |
| 79 | def run(self): |
| 80 | if 'linux' in sys.platform: |
| 81 | self.run_command('build_ext') |
| 82 | try: |
| 83 | os.makedirs('dist/') |
| 84 | except OSError: |
| 85 | pass |
| 86 | shutil.copyfile( |
| 87 | os.path.join(PYTHON_STEM, 'grpc/_cython/cygrpc.so'), |
| 88 | 'dist/{}.so'.format(_tagged_ext_name('cygrpc'))) |
| 89 | else: |
| 90 | sys.stderr.write('nothing to do for build_tagged_ext\n') |
| 91 | |
| 92 | |
| 93 | def update_setup_arguments(setup_arguments): |
Masood Malekghassemi | 7d8ac46 | 2016-03-15 12:15:13 -0700 | [diff] [blame^] | 94 | if not USE_PRECOMPILED_BINARIES: |
| 95 | sys.stderr.write('not using precompiled extension') |
| 96 | return |
Masood Malekghassemi | f747409 | 2016-02-12 12:04:53 -0800 | [diff] [blame] | 97 | url = '{}/{}.so'.format(BINARIES_REPOSITORY, _tagged_ext_name('cygrpc')) |
| 98 | target_path = os.path.join(PYTHON_STEM, 'grpc/_cython/cygrpc.so') |
| 99 | try: |
| 100 | extension = urlopen(url).read() |
| 101 | except: |
| 102 | sys.stderr.write( |
| 103 | 'could not download precompiled extension: {}\n'.format(url)) |
| 104 | return |
| 105 | try: |
| 106 | with open(target_path, 'w') as target: |
| 107 | target.write(extension) |
| 108 | setup_arguments['ext_modules'] = [] |
| 109 | except: |
| 110 | sys.stderr.write( |
| 111 | 'could not write precompiled extension to directory: {} -> {}\n' |
| 112 | .format(url, target_path)) |
Masood Malekghassemi | 7361734 | 2016-02-18 12:39:42 -0800 | [diff] [blame] | 113 | return |
| 114 | setup_arguments['package_data']['grpc._cython'].append('cygrpc.so') |