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 |
| 34 | |
| 35 | import setuptools |
| 36 | |
| 37 | import commands |
| 38 | import grpc_version |
| 39 | |
| 40 | try: |
| 41 | from urllib2 import urlopen |
| 42 | except ImportError: |
| 43 | from urllib.request import urlopen |
| 44 | |
| 45 | PYTHON_STEM = os.path.dirname(os.path.abspath(__file__)) |
| 46 | BINARIES_REPOSITORY = os.environ.get( |
| 47 | 'GRPC_PYTHON_BINARIES_REPOSITORY', |
| 48 | 'https://storage.googleapis.com/grpc-precompiled-binaries/python') |
| 49 | USE_PRECOMPILED_BINARIES = bool(int(os.environ.get( |
| 50 | 'GRPC_PYTHON_USE_PRECOMPILED_BINARIES', '1'))) |
| 51 | |
| 52 | def _tagged_ext_name(base): |
| 53 | uname = platform.uname() |
| 54 | tags = '-'.join((grpc_version.VERSION, uname[0], uname[4])) |
| 55 | flavor = 'ucs2' if sys.maxunicode == 65535 else 'ucs4' |
| 56 | return '{base}-{tags}-{flavor}'.format(base=base, tags=tags, flavor=flavor) |
| 57 | |
| 58 | |
| 59 | class BuildTaggedExt(setuptools.Command): |
| 60 | |
| 61 | description = 'build the gRPC tagged extensions' |
| 62 | user_options = [] |
| 63 | |
| 64 | def initialize_options(self): |
| 65 | # distutils requires this override. |
| 66 | pass |
| 67 | |
| 68 | def finalize_options(self): |
| 69 | # distutils requires this override. |
| 70 | pass |
| 71 | |
| 72 | def run(self): |
| 73 | if 'linux' in sys.platform: |
| 74 | self.run_command('build_ext') |
| 75 | try: |
| 76 | os.makedirs('dist/') |
| 77 | except OSError: |
| 78 | pass |
| 79 | shutil.copyfile( |
| 80 | os.path.join(PYTHON_STEM, 'grpc/_cython/cygrpc.so'), |
| 81 | 'dist/{}.so'.format(_tagged_ext_name('cygrpc'))) |
| 82 | else: |
| 83 | sys.stderr.write('nothing to do for build_tagged_ext\n') |
| 84 | |
| 85 | |
| 86 | def update_setup_arguments(setup_arguments): |
| 87 | url = '{}/{}.so'.format(BINARIES_REPOSITORY, _tagged_ext_name('cygrpc')) |
| 88 | target_path = os.path.join(PYTHON_STEM, 'grpc/_cython/cygrpc.so') |
| 89 | try: |
| 90 | extension = urlopen(url).read() |
| 91 | except: |
| 92 | sys.stderr.write( |
| 93 | 'could not download precompiled extension: {}\n'.format(url)) |
| 94 | return |
| 95 | try: |
| 96 | with open(target_path, 'w') as target: |
| 97 | target.write(extension) |
| 98 | setup_arguments['ext_modules'] = [] |
| 99 | except: |
| 100 | sys.stderr.write( |
| 101 | 'could not write precompiled extension to directory: {} -> {}\n' |
| 102 | .format(url, target_path)) |