-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmake.pbj
executable file
·143 lines (121 loc) · 3.98 KB
/
make.pbj
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
#!/usr/bin/env python
from pbj import Builder, cmd, PBJFailed
import os
import glob
import re
import traceback
import pjs
from pjs import PJsNotImplemented
build = Builder('PJs')
def get_lineno(text):
def meta(match):
lno = len(text[:match.start()].split('\n'))
return match.group()[:-1] + ' // %d :builtin:\n' % lno
return meta
@build.file('pjs/data/pjslib.js', depends='pjs/jslib/*.js')
def lib():
print 'Javascript Tests'
files = ('pjs/jslib/functions.js', 'pjs/jslib/classes.js', 'pjs/jslib/modules.js', 'pjs/jslib/__builtin__.js')
text = '\n'.join(open(fname).read() for fname in files)
text = re.sub('function(\s+[$\w-]+)?\s*\(([$\w_]+,\s*)*[$\w_]*\s*\)\s*{\s*\n', get_lineno(text), text)
open('pjs/data/pjslib.js', 'w').write(text)
build.cmd('jstest', ('rhino', 'test/runtests.js'), depends='@lib', always=True)
build.clean('build', 'test/py/*.js')
@build.target(depends='@lib', always=True, completion=glob.glob('test/pjs/*.py'))
def pjstest(fname='test/pjs/*.py'):
print 'PJs Tests'
files = glob.glob(fname)
for fname in files:
justjs(fname)
@build.target(depends='@lib', always=True, completion=glob.glob('test/py/*.py'))
def pytest(one=None, keep=False):
print 'Python Tests'
if one is not None:
files = [one]
else:
files = glob.glob('test/py/*.py')
for fname in files:
compare(fname, keep=keep)
py2js_files = glob.glob('test/py2js/*.py') + glob.glob('test/py2js/*/*.py') + glob.glob('test/py2js/*/*/*.py')
@build.target(depends='@lib', always=True, completion=py2js_files)
def py2jstest(one=None, full=True):
if one is not None:
if os.path.isdir(one):
files = glob.glob(os.path.join(one, '*.py'))
else:
files = [one]
else:
files = py2js_files
for fname in files:
compare(fname, full)
def justjs(fname):
jsname = fname.replace('.py', '.js')
try:
open(jsname, 'w').write(pjs.compile(fname, 'js', lib_dir='pjs/data', rhino=True))
except PJsNotImplemented, e:
print 'XFAIL %s :: %s' % (fname, e)
return
except Exception, e:
print 'FAILED %s :: conversion error:' % fname
print traceback.format_exc()
return
jo, je = cmd('rhino %s' % jsname, shell=True)
if je:
print 'FAILED %s :: javascript error:\n%s' % (fname, je)
print jo
return
print 'PASSED %s' % fname
os.unlink(jsname)
def compare(fname, full=False, keep=False):
jsname = fname.replace('.py', '.js')
o,e = cmd('python', os.path.basename(fname), cwd = os.path.dirname(fname))
if e:
print 'FAILED %s :: python error:\n%s' % (fname, e)
return
try:
open(jsname, 'w').write(pjs.compile(fname, 'js', lib_dir='pjs/data', rhino=True))
except (PJsNotImplemented, SyntaxError), e:
print 'XFAIL %s :: %s' % (fname, e)
return
except Exception, e:
print 'FAILED %s :: conversion error:' % fname
print traceback.format_exc()
return
jo, je = cmd('rhino %s' % jsname, shell=True)
if je:
if 'NotImplementedError' in je:
print 'XFAIL :: not implemented :: ', je
return
print 'FAILED %s :: javascript error:\n%s' % (fname, je)
print jo
return
if o != jo:
print 'FAILED %s :: different output:\n' % fname
if full:
print o
print '-'*20
print jo
print '-'*20
print diff(o, jo)
return
print 'PASSED %s' % fname
if not keep:
os.unlink(jsname)
@build.target(depends=('@jstest', '@pytest', '@pjstest'), always=True)
def test():
pass
from tempfile import NamedTemporaryFile as ntf
def diff(a, b):
af = ntf(delete=False)
af.write(a)
af.close()
bf = ntf(delete=False)
bf.write(b)
bf.close()
o, e = cmd('diff', af.name, bf.name)
os.unlink(af.name)
os.unlink(bf.name)
return o
if __name__ == '__main__':
build.run()
# vim: et sw=4 sts=4