-
Notifications
You must be signed in to change notification settings - Fork 3
/
setuplib.py
590 lines (486 loc) · 18.4 KB
/
setuplib.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# Author: Forest Bond
# This file is in the public domain.
from __future__ import with_statement, unicode_literals
import os, sys, shutil
from tempfile import mkdtemp
from modulefinder import Module
from distutils.command.build import build
from distutils.command.clean import clean as _clean
from distutils.command.install import install
from distutils.command.install_lib import install_lib as _install_lib
from distutils.command.install_data import install_data as _install_data
from distutils.core import (
setup as _setup,
Command,
)
from distutils.spawn import spawn
from distutils import log
from distutils.dir_util import remove_tree
from distutils.dist import Distribution
try:
from py2exe.build_exe import py2exe as _py2exe
except ImportError:
_py2exe = None
else:
from py2exe.build_exe import byte_compile
###
class test(Command):
description = 'run tests'
user_options = [
('tests=', None, 'names of tests to run'),
('print-only', None, "don't run tests, just print their names"),
('coverage', None, "print coverage analysis (requires coverage.py)"),
]
def initialize_options(self):
self.tests = None
self.print_only = False
self.coverage = False
def finalize_options(self):
if self.tests is not None:
self.tests = self.tests.split(',')
def run(self):
build_obj = self.get_finalized_command('build')
build_obj.run()
sys.path.insert(0, build_obj.build_lib)
try:
mod = __import__(self.distribution.test_module)
main = getattr(mod, 'main')
main(
test_names = self.tests,
print_only = self.print_only,
coverage = self.coverage,
)
finally:
sys.path.remove(build_obj.build_lib)
Distribution.test_module = 'tests'
###
class clean(_clean):
user_options = _clean.user_options + [
('build-man=', None, 'manpage build directory'),
]
def initialize_options(self):
_clean.initialize_options(self)
self.build_man = None
def finalize_options(self):
_clean.finalize_options(self)
self.set_undefined_options('build_man', ('build_dir', 'build_man'))
def run(self):
if self.all:
if os.path.exists(self.build_man):
remove_tree(self.build_man, dry_run = self.dry_run)
else:
log.debug(
"'%s' does not exist -- can't clean it",
self.build_man,
)
_clean.run(self)
###
class build_man(Command):
user_options = _install_data.user_options + [
('build-dir=', 'b', 'manpage build directory'),
]
description = 'Build manual pages from docbook XML.'
xsltproc = ['xsltproc', '--nonet', '--novalid', '--xinclude']
def initialize_options(self):
self.build_base = None
self.build_dir = None
def finalize_options(self):
self.set_undefined_options('build', ('build_base', 'build_base'))
if self.build_dir is None:
self.build_dir = os.path.join(self.build_base, 'man')
self.docbook_files = []
if self.distribution.manpage_sources is not None:
unclaimed_files = list(self.distribution.manpage_sources)
for index, filename in enumerate(list(unclaimed_files)):
if filename.endswith('.xml'):
self.docbook_files.append(os.path.abspath(filename))
del unclaimed_files[index]
if unclaimed_files:
log.error(
'unknown manpage source file types: %s',
', '.join(unclaimed_files),
)
raise SystemExit(1)
def _find_docbook_manpage_stylesheet(self):
from libxml2 import catalogResolveURI
return catalogResolveURI(
'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
)
def build_manpage_from_docbook(self, stylesheet, docbook_file):
if stylesheet is None:
raise AssertionError('stylesheet is None')
command = self.xsltproc + [stylesheet, docbook_file]
orig_wd = os.getcwd()
os.chdir(self.build_dir)
try:
spawn(command, dry_run = self.dry_run)
finally:
os.chdir(orig_wd)
def run(self):
if self.docbook_files:
stylesheet = self._find_docbook_manpage_stylesheet()
if stylesheet is None:
log.warn(
'Warning: missing docbook XSL stylesheets; '
'manpages will not be built.\n'
'Please install the docbook XSL stylesheets from '
'http://docbook.org/.'
)
else:
if not self.dry_run:
if not os.path.exists(self.build_dir):
os.mkdir(self.build_dir)
for docbook_file in self.docbook_files:
log.info('building manpage from docbook: %s', docbook_file)
if not self.dry_run:
self.build_manpage_from_docbook(
stylesheet,
docbook_file,
)
Distribution.manpage_sources = None
build.sub_commands.append((
'build_man',
(lambda self: bool(self.distribution.manpage_sources)),
))
class install_man(_install_data):
def initialize_options(self):
_install_data.initialize_options(self)
self.build_dir = None
def finalize_options(self):
_install_data.finalize_options(self)
self.set_undefined_options('build_man', ('build_dir', 'build_dir'))
self.data_files = []
if os.path.exists(self.build_dir):
for entry in os.listdir(self.build_dir):
path = os.path.join(self.build_dir, entry)
if os.path.isfile(path):
base, ext = os.path.splitext(entry)
section = int(ext[1:])
self.data_files.append(
('share/man/man%u' % section, [path])
)
install.sub_commands.append((
'install_man',
(lambda self: bool(self.distribution.manpage_sources)),
))
###
class _DistinfoMixin:
def _split_distinfo_module(self):
parts = self.distribution.distinfo_module.split('.')
return parts[:-1], parts[-1]
def _prepare_distinfo_string(self, value):
#if isinstance(value, str):
# value = unicode(value)
#return unicode(repr(value)).encode('utf-8')r
return value
def _write_distinfo_module(self, outfile, distinfo = (), imports = ()):
distinfo = list(distinfo)
imports = list(imports)
distinfo.insert(
0,
(
'version',
self._prepare_distinfo_string(self.distribution.metadata.version),
),
)
log.info("creating distinfo file %r:", outfile)
for k, v in distinfo:
log.info(' %s = %s', k, v)
if not self.dry_run:
with open(outfile, 'wb') as f:
f.write(b'# coding: utf-8\n')
f.write(b'\n')
for modname in imports:
f.write(b'import %s\n' % modname)
if imports:
f.write(b'\n')
for k, v in distinfo:
line = '%s = "%s"\n' % (k, v)
f.write(line.encode('utf8'))
###
class install_data(_install_data):
user_options = _install_data.user_options + [
(
'install-dir-arch=',
None,
'base directory for installing architecture-dependent data files',
),
(
'install-dir-arch-pkg=',
None,
'package-specific directory for installing architecture-dependent data files',
),
(
'install-dir-indep=',
None,
'base directory for installing architecture-independent data files',
),
(
'install-dir-indep-pkg=',
None,
'package-specific directory for installing architecture-independent data files',
),
]
def initialize_options(self):
_install_data.initialize_options(self)
self.install_dir_arch = None
self.install_dir_arch_pkg = None
self.install_dir_indep = None
self.install_dir_indep_pkg = None
self.data_files_arch = self.distribution.data_files_arch
self.data_files_arch_pkg = self.distribution.data_files_arch_pkg
self.data_files_indep = self.distribution.data_files_indep
self.data_files_indep_pkg = self.distribution.data_files_indep_pkg
def _get_relative_install_dir(self, p):
return p[len(self.install_dir) + len(os.sep):]
def finalize_options(self):
_install_data.finalize_options(self)
py2exe_obj = self.distribution.get_command_obj('py2exe', False)
if py2exe_obj is not None:
py2exe_obj.ensure_finalized()
if (py2exe_obj is not None) and (
self.install_dir == py2exe_obj.dist_dir
):
self.install_dir_arch = self.install_dir
self.install_dir_arch_pkg = self.install_dir
self.install_dir_indep = self.install_dir
self.install_dir_indep_pkg = self.install_dir
else:
if self.install_dir_arch is None:
if sys.platform == 'win32':
self.install_dir_arch = self.install_dir
else:
self.install_dir_arch = os.path.join(self.install_dir, 'lib')
if self.install_dir_arch_pkg is None:
self.install_dir_arch_pkg = os.path.join(
self.install_dir_arch,
self.distribution.metadata.name,
)
if self.install_dir_indep is None:
if sys.platform == 'win32':
self.install_dir_indep = self.install_dir
else:
self.install_dir_indep = os.path.join(self.install_dir, 'share')
if self.install_dir_indep_pkg is None:
self.install_dir_indep_pkg = os.path.join(
self.install_dir_indep,
self.distribution.metadata.name,
)
if self.data_files is None:
self.data_files = []
if self.data_files_arch:
self.data_files.extend(self._gen_data_files(
self._get_relative_install_dir(self.install_dir_arch),
self.data_files_arch,
))
if self.data_files_arch_pkg:
self.data_files.extend(self._gen_data_files(
self._get_relative_install_dir(self.install_dir_arch_pkg),
self.data_files_arch_pkg,
))
if self.data_files_indep:
self.data_files.extend(self._gen_data_files(
self._get_relative_install_dir(self.install_dir_indep),
self.data_files_indep,
))
if self.data_files_indep_pkg:
self.data_files.extend(self._gen_data_files(
self._get_relative_install_dir(self.install_dir_indep_pkg),
self.data_files_indep_pkg,
))
def _gen_data_files(self, base_dir, data_files):
for arg in data_files:
if isinstance(arg, basestring):
yield (base_dir, [arg])
else:
subdir, filenames = arg
yield (os.path.join(base_dir, subdir), filenames)
Distribution.data_files_arch = None
Distribution.data_files_arch_pkg = None
Distribution.data_files_indep = None
Distribution.data_files_indep_pkg = None
orig_has_data_files = Distribution.has_data_files
def has_data_files(self):
if orig_has_data_files(self):
return True
return any([
self.data_files_arch,
self.data_files_arch_pkg,
self.data_files_indep,
self.data_files_indep_pkg,
])
Distribution.has_data_files = has_data_files
###
class install_lib(_DistinfoMixin, _install_lib):
def initialize_options(self):
_install_lib.initialize_options(self)
self.distinfo_package = None
self.distinfo_module = None
def finalize_options(self):
_install_lib.finalize_options(self)
if self.distribution.distinfo_module is not None:
self.distinfo_package, self.distinfo_module = \
self._split_distinfo_module()
def install(self):
retval = _install_lib.install(self)
if retval is None:
return retval
py2exe_obj = self.distribution.get_command_obj('py2exe', False)
if (py2exe_obj is None) and (self.distinfo_package is not None):
parts = [self.install_dir]
parts.extend(self.distinfo_package)
parts.append('%s.py' % self.distinfo_module)
installed_module_path = os.path.join(*parts)
install_obj = self.get_finalized_command('install')
install_data_obj = self.get_finalized_command('install_data')
distinfo = [
(
'install_base',
self._prepare_distinfo_string(os.path.abspath(
install_obj.install_base,
)),
),
(
'install_platbase',
self._prepare_distinfo_string(os.path.abspath(
install_obj.install_platbase,
)),
),
(
'install_purelib',
self._prepare_distinfo_string(os.path.abspath(
install_obj.install_purelib,
)),
),
(
'install_platlib',
self._prepare_distinfo_string(os.path.abspath(
install_obj.install_platlib,
)),
),
(
'install_lib',
self._prepare_distinfo_string(os.path.abspath(
install_obj.install_lib,
)),
),
(
'install_headers',
self._prepare_distinfo_string(os.path.abspath(
install_obj.install_headers,
)),
),
(
'install_scripts',
self._prepare_distinfo_string(os.path.abspath(
install_obj.install_scripts,
)),
),
(
'install_data',
self._prepare_distinfo_string(os.path.abspath(
install_data_obj.install_dir,
)),
),
(
'install_data_arch',
self._prepare_distinfo_string(os.path.abspath(
install_data_obj.install_dir_arch,
)),
),
(
'install_data_arch_pkg',
self._prepare_distinfo_string(os.path.abspath(
install_data_obj.install_dir_arch_pkg,
)),
),
(
'install_data_indep',
self._prepare_distinfo_string(os.path.abspath(
install_data_obj.install_dir_indep,
)),
),
(
'install_data_indep_pkg',
self._prepare_distinfo_string(os.path.abspath(
install_data_obj.install_dir_indep_pkg,
)),
),
]
self._write_distinfo_module(installed_module_path, distinfo)
retval.append(installed_module_path)
return retval
if _py2exe is None:
py2exe = None
else:
class py2exe(_DistinfoMixin, _py2exe):
def make_lib_archive(self, *args, **kwargs):
if self.distribution.distinfo_module is not None:
imports = ['os', 'sys']
distinfo = [
(
'install_data',
'os.path.dirname(sys.executable)',
),
(
'install_data_arch',
'os.path.dirname(sys.executable)',
),
(
'install_data_arch_pkg',
'os.path.dirname(sys.executable)',
),
(
'install_data_indep',
'os.path.dirname(sys.executable)',
),
(
'install_data_indep_pkg',
'os.path.dirname(sys.executable)',
),
]
tmp_dir_path = mkdtemp()
try:
distinfo_package, distinfo_module = self._split_distinfo_module()
tmp_file_parent_path = os.path.join(
tmp_dir_path,
*distinfo_package
)
os.makedirs(tmp_file_parent_path)
tmp_file_path = os.path.join(
tmp_file_parent_path,
('%s.py' % distinfo_module),
)
self._write_distinfo_module(tmp_file_path, distinfo, imports)
sys.path.insert(0, tmp_dir_path)
try:
self._distinfo_compiled_files = byte_compile(
[Module(
name = self.distribution.distinfo_module,
file = tmp_file_path,
)],
target_dir = self.collect_dir,
optimize = self.optimize,
force = 0,
verbose = self.verbose,
dry_run = self.dry_run,
)
finally:
del sys.path[0]
finally:
shutil.rmtree(tmp_dir_path)
self.compiled_files.extend(self._distinfo_compiled_files)
return _py2exe.make_lib_archive(self, *args, **kwargs)
Distribution.distinfo_module = None
###
def setup(*args, **kwargs):
cmdclass = kwargs.setdefault('cmdclass', {})
kwargs['cmdclass'].setdefault('test', test)
kwargs['cmdclass'].setdefault('install_data', install_data)
kwargs['cmdclass'].setdefault('install_lib', install_lib)
kwargs['cmdclass'].setdefault('build_man', build_man)
kwargs['cmdclass'].setdefault('install_man', install_man)
kwargs['cmdclass'].setdefault('clean', clean)
if py2exe is not None:
kwargs['cmdclass'].setdefault('py2exe', py2exe)
return _setup(*args, **kwargs)