-
Notifications
You must be signed in to change notification settings - Fork 84
/
setup.py
221 lines (184 loc) · 7.08 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sys
vi = sys.version_info
if vi < (3, 5):
raise RuntimeError('httptools require Python 3.5 or greater')
else:
import os.path
import pathlib
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as build_ext
CFLAGS = ['-O2']
ROOT = pathlib.Path(__file__).parent
CYTHON_DEPENDENCY = 'Cython(>=0.29.24,<0.30.0)'
class httptools_build_ext(build_ext):
user_options = build_ext.user_options + [
('cython-always', None,
'run cythonize() even if .c files are present'),
('cython-annotate', None,
'Produce a colorized HTML version of the Cython source.'),
('cython-directives=', None,
'Cythion compiler directives'),
('use-system-llhttp', None,
'Use the system provided llhttp, instead of the bundled one'),
('use-system-http-parser', None,
'Use the system provided http-parser, instead of the bundled one'),
]
boolean_options = build_ext.boolean_options + [
'cython-always',
'cython-annotate',
'use-system-llhttp',
'use-system-http-parser',
]
def initialize_options(self):
# initialize_options() may be called multiple times on the
# same command object, so make sure not to override previously
# set options.
if getattr(self, '_initialized', False):
return
super().initialize_options()
self.use_system_llhttp = False
self.use_system_http_parser = False
self.cython_always = False
self.cython_annotate = None
self.cython_directives = None
def finalize_options(self):
# finalize_options() may be called multiple times on the
# same command object, so make sure not to override previously
# set options.
if getattr(self, '_initialized', False):
return
need_cythonize = self.cython_always
cfiles = {}
for extension in self.distribution.ext_modules:
for i, sfile in enumerate(extension.sources):
if sfile.endswith('.pyx'):
prefix, ext = os.path.splitext(sfile)
cfile = prefix + '.c'
if os.path.exists(cfile) and not self.cython_always:
extension.sources[i] = cfile
else:
if os.path.exists(cfile):
cfiles[cfile] = os.path.getmtime(cfile)
else:
cfiles[cfile] = 0
need_cythonize = True
if need_cythonize:
try:
import Cython
except ImportError:
raise RuntimeError(
'please install Cython to compile httptools from source')
if Cython.__version__ < '0.29':
raise RuntimeError(
'httptools requires Cython version 0.29 or greater')
from Cython.Build import cythonize
directives = {}
if self.cython_directives:
for directive in self.cython_directives.split(','):
k, _, v = directive.partition('=')
if v.lower() == 'false':
v = False
if v.lower() == 'true':
v = True
directives[k] = v
self.distribution.ext_modules[:] = cythonize(
self.distribution.ext_modules,
compiler_directives=directives,
annotate=self.cython_annotate)
super().finalize_options()
self._initialized = True
def build_extensions(self):
mod_parser, mod_url_parser = self.distribution.ext_modules
if self.use_system_llhttp:
mod_parser.libraries.append('llhttp')
if sys.platform == 'darwin' and \
os.path.exists('/opt/local/include'):
# Support macports on Mac OS X.
mod_parser.include_dirs.append('/opt/local/include')
else:
mod_parser.include_dirs.append(
str(ROOT / 'vendor' / 'llhttp' / 'include'))
mod_parser.include_dirs.append(
str(ROOT / 'vendor' / 'llhttp' / 'src'))
mod_parser.sources.append('vendor/llhttp/src/api.c')
mod_parser.sources.append('vendor/llhttp/src/http.c')
mod_parser.sources.append('vendor/llhttp/src/llhttp.c')
if self.use_system_http_parser:
mod_url_parser.libraries.append('http_parser')
if sys.platform == 'darwin' and \
os.path.exists('/opt/local/include'):
# Support macports on Mac OS X.
mod_url_parser.include_dirs.append('/opt/local/include')
else:
mod_url_parser.include_dirs.append(
str(ROOT / 'vendor' / 'http-parser'))
mod_url_parser.sources.append(
'vendor/http-parser/http_parser.c')
super().build_extensions()
with open(str(ROOT / 'README.md')) as f:
long_description = f.read()
with open(str(ROOT / 'httptools' / '_version.py')) as f:
for line in f:
if line.startswith('__version__ ='):
_, _, version = line.partition('=')
VERSION = version.strip(" \n'\"")
break
else:
raise RuntimeError(
'unable to read the version from httptools/_version.py')
setup_requires = []
if (not (ROOT / 'httptools' / 'parser' / 'parser.c').exists() or
'--cython-always' in sys.argv):
# No Cython output, require Cython to build.
setup_requires.append(CYTHON_DEPENDENCY)
setup(
name='httptools',
version=VERSION,
description='A collection of framework independent HTTP protocol utils.',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/MagicStack/httptools',
classifiers=[
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Operating System :: POSIX',
'Operating System :: MacOS :: MacOS X',
'Environment :: Web Environment',
'Development Status :: 5 - Production/Stable',
],
platforms=['macOS', 'POSIX', 'Windows'],
python_requires='>=3.5.0',
zip_safe=False,
author='Yury Selivanov',
author_email='yury@magic.io',
license='MIT',
packages=['httptools', 'httptools.parser'],
cmdclass={
'build_ext': httptools_build_ext,
},
ext_modules=[
Extension(
"httptools.parser.parser",
sources=[
"httptools/parser/parser.pyx",
],
extra_compile_args=CFLAGS,
),
Extension(
"httptools.parser.url_parser",
sources=[
"httptools/parser/url_parser.pyx",
],
extra_compile_args=CFLAGS,
),
],
include_package_data=True,
test_suite='tests.suite',
setup_requires=setup_requires,
extras_require={
'test': [
CYTHON_DEPENDENCY
]
}
)