/
make.py
executable file
·800 lines (703 loc) · 24.7 KB
/
make.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
#!/usr/bin/env python
# -*- coding: ascii -*-
#
# Copyright 2006 - 2014
# Andr\xe9 Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
===============
Build targets
===============
Build targets.
"""
__author__ = "Andr\xe9 Malo"
__author__ = getattr(__author__, 'decode', lambda x: __author__)('latin-1')
__docformat__ = "restructuredtext en"
import os as _os
import re as _re
import sys as _sys
from _setup import dist
from _setup import shell
from _setup import make
from _setup import term
from _setup.make import targets
if _sys.version_info[0] == 3:
cfgread = dict(encoding='utf-8')
def textopen(*args):
return open(*args, **cfgread)
else:
textopen = open
cfgread = {}
class Target(make.Target):
def init(self):
self.dirs = {
'lib': '.',
'docs': 'docs',
'tests': 'tests',
'coverage': 'docs/coverage',
'apidoc': 'docs/apidoc',
'userdoc': 'docs/userdoc',
'userdoc_source': 'docs/_userdoc',
'userdoc_build': 'docs/_userdoc/_build',
'website': 'dist/website',
'_website': '_website', # source dir
'fake': '_pkg/fake',
'dist': 'dist',
'build': 'build',
'ebuild': '_pkg/ebuilds',
}
libpath = shell.native(self.dirs['lib'])
if libpath != _sys.path[0]:
while libpath in _sys.path:
_sys.path.remove(libpath)
_sys.path.insert(0, libpath)
self.ebuild_files = {
'wtf-alpha.ebuild.in':
'wtf-%(VERSION)s_alpha%(REV)s.ebuild',
}
self.manpages = (
'docs/wtfctl.1',
)
Manifest = targets.Manifest
class Distribution(targets.Distribution):
def init(self):
self._dist = 'dist'
self._ebuilds = '_pkg/ebuilds'
self._changes = 'docs/CHANGES'
class Check(Target):
""" Check the python code """
NAME = "check"
DEPS = ["compile-quiet"]
def run(self):
fake = shell.native(self.dirs['fake'])
if fake in _sys.path:
_sys.path.remove(fake)
_sys.path.insert(0, fake)
from _setup.dev import analysis
term.green('Linting wtf sources...')
res = analysis.pylint('pylintrc', 'wtf')
if res == 2:
make.warn('pylint not found', self.NAME)
class Test(Target):
""" Test the code """
NAME = "test"
DEPS = ["nose-test"]
class NoseTest(Target):
""" Run the nose tests """
NAME = "nose-test"
DEPS = ["compile-quiet"]
def run(self):
if shell.spawn(
'nosetests',
'-c', 'package.cfg',
self.dirs['tests'], self.dirs['lib']):
raise RuntimeError('tests failed')
def clean(self, scm, dist):
term.green("Removing coverage files...")
shell.rm_rf(self.dirs['coverage'])
shell.rm('.coverage')
class Compile(Target):
""" Compile the python code """
NAME = "compile"
#DEPS = None
def run(self):
import setup
_old_argv = _sys.argv
try:
_sys.argv = ['setup.py', '-q', 'build']
if not self.HIDDEN:
_sys.argv.remove('-q')
setup.setup()
if 'java' not in _sys.platform.lower():
_sys.argv = [
'setup.py', '-q', 'install_lib', '--install-dir',
shell.native(self.dirs['lib']),
'--optimize', '2',
]
if not self.HIDDEN:
_sys.argv.remove('-q')
setup.setup()
finally:
_sys.argv = _old_argv
for name in shell.files("%s/wtf" % self.dirs['lib'], '*.py'):
self.compile(name)
term.write("%(ERASE)s")
term.green("All files successfully compiled.")
def compile(self, name):
path = shell.native(name)
term.write("%(ERASE)s%(BOLD)s>>> Compiling %(name)s...%(NORMAL)s",
name=name)
from distutils import util
try:
from distutils import log
except ImportError:
util.byte_compile([path], verbose=0, force=True)
else:
log.set_verbosity(0)
util.byte_compile([path], force=True)
def clean(self, scm, dist):
term.green("Removing python byte code...")
for name in shell.dirs('.', '__pycache__'):
shell.rm_rf(name)
for name in shell.files('.', '*.py[co]'):
shell.rm(name)
for name in shell.files('.', '*$py.class'):
shell.rm(name)
term.green("Removing c extensions...")
for name in shell.files('.', '*.so'):
shell.rm(name)
for name in shell.files('.', '*.pyd'):
shell.rm(name)
shell.rm('wtf/util_private.h')
shell.rm_rf(self.dirs['build'])
class CompileQuiet(Compile):
NAME = "compile-quiet"
HIDDEN = True
def clean(self, scm, dist):
pass
class Doc(Target):
""" Build the docs (api + user) """
NAME = "doc"
DEPS = ['apidoc', 'userdoc']
class ApiDoc(Target):
""" Build the API docs """
NAME = "apidoc"
DEPS = ['compile-quiet']
def run(self):
from _setup.dev import apidoc
apidoc.epydoc(
prepend=[
shell.native(self.dirs['fake']),
shell.native(self.dirs['lib']),
],
env={'WTF_NO_C_OVERRIDE': '1', 'EPYDOC_INSPECTOR': '1'}
)
def clean(self, scm, dist):
if scm:
term.green("Removing apidocs...")
shell.rm_rf(self.dirs['apidoc'])
class UserDoc(Target):
""" Build the user docs """
NAME = "userdoc"
#DEPS = None
def run(self):
from _setup.dev import userdoc
userdoc.sphinx(
build=shell.native(self.dirs['userdoc_build']),
source=shell.native(self.dirs['userdoc_source']),
target=shell.native(self.dirs['userdoc']),
)
def clean(self, scm, dist):
if scm:
term.green("Removing userdocs...")
shell.rm_rf(self.dirs['userdoc'])
shell.rm_rf(self.dirs['userdoc_build'])
class Website(Target):
""" Build the website """
NAME = "website"
DEPS = ["apidoc"]
def run(self):
from _setup.util import SafeConfigParser as parser
parser = parser()
parser.read('package.cfg', **cfgread)
strversion = parser.get('package', 'version.number')
shortversion = tuple(map(int, strversion.split('.')[:2]))
shell.rm_rf(self.dirs['_website'])
shell.cp_r(
self.dirs['userdoc_source'],
_os.path.join(self.dirs['_website'], 'src')
)
shell.rm_rf(_os.path.join(self.dirs['_website'], 'build'))
shell.rm_rf(self.dirs['website'])
_os.makedirs(self.dirs['website'])
filename = _os.path.join(
self.dirs['_website'], 'src', 'website_download.txt'
)
fp = textopen(filename)
try:
download = fp.read()
finally:
fp.close()
filename = _os.path.join(self.dirs['_website'], 'src', 'index.txt')
fp = textopen(filename)
try:
indexlines = fp.readlines()
finally:
fp.close()
fp = textopen(filename, 'w')
try:
for line in indexlines:
if line.startswith('.. placeholder: Download'):
line = download
fp.write(line)
finally:
fp.close()
shell.cp_r(
self.dirs['apidoc'],
_os.path.join(self.dirs['website'], 'doc-%d.%d' % shortversion)
)
shell.cp_r(
self.dirs['apidoc'],
_os.path.join(
self.dirs['_website'], 'src', 'doc-%d.%d' % shortversion
)
)
fp = textopen(_os.path.join(
self.dirs['_website'], 'src', 'conf.py'
), 'a')
try:
fp.write("\nepydoc = dict(wtf=%r)\n" % (
_os.path.join(
shell.native(self.dirs['_website']),
"src",
"doc-%d.%d" % shortversion,
),
))
fp.write("\nexclude_trees.append(%r)\n" %
"doc-%d.%d" % shortversion
)
finally:
fp.close()
from _setup.dev import userdoc
userdoc.sphinx(
build=shell.native(_os.path.join(self.dirs['_website'], 'build')),
source=shell.native(_os.path.join(self.dirs['_website'], 'src')),
target=shell.native(self.dirs['website']),
)
shell.rm(_os.path.join(self.dirs['website'], '.buildinfo'))
def clean(self, scm, dist):
if scm:
term.green("Removing website...")
shell.rm_rf(self.dirs['website'])
shell.rm_rf(self.dirs['_website'])
class PreCheck(Target):
""" Run clean, doc, check """
NAME = "precheck"
DEPS = ["clean", "doc", "check", "test"]
class SVNRelease(Target):
""" Release current version """
#NAME = "release"
DEPS = None
def run(self):
self._check_committed()
self._update_versions()
self._tag_release()
self.runner('dist', seen={})
def _tag_release(self):
""" Tag release """
from _setup.util import SafeConfigParser as parser
parser = parser()
parser.read('package.cfg', **cfgread)
strversion = parser.get('package', 'version.number')
isdev = parser.getboolean('package', 'version.dev')
revision = parser.getint('package', 'version.revision')
version = strversion
if isdev:
version += '-dev-r%d' % (revision,)
trunk_url = self._repo_url()
if not trunk_url.endswith('/trunk'):
rex = _re.compile(r'/branches/\d+(?:\.\d+)*\.[xX]$').search
match = rex(trunk_url)
if not match:
make.fail("Not in trunk or release branch!")
found = match.start(0)
else:
found = -len('/trunk')
release_url = trunk_url[:found] + '/releases/' + version
svn = shell.frompath('svn')
shell.spawn(
svn, 'copy', '-m', 'Release version ' + version, '--',
trunk_url, release_url,
echo=True,
)
def _update_versions(self):
""" Update versions """
self.runner('revision', 'version', seen={})
svn = shell.frompath('svn')
shell.spawn(svn, 'commit', '-m', 'Pre-release: version update',
echo=True
)
def _repo_url(self):
""" Determine URL """
from xml.dom import minidom
svn = shell.frompath('svn')
info = minidom.parseString(
shell.spawn(svn, 'info', '--xml', stdout=True)
)
try:
url = info.getElementsByTagName('url')[0]
text = []
for node in url.childNodes:
if node.nodeType == node.TEXT_NODE:
text.append(node.data)
finally:
info.unlink()
return ''.join(text).encode('utf-8')
def _check_committed(self):
""" Check if everything is committed """
if not self._repo_url().endswith('/trunk'):
rex = _re.compile(r'/branches/\d+(?:\.\d+)*\.[xX]$').search
match = rex(self._repo_url())
if not match:
make.fail("Not in trunk or release branch!")
svn = shell.frompath('svn')
lines = shell.spawn(svn, 'stat', '--ignore-externals',
stdout=True, env=dict(_os.environ, LC_ALL='C'),
).splitlines()
for line in lines:
if line.startswith('X'):
continue
make.fail("Uncommitted changes!")
class GitRelease(Target):
""" Release current version """
#NAME = "release"
DEPS = None
def run(self):
self._check_committed()
self._update_versions()
self._tag_release()
self.runner('dist', seen={})
def _tag_release(self):
""" Tag release """
from _setup.util import SafeConfigParser as parser
parser = parser()
parser.read('package.cfg', **cfgread)
strversion = parser.get('package', 'version.number')
isdev = parser.getboolean('package', 'version.dev')
revision = parser.getint('package', 'version.revision')
version = strversion
if isdev:
version += '-dev-r%d' % (revision,)
git = shell.frompath('git')
shell.spawn(
git, 'tag', '-a', '-m', 'Release version ' + version, '--',
version,
echo=True,
)
def _update_versions(self):
""" Update versions """
self.runner('revision', 'version', seen={})
git = shell.frompath('git')
shell.spawn(git, 'commit', '-a', '-m', 'Pre-release: version update',
echo=True
)
def _check_committed(self):
""" Check if everything is committed """
git = shell.frompath('git')
lines = shell.spawn(git, 'branch', '--color=never',
stdout=True, env=dict(_os.environ, LC_ALL='C')
).splitlines()
for line in lines:
if line.startswith('*'):
branch = line.split(None, 1)[1]
break
else:
make.fail("Could not determine current branch.")
if branch != 'master':
rex = _re.compile(r'^\d+(?:\.\d+)*\.[xX]$').match
match = rex(branch)
if not match:
make.fail("Not in master or release branch.")
lines = shell.spawn(git, 'status', '--porcelain',
stdout=True, env=dict(_os.environ, LC_ALL='C'),
)
if lines:
make.fail("Uncommitted changes!")
class Release(GitRelease):
NAME = "release"
#DEPS = None
class SVNRevision(Target):
""" Insert the svn revision into all relevant files """
#NAME = "revision"
#DEPS = None
def run(self):
revision = self._revision()
self._revision_cfg(revision)
def _revision(self):
""" Find SVN revision """
rev = shell.spawn(shell.frompath('svnversion'), '.', stdout=True)
rev = rev.strip()
if ':' in rev:
rev = rev.split(':')[1]
try:
rev = int(rev)
except ValueError:
try:
rev = int(rev[:-1])
except ValueError:
make.fail("No clean revision found (%s)" % rev)
return rev
def _revision_cfg(self, revision):
""" Modify version in package.cfg """
filename = 'package.cfg'
fp = textopen(filename)
try:
initlines = fp.readlines()
finally:
fp.close()
fp = textopen(filename, 'w')
replaced = False
try:
for line in initlines:
if line.startswith('version.revision'):
line = 'version.revision = %d\n' % (revision,)
replaced = True
fp.write(line)
finally:
fp.close()
assert replaced, "version.revision not found in package.cfg"
class SimpleRevision(Target):
""" Update the revision number and insert into all relevant files """
#NAME = "revision"
#DEPS = None
def run(self):
self._revision_cfg()
def _revision_cfg(self):
""" Modify version in package.cfg """
filename = 'package.cfg'
fp = textopen(filename)
try:
initlines = fp.readlines()
finally:
fp.close()
fp = textopen(filename, 'w')
revision, replaced = None, False
try:
for line in initlines:
if line.startswith('version.revision'):
if revision is None:
revision = int(line.split('=', 1)[1].strip() or 0, 10)
revision += 1
line = 'version.revision = %d\n' % (revision,)
replaced = True
fp.write(line)
finally:
fp.close()
assert replaced, "version.revision not found in package.cfg"
GitRevision = SimpleRevision
class Revision(GitRevision):
""" Insert the revision into all relevant files """
NAME = "revision"
#DEPS = None
class Version(Target):
""" Insert the program version into all relevant files """
NAME = "version"
#DEPS = None
def run(self):
from _setup.util import SafeConfigParser as parser
parser = parser()
parser.read('package.cfg', **cfgread)
strversion = parser.get('package', 'version.number')
isdev = parser.getboolean('package', 'version.dev')
revision = parser.getint('package', 'version.revision')
self._version_init(strversion, isdev, revision)
self._version_userdoc(strversion, isdev, revision)
self._version_download(strversion, isdev, revision)
self._version_changes(strversion, isdev, revision)
parm = {'VERSION': strversion, 'REV': revision}
for src, dest in self.ebuild_files.items():
src = "%s/%s" % (self.dirs['ebuild'], src)
dest = "%s/%s" % (self.dirs['ebuild'], dest % parm)
term.green("Creating %(name)s...", name=dest)
shell.cp(src, dest)
def _version_init(self, strversion, isdev, revision):
""" Modify version in __init__ """
filename = _os.path.join(
self.dirs['lib'], 'wtf', '__init__.py'
)
fp = textopen(filename)
try:
initlines = fp.readlines()
finally:
fp.close()
fp = textopen(filename, 'w')
replaced = False
try:
for line in initlines:
if line.startswith('__version__'):
line = '__version__ = (%r, %r, %r)\n' % (
strversion, isdev, revision
)
replaced = True
fp.write(line)
finally:
fp.close()
assert replaced, "__version__ not found in __init__.py"
def _version_changes(self, strversion, isdev, revision):
""" Modify version in changes """
filename = _os.path.join(shell.native(self.dirs['docs']), 'CHANGES')
if isdev:
strversion = "%s-dev-r%d" % (strversion, revision)
fp = textopen(filename)
try:
initlines = fp.readlines()
finally:
fp.close()
fp = textopen(filename, 'w')
try:
for line in initlines:
if line.rstrip() == "Changes with version":
line = "%s %s\n" % (line.rstrip(), strversion)
fp.write(line)
finally:
fp.close()
def _version_userdoc(self, strversion, isdev, revision):
""" Modify version in userdoc """
filename = _os.path.join(self.dirs['userdoc_source'], 'conf.py')
shortversion = '.'.join(strversion.split('.')[:2])
longversion = strversion
if isdev:
longversion = "%s-dev-r%d" % (strversion, revision)
fp = textopen(filename)
try:
initlines = fp.readlines()
finally:
fp.close()
replaced = 0
fp = textopen(filename, 'w')
try:
for line in initlines:
if line.startswith('version'):
line = 'version = %r\n' % shortversion
replaced |= 1
elif line.startswith('release'):
line = 'release = %r\n' % longversion
replaced |= 2
fp.write(line)
finally:
fp.close()
assert replaced & 3 != 0, "version/release not found in conf.py"
def _version_download(self, strversion, isdev, revision):
""" Modify version in website download docs """
filename = _os.path.join(
self.dirs['userdoc_source'], 'website_download.txt'
)
dllines, VERSION, PATH = [], strversion, ''
if isdev:
oldstable = []
hasstable = False
try:
fp = open(filename)
except IOError, e:
if e[0] != _errno.ENOENT:
raise
else:
try:
for line in fp:
if line.startswith('.. begin stable'):
hasstable = True
oldstable.append(line)
finally:
fp.close()
if hasstable:
dllines = oldstable
else:
VERSION = "%s-dev-%s" % (strversion, revision)
PATH='dev/'
newdev = []
fp = textopen(filename + '.in')
try:
if dllines:
for line in fp:
if newdev:
newdev.append(line)
if line.startswith('.. end dev'):
break
elif line.startswith('.. begin dev'):
newdev.append(line)
else:
raise AssertionError("Incomplete dev marker")
else:
dllines = fp.readlines()
finally:
fp.close()
instable, indev = [], []
fp = textopen(filename, 'w')
try:
for line in dllines:
if instable:
instable.append(line)
if line.startswith('.. end stable'):
if not isdev:
res = (''.join(instable)
.replace('@@VERSION@@', strversion)
.replace('@@PATH@@', '')
)
elif not hasstable:
res = ''
else:
res = ''.join(instable)
fp.write(res)
instable = []
elif indev:
indev.append(line)
if line.startswith('.. end dev'):
if isdev:
if newdev:
indev = newdev
fp.write(''.join(indev)
.replace('@@DEVVERSION@@', "%s-dev-r%d" % (
strversion, revision
))
.replace('@@PATH@@', 'dev/')
)
else:
fp.write(''.join([indev[0], indev[-1]]))
indev = []
elif line.startswith('.. begin stable'):
instable.append(line)
elif line.startswith('.. begin dev'):
indev.append(line)
elif isdev and hasstable:
fp.write(line)
else:
fp.write(line
.replace('@@VERSION@@', VERSION)
.replace('@@PATH@@', PATH)
)
finally:
fp.close()
def _version_man(self, strversion, isdev, revision):
""" Modify version in man pages """
import datetime
shortversion = '.'.join(strversion.split('.')[:2])
for filename in self.manpages:
fp = textopen(filename)
try:
initlines = fp.readlines()
finally:
fp.close()
fp = textopen(filename, 'w')
replaced = 0
try:
for line in initlines:
if line.startswith('.TH'):
splitted = line.split('"')
splitted[3] = str(datetime.date.today().year)
splitted[5] = ' '.join(
splitted[5].split(' ')[:-1] + [shortversion]
)
line = '"'.join(splitted)
replaced = 1
fp.write(line)
finally:
fp.close()
assert replaced, ".TH line not found in %r" % (filename,)
def clean(self, scm, dist):
""" Clean versioned files """
if scm:
term.green("Removing generated ebuild files")
for name in shell.files(self.dirs['ebuild'], '*.ebuild'):
shell.rm(name)
make.main(name=__name__)