OLD | NEW |
(Empty) | |
| 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 |
| 34 import sysconfig |
| 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() |
| 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) |
| 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): |
| 94 url = '{}/{}.so'.format(BINARIES_REPOSITORY, _tagged_ext_name('cygrpc')) |
| 95 target_path = os.path.join(PYTHON_STEM, 'grpc/_cython/cygrpc.so') |
| 96 try: |
| 97 extension = urlopen(url).read() |
| 98 except: |
| 99 sys.stderr.write( |
| 100 'could not download precompiled extension: {}\n'.format(url)) |
| 101 return |
| 102 try: |
| 103 with open(target_path, 'w') as target: |
| 104 target.write(extension) |
| 105 setup_arguments['ext_modules'] = [] |
| 106 except: |
| 107 sys.stderr.write( |
| 108 'could not write precompiled extension to directory: {} -> {}\n' |
| 109 .format(url, target_path)) |
| 110 return |
| 111 setup_arguments['package_data']['grpc._cython'].append('cygrpc.so') |
OLD | NEW |