forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
169 lines (148 loc) · 5.48 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import platform
import sys
import warnings
from distutils.command.build_ext import build_ext
from distutils.errors import CCompilerError
from distutils.errors import DistutilsExecError
from distutils.errors import DistutilsPlatformError
from setuptools import setup
from setuptools.extension import Extension
f = open(os.path.join(os.path.dirname(__file__), 'README.rst'))
readme = f.read()
f.close()
extension_support = True # Assume we are building C extensions.
# Check if Cython is available and use it to generate extension modules. If
# Cython is not installed, we will fall back to using the pre-generated C files
# (so long as we're running on CPython).
try:
from Cython.Build import cythonize
from Cython.Distutils import build_ext
from Cython.Distutils.extension import Extension
except ImportError:
cython_installed = False
else:
if platform.python_implementation() != 'CPython':
cython_installed = extension_support = False
warnings.warn('C extensions disabled as you are not using CPython.')
else:
cython_installed = True
if 'sdist' in sys.argv and not cython_installed:
raise Exception('Building sdist requires that Cython be installed.')
if cython_installed:
src_ext = '.pyx'
else:
src_ext = '.c'
cythonize = lambda obj: obj
speedups_ext_module = Extension(
'playhouse._speedups',
['playhouse/_speedups' + src_ext])
sqlite_udf_module = Extension(
'playhouse._sqlite_udf',
['playhouse/_sqlite_udf' + src_ext])
sqlite_ext_module = Extension(
'playhouse._sqlite_ext',
['playhouse/_sqlite_ext' + src_ext],
libraries=['sqlite3'])
def _have_sqlite_extension_support():
import shutil
import tempfile
from distutils.ccompiler import new_compiler
from distutils.sysconfig import customize_compiler
libraries = ['sqlite3']
c_code = ('#include <sqlite3.h>\n\n'
'int main(int argc, char **argv) { return 0; }')
tmp_dir = tempfile.mkdtemp(prefix='tmp_pw_sqlite3_')
bin_file = os.path.join(tmp_dir, 'test_pw_sqlite3')
src_file = bin_file + '.c'
with open(src_file, 'w') as fh:
fh.write(c_code)
compiler = new_compiler()
customize_compiler(compiler)
success = False
try:
compiler.link_executable(
compiler.compile([src_file], output_dir=tmp_dir),
bin_file,
libraries=['sqlite3'])
except CCompilerError:
print('unable to compile sqlite3 C extensions - missing headers?')
except DistutilsExecError:
print('unable to compile sqlite3 C extensions - no c compiler?')
except DistutilsPlatformError:
print('unable to compile sqlite3 C extensions - platform error')
else:
success = True
shutil.rmtree(tmp_dir)
return success
# This is set to True if there is extension support and libsqlite3 is found.
sqlite_extension_support = False
if extension_support:
if os.environ.get('NO_SQLITE'):
warnings.warn('SQLite extensions will not be built at users request.')
elif not _have_sqlite_extension_support():
warnings.warn('Could not find libsqlite3, SQLite extensions will not '
'be built.')
else:
sqlite_extension_support = True
# Exception we will raise to indicate a failure to build C extensions.
class BuildFailure(Exception): pass
class _PeeweeBuildExt(build_ext):
def run(self):
try:
build_ext.run(self)
except DistutilsPlatformError:
raise BuildFailure()
def build_extension(self, ext):
try:
build_ext.build_extension(self, ext)
except (CCompilerError, DistutilsExecError, DistutilsPlatformError):
raise BuildFailure()
def _do_setup(c_extensions, sqlite_extensions):
if c_extensions:
ext_modules = [speedups_ext_module]
if sqlite_extensions:
ext_modules.extend([sqlite_udf_module, sqlite_ext_module])
else:
ext_modules = None
setup(
name='peewee',
version=__import__('peewee').__version__,
description='a little orm',
long_description=readme,
author='Charles Leifer',
author_email='coleifer@gmail.com',
url='https://github.com/coleifer/peewee/',
packages=['playhouse'],
py_modules=['peewee', 'pwiz'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: Software Development :: Libraries :: Python Modules',
],
license='MIT License',
platforms=['any'],
scripts=['pwiz.py'],
zip_safe=False,
cmdclass={'build_ext': _PeeweeBuildExt},
ext_modules=cythonize(ext_modules))
if extension_support:
try:
_do_setup(extension_support, sqlite_extension_support)
except BuildFailure:
print('#' * 75)
print('Error compiling C extensions, C extensions will not be built.')
print('#' * 75)
_do_setup(False, False)
else:
_do_setup(False, False)