forked from pyne/pyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·521 lines (453 loc) · 17.4 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
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
#!/usr/bin/env python
"""Welcome to PyNE's setup.py script. This is a little non-standard because pyne
is a multilanguage projects. Still this script follows a predicatable ordering:
1. Parse command line arguments,
2. Call cmake from the 'build' directory
3. Call make from the 'build' directory
4. Use distuitls/setuptools from the 'build' directory
This gives us the best of both worlds. Compiled code is installed with cmake/make
and Cython/Python code is installed with normal Python tools. The only trick here is
how the various command line arguments are handed off to the three sub-processes.
To acomplish this we use argparser groups to group command line arguments based on
whether they go to:
1. the setup() function,
2. cmake,
3. make, or
4. other - typically used for args that apply to multiple other groups or
modify the environment in some way.
To add a new command line argument, first add it to the appropriate group in the
``parse_args()`` function. Then, modify the logic in the cooresponding
``parse_setup()``, ``parse_cmake()``, ``parse_make()``, or ``parse_others()``
functions to consume your new command line argument. It is OK for more than
one of the parser functions to comsume the argument. Where appropriate,
ensure the that argument is appended to the argument list that is returned by these
functions.
"""
from __future__ import print_function
import io
import os
import re
import sys
import imp
import shutil
import tarfile
import argparse
import platform
import warnings
import subprocess
from glob import glob
from distutils import core, dir_util, sysconfig
from contextlib import contextmanager
if sys.version_info[0] < 3:
from urllib import urlopen
else:
from urllib.request import urlopen
try:
from setuptools import setup as _setup
have_setuptools = True
except ImportError:
from distutils.core import setup as _setup
have_setuptools = False
import numpy as np
# import src into pythonpath - needed to actually run decaygen/atomicgen
if '.' not in sys.path:
sys.path.append(os.getcwd()+'/src')
# Thanks to http://patorjk.com/software/taag/
# and http://www.chris.com/ascii/index.php?art=creatures/dragons
# for ASCII art inspiriation
pyne_logo = """\
/ \
_ ) (( )) (
(@) /|\ ))_(( /|\
|-| / | \ (/\|/\) / | \ (@)
| | -------------------/--|-voV---\`|'/--Vov-|--\---------------------|-|
|-| '^` (o o) '^` | |
| | `\Y/' |-|
|-| | |
| | /\ ___ __ __ /\ |-|
|-| /^~\ / _ \_ _ /\ \ \/__\ /^~\ | |
| | /^~\ / /_)/ | | |/ \/ /_\ /^~\ |-|
|-| /^~\ / ___/| |_| / /\ //__ /^~\ | |
| | ^||` \/ \__, \_\ \/\__/ ^||` |-|
|-| || |____/ || | |
| | ==== ==== |-|
|-| | |
| | |-|
|-|___________________________________________________________________| |
(@) l /\ / ( ( \ /\ l `\|-|
l / V \ \ V \ l (@)
l/ _) )_ \I
`\ /'
`
"""
VERSION = '0.5.0-rc1'
IS_NT = os.name == 'nt'
CMAKE_BUILD_TYPES = {
'none': 'None',
'debug': 'Debug',
'release': 'Release',
'relwithdebinfo': 'RelWithDebInfo',
'minsizerel': 'MinSizeRel',
}
@contextmanager
def indir(path):
orig = os.getcwd()
os.chdir(path)
yield
os.chdir(orig)
@contextmanager
def cleanpypath(path):
orig = sys.path
sys.path = [p for p in sys.path if p != path]
yield
sys.path = orig
def assert_np_version():
low = (1, 8)
v = np.version.short_version
cur = tuple(map(int, v.split('.')[:2]))
if cur < low:
msg = "numpy version too low! {0} (have) < 1.8.0 (min)".format(v)
raise ValueError(msg)
def assert_ipython_version():
try:
import IPython
except ImportError:
return
low = (1, 2, 1)
v = IPython.__version__.split('-')[0]
cur = tuple(map(int, v.split('.')))
if cur < low:
msg = "ipython version is too low! {0} (have) < 2.0.0 (min)".format(v)
raise ValueError(msg)
def assert_ubuntu_version():
v = platform.uname()
for itm in v:
if 'precise' in itm:
msg = ("ubuntu 12/precise packages may be outdated, it is highly "
"recommended to update to ubuntu 14 LTS.")
warnings.warn(msg, Warning)
def assert_dep_versions():
assert_np_version()
assert_ubuntu_version()
assert_ipython_version()
DECAY_H = os.path.join('src', 'decay.h')
DECAY_CPP = os.path.join('src', 'decay.cpp')
DECAY_H_REP = os.path.join('src', '_decay.h')
DECAY_CPP_REP = os.path.join('src', '_decay.cpp')
DECAY_URL = 'http://data.pyne.io/decay.tar.gz'
def download_decay():
print('Downloading ' + DECAY_URL)
try:
durl = urlopen(DECAY_URL)
d = durl.read()
durl.close()
except IOError:
print('...failed!')
return False
f = io.BytesIO(d)
tar = tarfile.open(fileobj=f, mode='r:gz')
tar.extractall('src')
tar.close()
durl.close()
return True
local_ensdf_evaluators = ['alphad', 'delta', 'gtol', 'hsicc', 'hsmrg', 'seqhst',
'logft', 'radd', 'ruler']
local_ensdf_tools = [['ensdf_processing/RADD/98AK04.in', '98AK04.in'],
['ensdf_processing/RADD/ELE.in', 'ELE.in']]
def copy_ensdf_executables(exe_dest):
print('Copying ENSDF Executables to install directory')
# Hack for copying the executables the first time PyNE is instealled, before
# pyne has been added to the python path.
if exe_dest[-4:] != 'pyne':
exe_dest = sysconfig.get_python_lib()
for f in os.listdir(sysconfig.get_python_lib()):
if re.match('pyne', f):
exe_dest = exe_dest + '/' + f
exe_dest = exe_dest + '/pyne'
for tool in local_ensdf_evaluators:
try:
local_path = os.path.join('build',os.path.join('src',tool))
dest_path = os.path.join(exe_dest, tool)
shutil.copy(local_path, dest_path)
except Exception:
print('Some ENSDF processing executables were unable to be copied to the \
install directory.')
for tool in local_ensdf_tools:
try:
local_path = os.path.join('src', tool[0])
dest_path = os.path.join(exe_dest, tool[1])
shutil.copy(local_path, dest_path)
except Exception:
print('Some ENSDF processing executables were unable to be copied to the \
install directory.')
def generate_decay():
with indir('src'):
try:
import decaygen
except ImportError:
return False
try:
decaygen.build()
except Exception:
return False
return True
def ensure_decay():
mb = 1024**2
if os.path.isfile(DECAY_H) and os.path.isfile(DECAY_CPP) and \
os.stat(DECAY_CPP).st_size > mb:
return
generated = generate_decay()
if generated:
return
downloaded = download_decay()
if downloaded:
return
print('!'*42)
print('Decay files could not be downloaded or generated, using surrogates instead.')
print('Please consider using the --bootstrap command line argument.')
print('!'*42 + '\n')
shutil.copy(DECAY_H_REP, DECAY_H)
shutil.copy(DECAY_CPP_REP, DECAY_CPP)
ATOMIC_H = os.path.join('src', 'atomic_data.h')
ATOMIC_CPP = os.path.join('src', 'atomic_data.cpp')
ATOMIC_H_UNDER = os.path.join('src', '_atomic_data.h')
ATOMIC_CPP_UNDER = os.path.join('src', '_atomic_data.cpp')
def generate_atomic():
with indir('src'):
try:
import atomicgen
except ImportError:
return False
try:
atomicgen.build()
except Exception:
return False
return True
def ensure_atomic():
mb = 1024**2
# if the file exists then we're done!
if os.path.isfile(ATOMIC_H) and os.path.isfile(ATOMIC_CPP) and \
os.stat(ATOMIC_CPP).st_size > mb:
return
# generate the data
generated = generate_atomic()
if generated:
return
# last resort - if generate atomic failed, use the backup
if not os.path.isfile(ATOMIC_H) and not os.path.isfile(ATOMIC_CPP):
shutil.copy(ATOMIC_H_UNDER, ATOMIC_H)
shutil.copy(ATOMIC_CPP_UNDER, ATOMIC_CPP)
else:
# copy the freshly generated file to the last resort for consistency
shutil.copy(ATOMIC_H, ATOMIC_H_UNDER)
shutil.copy(ATOMIC_CPP, ATOMIC_CPP_UNDER)
def ensure_nuc_data():
import tempfile
tdir = tempfile.gettempdir()
with cleanpypath('.'), cleanpypath(os.getcwd()), indir(tdir):
from pyne.dbgen import nuc_data_make
from pyne.dbgen.api import build_dir
bdir = os.path.join(os.getcwd(), 'build', build_dir)
nuc_data_make.main(args=['-b', bdir])
def parse_setup(ns):
a = [sys.argv[0], ns.cmd]
if ns.user:
a.append('--user')
if ns.prefix is not None:
a.append('--prefix=' + ns.prefix)
if ns.egg_base is not None:
local_path = os.path.dirname(os.path.abspath(sys.argv[0]))
a.append('--egg-base=' + os.path.join(local_path, ns.egg_base))
if ns.cmd == 'clean':
if os.path.exists('build'):
dir_util.remove_tree('build')
print('build directory cleaned ... exiting')
sys.exit()
if ns.clean:
if os.path.exists('build'):
dir_util.remove_tree('build')
return a
def parse_cmake(ns):
a = []
if ns.D is not None:
a += ['-D' + x for x in ns.D]
if ns.build_type is not None:
a.append('-DCMAKE_BUILD_TYPE=' + CMAKE_BUILD_TYPES[ns.build_type.lower()])
if ns.prefix is not None:
a.append('-DCMAKE_INSTALL_PREFIX=' + ns.prefix)
if have_setuptools:
a.append('-DHAVE_SETUPTOOLS=TRUE')
return a
def parse_make(ns):
a = []
if ns.j is not None:
a.append('-j' + ns.j)
return a
def parse_others(ns):
if ns.hdf5 is not None:
os.environ['HDF5_ROOT'] = ns.hdf5
if ns.moab is not None:
os.environ['MOAB_ROOT'] = ns.moab
if ns.clean:
if os.path.isfile(DECAY_H):
os.remove(DECAY_H)
if os.path.isfile(DECAY_CPP):
os.remove(DECAY_CPP)
def parse_args():
argv = [a for a in sys.argv[1:] if a != '--'] # needed for backwards compat.
parser = argparse.ArgumentParser()
setup = parser.add_argument_group('setup', 'Group for normal setup.py arguments')
setup.add_argument('cmd', help="command to send to normal setup, e.g. "
"install or build.")
parser.add_argument('--clean', nargs='?', const=True, default=False)
parser.add_argument('--user', nargs='?', const=True, default=False)
parser.add_argument('--egg-base')
cmake = parser.add_argument_group('cmake', 'Group for CMake arguments.')
cmake.add_argument('-D', metavar='VAR', action='append',
help='Set enviornment variable.')
cmake.add_argument('--build-type', metavar='BT',
help='Set build type via CMAKE_BUILD_TYPE, '
'e.g. Release or Debug.')
make = parser.add_argument_group('make', 'Group for make arguments.')
make.add_argument('-j', help='Degree of parallelism for build.')
other = parser.add_argument_group('other', 'Group for miscellaneous arguments.')
other.add_argument('--hdf5', help='Path to HDF5 root directory.')
other.add_argument('--moab', help='Path to MOAB root directory.')
other.add_argument('--prefix', help='Prefix for install location.')
other.add_argument('--bootstrap', default=False, action='store_true',
help='Bootstraps the PyNE installation, including '
'nuc_data_make and possibly decaygen.')
ns = parser.parse_args(argv)
sys.argv = parse_setup(ns)
cmake_args = parse_cmake(ns)
make_args = parse_make(ns)
parse_others(ns)
return cmake_args, make_args, ns.bootstrap
def setup():
scripts = [os.path.join('scripts', f) for f in os.listdir('scripts')]
scripts = [s for s in scripts if (os.name == 'nt' and s.endswith('.bat'))
or (os.name != 'nt' and
not s.endswith('.bat'))]
packages = ['pyne', 'pyne.dbgen', 'pyne.apigen', 'pyne.xs',
'pyne.transmute', 'pyne.gui', 'pyne.cli']
pack_dir = {
'pyne': 'pyne',
'pyne.xs': 'pyne/xs',
'pyne.gui': 'pyne/gui',
'pyne.cli': 'pyne/cli',
'pyne.dbgen': 'pyne/dbgen',
'pyne.apigen': 'pyne/apigen',
'pyne.transmute': 'pyne/transmute',
}
extpttn = ['*.dll', '*.so', '*.dylib', '*.pyd', '*.pyo']
pack_data = {
'lib': extpttn,
'pyne': ['*.pxd', 'include/*.h', 'include/*.pxi', 'include/*/*.h',
'*.inp', 'include/*/*/*.h', 'include/*/*/*/*.h', '*.json',
'_includes/*.txt', '_includes/*.pxd', '_includes/*/*',
'_includes/*/*/*'] + extpttn,
'pyne.xs': ['*.pxd'] + extpttn,
'pyne.gui': ['*.pyw'],
'pyne.dbgen': ['*.html', '*.csv', 'abundances.txt', 'mass.mas12'],
}
libpynes = set()
for ext in extpttn:
libpynes |= set(glob('src/' + ext))
data_files = [
('lib', libpynes),
('include/pyne', glob('../src/*.h')),
]
setup_kwargs = {
"name": "pyne",
"version": VERSION,
"description": 'The Nuclear Engineering Toolkit',
"author": 'PyNE Development Team',
"author_email": '[email protected]',
"url": 'http://pyne.github.com/',
"packages": packages,
"package_dir": pack_dir,
"package_data": pack_data,
"data_files": data_files,
"scripts": scripts,
#"zip_safe": False,
}
rtn = _setup(**setup_kwargs)
def cmake_cli(cmake_args):
if not IS_NT:
rtn = subprocess.call(['which', 'cmake'])
if rtn != 0:
sys.exit('CMake is not installed, aborting PyNE build.')
cmake_cmd = ['cmake', '..'] + cmake_args
cmake_cmd += ['-DPYTHON_EXECUTABLE=' + sys.executable]
if IS_NT:
files_on_path = set()
for p in os.environ['PATH'].split(';')[::-1]:
if os.path.exists(p):
files_on_path.update(os.listdir(p))
if 'cl.exe' in files_on_path:
pass
elif 'sh.exe' in files_on_path:
cmake_cmd += ['-G "MSYS Makefiles"']
elif 'gcc.exe' in files_on_path:
cmake_cmd += ['-G "MinGW Makefiles"']
cmake_cmd = ' '.join(cmake_cmd)
cmake_cmdstr = cmake_cmd if isinstance(cmake_cmd, str) else ' '.join(cmake_cmd)
print("CMake command is\n", cmake_cmdstr, sep="")
return cmake_cmd
def main_body(cmake_args, make_args):
assert_dep_versions()
ensure_decay()
ensure_atomic()
if not os.path.exists('build'):
os.mkdir('build')
cmake_cmd = cmake_cli(cmake_args)
rtn = subprocess.check_call(cmake_cmd, cwd='build', shell=IS_NT)
rtn = subprocess.check_call(['make'] + make_args, cwd='build')
cwd = os.getcwd()
os.chdir('build')
setup()
os.chdir(cwd)
def final_message(success=True):
if success:
return
msg = ("\n\nIf you are having issues building pyne, please report your problem "
"to [email protected] or look for help at http://pyne.io\n\n"
)
print('\n' + '-'*20 + msg + '-'*20)
def main_safe(cmake_args, make_args):
success = False
try:
main_body(cmake_args, make_args)
success = True
finally:
final_message(success)
def main():
cmake_args, make_args, bootstrap = parse_args()
main_safe(cmake_args, make_args)
if bootstrap:
ensure_nuc_data()
main_safe(cmake_args, make_args)
# trick to get install path
abspath = os.path.abspath
joinpath = os.path.join
cwd = abspath(os.getcwd())
pypath = [p for p in sys.path if len(p) > 0 and cwd != abspath(p)]
try:
_, pynepath, _ = imp.find_module('pyne', pypath)
except ImportError:
pynepath = "${HOME}/.local/python2.7/site-packages"
libpath = abspath(joinpath(pynepath, '..', '..', '..'))
binpath = abspath(joinpath(libpath, '..', 'bin'))
copy_ensdf_executables(pynepath)
msg = ("\nNOTE: If you have not done so already, please be sure that your PATH and "
"LD_LIBRARY_PATH (or DYLD_FALLBACK_LIBRARY_PATH on Mac OSX) has been "
"appropriately set to the install prefix of pyne. For this install of "
"pyne you may add the following lines to your '~/.bashrc' file or "
"equivalent:\n\n"
"# PyNE Environment Settings\n"
'export PATH="{binpath}:${{PATH}}"\n'
'export LD_LIBRARY_PATH="{libpath}:${{LD_LIBRARY_PATH}}"'
).format(binpath=binpath, libpath=libpath)
print(msg, file=sys.stderr)
if __name__ == "__main__":
main()