1#!/usr/bin/env python
2
3#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12#   http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
22import sys
23try:
24    from setuptools import setup, Extension
25except Exception:
26    from distutils.core import setup, Extension
27
28from distutils.command.build_ext import build_ext
29from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError
30
31# Fix to build sdist under vagrant
32import os
33if 'vagrant' in str(os.environ):
34    try:
35        del os.link
36    except AttributeError:
37        pass
38
39include_dirs = ['src']
40if sys.platform == 'win32':
41    include_dirs.append('compat/win32')
42    ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError, IOError)
43else:
44    ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
45
46
47class BuildFailed(Exception):
48    pass
49
50
51class ve_build_ext(build_ext):
52    def run(self):
53        try:
54            build_ext.run(self)
55        except DistutilsPlatformError:
56            raise BuildFailed()
57
58    def build_extension(self, ext):
59        try:
60            build_ext.build_extension(self, ext)
61        except ext_errors:
62            raise BuildFailed()
63
64
65def run_setup(with_binary):
66    if with_binary:
67        extensions = dict(
68            ext_modules=[
69                Extension('thrift.protocol.fastbinary',
70                          sources=[
71                              'src/ext/module.cpp',
72                              'src/ext/types.cpp',
73                              'src/ext/binary.cpp',
74                              'src/ext/compact.cpp',
75                          ],
76                          include_dirs=include_dirs,
77                          )
78            ],
79            cmdclass=dict(build_ext=ve_build_ext)
80        )
81    else:
82        extensions = dict()
83
84    ssl_deps = []
85    if sys.version_info[0] == 2:
86        ssl_deps.append('ipaddress')
87    if sys.hexversion < 0x03050000:
88        ssl_deps.append('backports.ssl_match_hostname>=3.5')
89    tornado_deps = ['tornado>=4.0']
90    twisted_deps = ['twisted']
91
92    setup(name='thrift',
93          version='0.13.0',
94          description='Python bindings for the Apache Thrift RPC system',
95          author='Apache Thrift Developers',
96          author_email='dev@thrift.apache.org',
97          url='http://thrift.apache.org',
98          license='Apache License 2.0',
99          install_requires=['six>=1.7.2'],
100          extras_require={
101              'ssl': ssl_deps,
102              'tornado': tornado_deps,
103              'twisted': twisted_deps,
104              'all': ssl_deps + tornado_deps + twisted_deps,
105          },
106          packages=[
107              'thrift',
108              'thrift.protocol',
109              'thrift.transport',
110              'thrift.server',
111          ],
112          package_dir={'thrift': 'src'},
113          classifiers=[
114              'Development Status :: 5 - Production/Stable',
115              'Environment :: Console',
116              'Intended Audience :: Developers',
117              'Programming Language :: Python',
118              'Programming Language :: Python :: 2',
119              'Programming Language :: Python :: 3',
120              'Topic :: Software Development :: Libraries',
121              'Topic :: System :: Networking'
122          ],
123          zip_safe=False,
124          **extensions
125          )
126
127
128try:
129    with_binary = True
130    run_setup(with_binary)
131except BuildFailed:
132    print()
133    print('*' * 80)
134    print("An error occurred while trying to compile with the C extension enabled")
135    print("Attempting to build without the extension now")
136    print('*' * 80)
137    print()
138
139    run_setup(False)
140