-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
executable file
·350 lines (276 loc) · 11.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python
# Python
import os
import shutil
import subprocess
import sys
import tarfile
from distutils.spawn import find_executable
# Setuptools
from setuptools import Command, setup
import setuptools.command.install as orig_install
# Wheel
import wheel.bdist_wheel as orig_bdist_wheel
class XapianConfigCommand(Command):
description = 'xapian config'
user_options = [
('build-temp=', 't',
"directory to put temporary build by-products"),
('xapian-config=', None,
'path to xapian-config command (searches path by default)'),
('xapian-version=', None,
'version of xapian-bindings to build (uses xapian-config --version by default)'),
]
def initialize_options(self):
self.build_temp = None
self.xapian_config = None
self.xapian_version = None
def finalize_options(self):
self.set_undefined_options(
'build',
('build_temp', 'build_temp'),
)
if self.xapian_config is None:
self.xapian_config = 'xapian-config'
def run(self):
xapian_config = find_executable(self.xapian_config)
if not xapian_config:
raise RuntimeError('{0} not found'.format(self.xapian_config))
print(xapian_config)
xc_version_output = subprocess.check_output([xapian_config, '--version'])
xc_version = xc_version_output.strip().rsplit()[-1].decode()
if self.xapian_version is None:
self.xapian_version = xc_version
elif self.xapian_version != xc_version:
print('warning: {0} != {1}'.format(self.xapian_version, xc_version))
self.xapian_version = xc_version
print(self.xapian_version)
class XapianDownloadCommand(Command):
description = 'xapian download'
user_options = [
('build-temp=', 't',
"directory to put temporary build by-products"),
('xapian-version=', None,
'version of xapian-bindings to download (uses xapian-config --version by default)'),
('xapian-url=', None,
'custom url for xapian bindings'),
('xapian-archive=', None,
'custom path to archive file for xapian bindings'),
]
def initialize_options(self):
self.build_temp = None
self.xapian_version = None
self.xapian_url = None
self.xapian_archive = None
def finalize_options(self):
self.set_undefined_options(
'xapian_config',
('build_temp', 'build_temp'),
('xapian_version', 'xapian_version'),
)
def run(self):
if not self.xapian_url:
if not self.xapian_version:
self.run_command('xapian_config')
xapian_config_cmd = self.get_finalized_command('xapian_config')
self.xapian_version = xapian_config_cmd.xapian_version
if not self.xapian_url:
self.xapian_url = 'https://oligarchy.co.uk/xapian/{0}/xapian-bindings-{0}.tar.xz'.format(self.xapian_version)
print(self.xapian_url)
if not self.xapian_archive:
self.xapian_archive = os.path.join(self.build_temp, self.xapian_url.split('/')[-1])
if not os.path.exists(os.path.dirname(self.xapian_archive)):
os.makedirs(os.path.dirname(self.xapian_archive))
import requests # noqa
response = requests.get(self.xapian_url, stream=True)
print(response.headers)
content_length = response.headers.get('Content-Length', None)
if os.path.exists(self.xapian_archive):
xb_size = os.path.getsize(self.xapian_archive)
else:
xb_size = 0
if xb_size == 0 or xb_size != content_length:
# FIXME: Check PGP signature!
with response.raw as xb_dl:
with open(self.xapian_archive, 'wb') as xb_archive:
shutil.copyfileobj(xb_dl, xb_archive)
class XapianExtractCommand(Command):
description = 'xapian extract'
user_options = [
('build-temp=', 't',
"directory to put temporary build by-products"),
('xapian-archive=', None,
'path to xapian bindings archive'),
]
def initialize_options(self):
self.build_temp = None
self.xapian_archive = None
self.xapian_src_dir = None
def finalize_options(self):
self.set_undefined_options(
'xapian_download',
('build_temp', 'build_temp'),
('xapian_archive', 'xapian_archive'),
)
def run(self):
if not self.xapian_archive:
self.run_command('xapian_download')
xapian_download_cmd = self.get_finalized_command('xapian_download')
self.xapian_archive = xapian_download_cmd.xapian_archive
tf_root_dirs = set()
with tarfile.open(self.xapian_archive) as tf:
for name in tf.getnames():
tf_root_dirs.add(name.split('/')[0])
tf.extractall(self.build_temp)
print(tf_root_dirs)
self.xapian_src_dir = os.path.join(self.build_temp, list(tf_root_dirs)[0])
class XapianBuildCommand(Command):
description = 'xapian build'
user_options = [
('xapian-config=', None,
'path to xapian config'),
('xapian-src-dir=', None,
'path to xapian bindings archive'),
('xapian-prefix=', None,
'path prefix where xapian bindings will be installed'),
]
def initialize_options(self):
self.xapian_config = None
self.xapian_src_dir = None
self.xapian_prefix = None
def finalize_options(self):
self.set_undefined_options(
'xapian_config',
('xapian_config', 'xapian_config'),
)
self.set_undefined_options(
'xapian_extract',
('xapian_src_dir', 'xapian_src_dir'),
)
self.set_undefined_options(
'install',
('root', 'xapian_prefix'),
)
def run(self):
if not self.xapian_config:
self.run_command('xapian_config')
xapian_config_cmd = self.get_finalized_command('xapian_config')
self.xapian_config = xapian_config_cmd.xapian_config
if not self.xapian_src_dir:
self.run_command('xapian_extract')
xapian_extract_cmd = self.get_finalized_command('xapian_extract')
self.xapian_src_dir = xapian_extract_cmd.xapian_src_dir
if not self.xapian_prefix:
install_cmd = self.get_finalized_command('install')
self.xapian_prefix = install_cmd.root or sys.prefix
self.xapian_prefix = os.path.normpath(os.path.abspath(self.xapian_prefix))
xb_build_env = dict(os.environ.items())
xb_build_env['XAPIAN_CONFIG'] = self.xapian_config
xb_build_env['PYTHON3'] = os.path.normpath(sys.executable)
xb_build_env['PYTHON3_LIB'] = self.xapian_prefix
xb_build_env['PYTHONPATH'] = os.path.pathsep.join(sys.path)
subprocess.check_call(['./configure', '--with-python3', '--prefix={}'.format(self.xapian_prefix)], cwd=self.xapian_src_dir, env=xb_build_env)
subprocess.check_call(['make', 'clean', 'all'], cwd=self.xapian_src_dir, env=xb_build_env)
class XapianInstallCommand(Command):
description = 'xapian install'
user_options = [
('xapian-config=', None,
'path to xapian config'),
('xapian-src-dir=', None,
'path to xapian bindings archive'),
('xapian-prefix=', None,
'path prefix where xapian bindings will be installed'),
]
def initialize_options(self):
self.xapian_config = None
self.xapian_src_dir = None
self.xapian_prefix = None
def finalize_options(self):
self.set_undefined_options(
'xapian_build',
('xapian_config', 'xapian_config'),
('xapian_src_dir', 'xapian_src_dir'),
('xapian_prefix', 'xapian_prefix'),
)
def run(self):
if not self.xapian_config or not self.xapian_src_dir or not self.xapian_prefix:
self.run_command('xapian_build')
xapian_build_cmd = self.get_finalized_command('xapian_build')
if not self.xapian_config:
self.xapian_config = xapian_build_cmd.xapian_config
if not self.xapian_src_dir:
self.xapian_src_dir = xapian_build_cmd.xapian_src_dir
if not self.xapian_prefix:
self.xapian_prefix = xapian_build_cmd.xapian_prefix
xb_build_env = dict(os.environ.items())
xb_build_env['XAPIAN_CONFIG'] = self.xapian_config
xb_build_env['PYTHON3'] = os.path.normpath(sys.executable)
xb_build_env['PYTHON3_LIB'] = self.xapian_prefix
xb_build_env['PYTHONPATH'] = os.path.pathsep.join(sys.path)
xb_build_env['PYTHONDONTWRITEBYTECODE'] = '0'
subprocess.check_call(['make', 'install'], cwd=self.xapian_src_dir, env=xb_build_env)
share_path = os.path.join(self.xapian_prefix, 'share')
if os.path.exists(share_path):
shutil.rmtree(share_path)
class BaseTwineCommand(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
for sub_cmd in self.get_sub_commands():
self.run_command(sub_cmd)
self.get_finalized_command(sub_cmd)
dist_files = [df[2] for df in self.distribution.dist_files]
self.spawn(['twine', self.twine_subcommand] + dist_files)
sub_commands = [
('sdist', lambda self: True),
]
class TwineCheckCommand(BaseTwineCommand):
description = 'Check distribution files with twine'
twine_subcommand = 'check'
class TwineUploadCommand(BaseTwineCommand):
description = 'Upload distribution files with twine'
twine_subcommand = 'upload'
class UnsupportedCommand(Command):
description = 'This command is not supported'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
sys.exit('This command is not supported!')
class InstallCommand(orig_install.install):
def do_egg_install(self):
sys.exit('Egg install is not supported!')
InstallCommand.sub_commands = orig_install.install.sub_commands + [
('xapian_install', lambda self: True),
]
class BdistWheelCommand(orig_bdist_wheel.bdist_wheel):
def finalize_options(self):
if not self.build_number:
self.run_command('xapian_config')
xapian_config_cmd = self.get_finalized_command('xapian_config')
self.build_number = xapian_config_cmd.xapian_version
orig_bdist_wheel.bdist_wheel.finalize_options(self)
self.root_is_pure = False
setup(
cmdclass={
'xapian_config': XapianConfigCommand,
'xapian_download': XapianDownloadCommand,
'xapian_extract': XapianExtractCommand,
'xapian_build': XapianBuildCommand,
'xapian_install': XapianInstallCommand,
'install': InstallCommand,
'bdist_wheel': BdistWheelCommand,
'twine_check': TwineCheckCommand,
'twine_upload': TwineUploadCommand,
'unsupported': UnsupportedCommand,
'bdist_egg': UnsupportedCommand,
'develop': UnsupportedCommand,
'easy_install': UnsupportedCommand,
'upload_docs': UnsupportedCommand,
},
)