forked from SasView/sasview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
139 lines (111 loc) · 4.16 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
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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
Run sasview in place. This allows sasview to use the python
files in the source tree without having to call setup.py install
first. A rebuild is still necessary when working on sas models
or c modules.
Usage:
./run.py [(module|script) args...]
Without arguments run.py runs sasview. With arguments, run.py will run
the given module or script.
"""
import sys
import os
from os.path import abspath, dirname, realpath, join as joinpath
from contextlib import contextmanager
PLUGIN_MODEL_DIR = 'plugin_models'
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)
@contextmanager
def cd(path):
"""
Change directory for duration of "with" context.
"""
old_dir = os.getcwd()
os.chdir(path)
yield
os.chdir(old_dir)
def setup_sasmodels():
"""
Prepare sasmodels for running within sasview.
"""
# Set SAS_MODELPATH so sasmodels can find our custom models
import sas
plugin_dir = os.path.join(sas.get_user_dir(), PLUGIN_MODEL_DIR)
os.environ['SAS_MODELPATH'] = plugin_dir
def prepare():
# Don't create *.pyc files
sys.dont_write_bytecode = True
# Debug numpy warnings
#import numpy; numpy.seterr(all='raise')
# find the directories for the source and build
from distutils.util import get_platform
root = abspath(dirname(realpath(__file__)))
platform = '%s-%s' % (get_platform(), sys.version[:3])
build_path = joinpath(root, 'build', 'lib.' + platform)
# Notify the help menu that the Sphinx documentation is in a different
# place than it otherwise would be.
os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc")
# Make sure that we have a private version of mplconfig
#mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
#os.environ['MPLCONFIGDIR'] = mplconfig
#if not os.path.exists(mplconfig): os.mkdir(mplconfig)
#import matplotlib
# matplotlib.use('Agg')
# print matplotlib.__file__
#import pylab; pylab.hold(False)
# add periodictable to the path
try:
import periodictable
except ImportError:
addpath(joinpath(root, '..', 'periodictable'))
try:
import bumps
except ImportError:
addpath(joinpath(root, '..', 'bumps'))
# == no more C sources so no need to build project to run it ==
## Build project if the build directory does not already exist.
#if not os.path.exists(build_path):
# import subprocess
# with cd(root):
# subprocess.call((sys.executable, "setup.py", "build"), shell=False)
# Put the source trees on the path
addpath(joinpath(root, 'src'))
# sasmodels on the path
addpath(joinpath(root, '../sasmodels/'))
# Note: only needed when running gui so suppress for now.
## Run the UI conversion tool.
#import sas.qtgui.convertUI
# initialize OpenCL setting
import sas
SAS_OPENCL = sas.get_custom_config().SAS_OPENCL
if SAS_OPENCL and "SAS_OPENCL" not in os.environ:
os.environ["SAS_OPENCL"] = SAS_OPENCL
if __name__ == "__main__":
# Need to add absolute path before actual prepare call,
# so logging can be done during initialization process too
root = abspath(dirname(realpath(sys.argv[0])))
addpath(joinpath(root, 'src'))
addpath(joinpath(root, joinpath('..', 'sasmodels'))) # dependency (for loading custom_config.py during log setup)
#from sas.logger_config import SetupLogger
#logger = SetupLogger(__name__).config_development()
#logger.debug("Starting SASVIEW in debug mode.")
prepare()
# Run the UI conversion tool when executed from script. This has to
# happen after prepare() so that sas.qtgui is on the path.
import sas.qtgui.convertUI
setup_sasmodels()
from sas.qtgui.MainWindow.MainWindow import run_sasview
run_sasview()
#logger.debug("Ending SASVIEW in debug mode.")