forked from cyclus/cyclus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getdecay.py
75 lines (67 loc) · 2 KB
/
getdecay.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
#! /usr/bin/env python
from __future__ import print_function, unicode_literals
import os
import sys
import io
import shutil
import tarfile
import argparse
if sys.version_info[0] < 3:
from urllib import urlopen
else:
from urllib.request import urlopen
DECAY_H = os.path.join('src', 'pyne_decay.h')
DECAY_CPP = os.path.join('src', 'pyne_decay.cc')
DECAY_H_REP = os.path.join('src', '_pyne_decay.h')
DECAY_CPP_REP = os.path.join('src', '_pyne_decay.cc')
DECAY_URL = 'http://data.pyne.io/decay.tar.gz'
def ensure_include():
with io.open(DECAY_CPP, 'r') as f:
cc = f.read()
if cc.startswith('#include "pyne.h"'):
return
incs = ('#include "pyne.h"\n'
'#include "pyne_decay.h"\n')
with io.open(DECAY_CPP, 'w') as f:
f.write(incs)
f.write(cc)
def download():
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()
tar.close()
durl.close()
shutil.move('decay.h', DECAY_H)
shutil.move('decay.cpp', DECAY_CPP)
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
downloaded = download()
if downloaded:
ensure_include()
return
print('!'*42)
print('Decay files could not be downloaded or generated, using surrogates instead.')
print('!'*42 + '\n')
shutil.copy(DECAY_H_REP, DECAY_H)
shutil.copy(DECAY_CPP_REP, DECAY_CPP)
ensure_include()
if __name__ == '__main__':
desc = 'Downloads pre-generated decay source code for Cyclus.'
parser = argparse.ArgumentParser(description=desc)
desc = 'Root directory for Cyclus project code.'
parser.add_argument('--root', help=desc, default='.')
args = parser.parse_args()
os.chdir(args.root)
ensure_decay()