blob: aeeb2754eaaaf719a5485f66f6c44781822e07b0 [file] [log] [blame]
Masood Malekghassemif7474092016-02-12 12:04:53 -08001# 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
30import os
31import platform
32import shutil
33import sys
Masood Malekghassemie5e01312016-02-17 13:06:01 -080034import sysconfig
Masood Malekghassemif7474092016-02-12 12:04:53 -080035
36import setuptools
37
38import commands
39import grpc_version
40
41try:
42 from urllib2 import urlopen
43except ImportError:
44 from urllib.request import urlopen
45
46PYTHON_STEM = os.path.dirname(os.path.abspath(__file__))
47BINARIES_REPOSITORY = os.environ.get(
48 'GRPC_PYTHON_BINARIES_REPOSITORY',
49 'https://storage.googleapis.com/grpc-precompiled-binaries/python')
50USE_PRECOMPILED_BINARIES = bool(int(os.environ.get(
51 'GRPC_PYTHON_USE_PRECOMPILED_BINARIES', '1')))
52
53def _tagged_ext_name(base):
54 uname = platform.uname()
Masood Malekghassemie5e01312016-02-17 13:06:01 -080055 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 Malekghassemif7474092016-02-12 12:04:53 -080064
65
66class 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
93def update_setup_arguments(setup_arguments):
Masood Malekghassemi7d8ac462016-03-15 12:15:13 -070094 if not USE_PRECOMPILED_BINARIES:
95 sys.stderr.write('not using precompiled extension')
96 return
Masood Malekghassemif7474092016-02-12 12:04:53 -080097 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 Malekghassemi73617342016-02-18 12:39:42 -0800113 return
114 setup_arguments['package_data']['grpc._cython'].append('cygrpc.so')