forked from serge-sans-paille/pythran
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
246 lines (204 loc) · 9.05 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
from distutils.core import setup, Command
from distutils.command.build import build
from unittest import TextTestRunner, TestLoader
import os
import sys
import shutil
from subprocess import check_call
from pythran import __version__
def _exclude_current_dir_from_import():
""" Prevents Python loading from current directory, so that
`import pythran` lookup the PYTHONPATH.
Returns current_dir
"""
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path = filter(lambda p: p != current_dir, sys.path)
return current_dir
class BuildWithPly(build):
'''Use ply to generate parsetab before building module.'''
def build_ply(self):
from pythran.spec import SpecParser
SpecParser() # this forces the generation of the parsetab file
self.mkpath(os.path.join(self.build_lib, 'pythran'))
for p in ('parsetab.py',):
target = os.path.join(self.build_lib, 'pythran', p)
if os.path.exists(p):
os.rename(p, target)
assert os.path.exists(target)
def build_nt2(self):
nt2_dir = 'nt2'
if not os.path.isdir(nt2_dir):
print 'nt2 git repository not setup, cloning it'
cmd = 'git clone https://github.com/MetaScale/nt2.git -b release'
check_call(cmd.split())
# retreive previous nt2 release
os.chdir('nt2')
check_call('git reset --hard 9024abe687'.split())
os.chdir('..')
nt2_build_dir = os.path.join(self.build_temp, nt2_dir)
if not os.path.isdir(nt2_build_dir):
os.makedirs(nt2_build_dir)
if not os.path.exists(os.path.join(nt2_build_dir, 'Makefile')):
print 'nt2 not already configured, configuring it'
cwd = os.getcwd()
abs_nt2_dir = os.path.join(cwd, nt2_dir)
os.chdir(nt2_build_dir)
build_cmd = ['cmake',
abs_nt2_dir,
'-DCMAKE_INSTALL_PREFIX=.',
'-DNT2_FIND_REPOSITORIES='
'git://github.com/MetaScale/nt2-modules.git']
check_call(build_cmd)
os.chdir(cwd)
check_call(['make', '-C', nt2_build_dir, 'install', '-j'])
for d in ('nt2', 'boost'):
src = os.path.join(nt2_build_dir, 'include', d)
# copy to the build tree
target = os.path.join(self.build_lib, 'pythran', d)
shutil.rmtree(target, True)
shutil.copytree(src, target)
# copy them to the source tree too, needed for sdist
target = os.path.join('pythran', d)
shutil.rmtree(target, True)
shutil.copytree(src, target)
def run(self, *args, **kwargs):
if not self.dry_run: # compatibility with the parent options
self.build_ply()
self.build_nt2()
# regular build done by parent class
build.run(self, *args, **kwargs)
class TestCommand(Command):
'''Scan the test directory for any tests, and run them.'''
description = 'run the test suite for the package'
user_options = [('failfast', None, 'Stop upon first fail'),
('cov', None, 'Perform coverage analysis'),
('num-threads=', None, 'Number of thread to execute tests')]
def initialize_options(self):
self.failfast = False
self.cov = False
import multiprocessing
self.num_threads = multiprocessing.cpu_count()
def finalize_options(self):
pass
def run(self):
# Do not include current directory, validate using installed pythran
current_dir = _exclude_current_dir_from_import()
os.chdir("pythran/tests")
where = os.path.join(current_dir, 'pythran')
from pythran import test_compile
test_compile()
try:
import py
import xdist
args = ["-n", str(self.num_threads), where, '--pep8']
if self.failfast:
args.insert(0, '-x')
if self.cov:
try:
import pytest_cov
args = ["--cov-report", "html",
"--cov-report", "annotate",
"--cov", "pythran"] + args
except ImportError:
print ("W: Skipping coverage analysis, pytest_cov"
"not found")
if py.test.cmdline.main(args) == 0:
print "\\_o<"
except ImportError:
print ("W: Using only one thread, "
"try to install pytest-xdist package")
loader = TestLoader()
t = TextTestRunner(failfast=self.failfast)
t.run(loader.discover(where))
if t.wasSuccessful():
print "\\_o<"
class BenchmarkCommand(Command):
'''Scan the test directory for any runnable test, and benchmark them.'''
default_nb_iter = 11
description = 'run the benchmark suite for the package'
user_options = [
('nb-iter=', None,
'number of times the benchmark is run'
'(default={0})'.format(default_nb_iter)),
('mode=', None,
'mode to use (cpython, pythran, pythran' '+omp)')
]
runas_marker = '#runas '
def __init__(self, *args, **kwargs):
Command.__init__(self, *args, **kwargs)
def initialize_options(self):
self.nb_iter = BenchmarkCommand.default_nb_iter
self.parallel = False
self.mode = "pythran"
def finalize_options(self):
self.nb_iter = int(self.nb_iter)
def run(self):
import glob
import timeit
# Do not include current directory, validate using installed pythran
current_dir = _exclude_current_dir_from_import()
os.chdir("pythran/tests")
where = os.path.join(current_dir, 'pythran', 'tests', 'cases')
from pythran import test_compile, compile_pythranfile
test_compile()
candidates = glob.glob(os.path.join(where, '*.py'))
sys.path.append(where)
median = lambda x: sorted(x)[len(x) / 2]
for candidate in candidates:
with file(candidate) as content:
runas = [line for line in content.readlines()
if line.startswith(BenchmarkCommand.runas_marker)]
if runas:
modname, _ = os.path.splitext(os.path.basename(candidate))
runas_commands = runas[0].replace(
BenchmarkCommand.runas_marker, '').split(";")
runas_context = ";".join(["import {0}".format(
modname)] + runas_commands[:-1])
runas_command = modname + '.' + runas_commands[-1]
# cleaning
sopath = os.path.splitext(candidate)[0] + ".so"
if os.path.exists(sopath):
os.remove(sopath)
ti = timeit.Timer(runas_command, runas_context)
# pythran part
if self.mode.startswith('pythran'):
cxxflags = ["-Ofast", "-DNDEBUG"]
if self.mode == "pythran+omp":
cxxflags.append("-fopenmp")
compile_pythranfile(candidate,
cxxflags=cxxflags)
print modname + " running ...",
sys.stdout.flush()
timing = median(ti.repeat(self.nb_iter, number=1))
print timing
else:
print "* Skip '" + candidate + ', no #runas directive'
# Cannot use glob here, as the files may not be genrated yet
nt2_headers = (['nt2/' + '*/' * i + '*.hpp' for i in range(1, 20)] +
['boost/' + '*/' * i + '*.hpp' for i in range(1, 20)])
setup(name='pythran',
version=__version__,
description='a claimless python to c++ converter',
author='Serge Guelton',
author_email='serge.guelton@telecom-bretagne.eu',
url='https://github.com/serge-sans-paille/pythran',
packages=['pythran', 'omp', 'pythran/pythonic'],
package_data={'pythran': ['pythran.cfg'] + nt2_headers,
'pythran/pythonic': ['*.hpp', '*/*.hpp']},
scripts=['scripts/pythran', 'scripts/pythran-config'],
classifiers=['Development Status :: 4 - Beta',
'Environment :: Console',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: C++',
'Topic :: Software Development :: Code Generators'],
license="BSD 3-Clause",
requires=['ply (>=3.4)', 'networkx (>=1.5)', 'numpy', 'colorlog'],
cmdclass={'build': BuildWithPly,
'test': TestCommand,
'bench': BenchmarkCommand}
)