-
Notifications
You must be signed in to change notification settings - Fork 15
/
setup.py
202 lines (176 loc) · 7.27 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""The setup script."""
import os
import re
import subprocess
import sys
import sysconfig
import setuptools
from distutils import log
from distutils.command.build import build
from urllib.request import urlretrieve
from setuptools import setup, find_packages, Extension
from setuptools.command.develop import develop
with open('README.rst') as readme_file:
readme = readme_file.read()
with open('HISTORY.rst') as history_file:
history = history_file.read()
requirements = []
test_requirements = []
class nginxpy_build(build):
def run(self):
print('checking current NGINX install')
nginx_bin = subprocess.getoutput('which nginx')
nginx_ver = subprocess.getoutput(nginx_bin + ' -v')
nginx_ver = re.findall(r'nginx/([\d\.]+)', nginx_ver)[0]
print(nginx_ver, '@', nginx_bin)
print('checking NGINX configure parameters')
nginx_params = subprocess.getoutput(nginx_bin + ' -V')
nginx_params = re.findall(r'configure arguments: (.*)',
nginx_params)[0]
def filter_params(params):
params = iter(params)
try:
while True:
val = next(params)
if val.startswith('--add-dynamic-module'):
if val == '--add-dynamic-module':
next(params)
else:
yield val
except StopIteration:
pass
nginx_params = list(filter_params(nginx_params.split()))
print(' '.join(nginx_params))
print('updating NGINX source')
build_base = os.path.abspath(self.build_base)
if not os.path.exists(build_base):
os.mkdir(build_base)
nginx_src = os.path.join(build_base, 'nginx-' + nginx_ver)
nginx_tarball = os.path.join(build_base,
'nginx-' + nginx_ver + '.tar.gz')
nginx_url = 'https://nginx.org/download/nginx-{}.tar.gz'.format(
nginx_ver)
if os.path.exists(nginx_src):
print('reusing', nginx_src)
else:
if os.path.exists(nginx_tarball):
print('reusing', nginx_tarball)
else:
print('downloading', nginx_url)
urlretrieve(nginx_url, nginx_tarball)
print('extracting tarball')
subprocess.check_call(['tar', 'xvf', nginx_tarball,
'-C', build_base])
print('retrieving NGINX build options')
nginx_configure = os.path.join(nginx_src, 'configure')
nginx_make = os.path.join(nginx_src, 'objs', 'Makefile')
if os.path.exists(nginx_make):
print('reusing', nginx_make)
else:
print('configuring NGINX')
src_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'nginx')
cmd = (' '.join([nginx_configure] + nginx_params +
['--add-dynamic-module=' + src_dir]))
subprocess.check_call([cmd],
cwd=nginx_src, shell=True)
with open(nginx_make, 'a') as f:
f.write('\n')
f.write('print-% : ; @echo $* = $($*)')
f.write('\n')
nginx_cflags = subprocess.getoutput(
'make -f ' + nginx_make + ' print-CFLAGS')
nginx_cflags = re.findall(r'CFLAGS = (.*)', nginx_cflags)[0].split()
nginx_cflags = list(filter(lambda x: x != '-Werror', nginx_cflags))
nginx_all_incs = subprocess.getoutput(
'make -f ' + nginx_make + ' print-ALL_INCS')
nginx_all_incs = re.findall(r'ALL_INCS = (.*)', nginx_all_incs)[0]
nginx_all_incs = list(map(lambda x: os.path.join(nginx_src, x),
filter(lambda x: x != '-I',
nginx_all_incs.split())))
if subprocess.getoutput('grep fPIC ' + nginx_make):
nginx_cflags.append('-fPIC')
print('NGINX CFLAGS:', ' '.join(nginx_cflags))
print('NGINX ALL_INCS:', ' '.join(nginx_all_incs))
nginxpy.include_dirs.extend(nginx_all_incs)
nginxpy.extra_compile_args.extend(nginx_cflags)
print('retrieving Python link options')
pyver = sysconfig.get_config_var('VERSION')
getvar = sysconfig.get_config_var
libs = ['-lpython' + pyver + sys.abiflags]
libs += getvar('LIBS').split()
libs += getvar('SYSLIBS').split()
# add the prefix/lib/pythonX.Y/config dir, but only if there is no
# shared library in prefix/lib/.
if not getvar('Py_ENABLE_SHARED'):
libs.insert(0, '-L' + getvar('LIBPL'))
if not getvar('PYTHONFRAMEWORK'):
libs.extend(getvar('LINKFORSHARED').split())
libs = list(filter(lambda x: 'stack_size' not in x, libs))
print('link options:', ' '.join(libs))
nginxpy.extra_link_args.extend(libs)
super().run()
class nginxpy_develop(develop):
def install_for_development(self):
self.reinitialize_command('build', inplace=1)
self.run_command('build')
self.install_site_py() # ensure that target dir is site-safe
if setuptools.bootstrap_install_from:
self.easy_install(setuptools.bootstrap_install_from)
setuptools.bootstrap_install_from = None
self.install_namespaces()
# create an .egg-link in the installation dir, pointing to our egg
log.info("Creating %s (link to %s)", self.egg_link, self.egg_base)
if not self.dry_run:
with open(self.egg_link, "w") as f:
f.write(self.egg_path + "\n" + self.setup_path)
# postprocess the installed distro, fixing up .pth, installing scripts,
# and handling requirements
self.process_distribution(None, self.dist, not self.no_deps)
nginxpy = Extension(
'nginx._nginx',
sources=[
'nginx/ngx_python_module.c',
'nginx/ngx_python_module_modules.c',
'nginx/nginx.pyx',
], define_macros=[
('PYTHON_EXEC', '"{}"'.format(os.path.abspath(sys.executable))),
], depends=[
'nginx/cycle.pyx',
'nginx/log.pyx',
'nginx/asyncio/loop.pyx',
'nginx/http/http.pyx',
])
setup(
author="DecentFoX Studio",
author_email='[email protected]',
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
],
description="Embed Python in NGINX.",
install_requires=requirements,
license="Apache Software License 2.0",
long_description=readme + '\n\n' + history,
include_package_data=True,
keywords='nginxpy',
name='nginxpy',
packages=find_packages(include=['nginx', 'nginx.asyncio', 'nginx.http']),
ext_modules=[nginxpy],
cmdclass=dict(build=nginxpy_build),
entry_points='''\
[nginx.modules]
100 = nginx.asyncio:AsyncioModule
200 = nginx.http:HTTPModule
''',
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/decentfox/nginxpy',
version='0.1.0',
zip_safe=False,
)