-
Notifications
You must be signed in to change notification settings - Fork 24
/
run.py
executable file
·86 lines (61 loc) · 2.07 KB
/
run.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
#!/usr/bin/env python
"""
Build and run refl1d.
Usage:
./run.py [refl1d cli args]
"""
import os
import sys
from os.path import abspath, join as joinpath, dirname
import traceback
import warnings
# From mgab at https://stackoverflow.com/a/22376126/6195051
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
log = file if hasattr(file, "write") else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))
warnings.showwarning = warn_with_traceback
def addpath(path):
"""
Add a directory to the python path environment, and to the PYTHONPATH
environment variable for subprocesses.
"""
path = abspath(path)
if "PYTHONPATH" in os.environ:
PYTHONPATH = path + os.pathsep + os.environ["PYTHONPATH"]
else:
PYTHONPATH = path
os.environ["PYTHONPATH"] = PYTHONPATH
sys.path.insert(0, path)
from contextlib import contextmanager
@contextmanager
def cd(path):
old_dir = os.getcwd()
os.chdir(path)
yield
os.chdir(old_dir)
def prepare_environment():
# sys.dont_write_bytecode = True
# Make sure that we have a private version of mplconfig
# mplconfig = joinpath(os.getcwd(), '.mplconfig')
# os.environ['MPLCONFIGDIR'] = mplconfig
# if not os.path.exists(mplconfig):
# os.mkdir(mplconfig)
# import numpy; numpy.seterr(all='raise')
refl1d_root = abspath(dirname(__file__))
periodictable_root = abspath(joinpath(refl1d_root, "..", "periodictable"))
bumps_root = abspath(joinpath(refl1d_root, "..", "bumps"))
# add bumps and periodictable to the path
addpath(periodictable_root)
addpath(bumps_root)
# Add the build dir to the system path
addpath(refl1d_root)
# Make sample data and models available
os.environ["REFL1D_DATA"] = joinpath(refl1d_root, "doc", "examples")
# print "\n".join(sys.path)
if __name__ == "__main__":
import multiprocessing
multiprocessing.freeze_support()
prepare_environment()
import refl1d.main
refl1d.main.cli()