1 | # -*- coding: utf-8 -*- |
---|
2 | #!/usr/bin/env python |
---|
3 | |
---|
4 | """ |
---|
5 | Run sasview in place. This allows sasview to use the python |
---|
6 | files in the source tree without having to call setup.py install |
---|
7 | first. A rebuild is still necessary when working on sas models |
---|
8 | or c modules. |
---|
9 | |
---|
10 | Usage: |
---|
11 | |
---|
12 | ./run.py [(module|script) args...] |
---|
13 | |
---|
14 | Without arguments run.py runs sasview. With arguments, run.py will run |
---|
15 | the given module or script. |
---|
16 | """ |
---|
17 | |
---|
18 | import imp |
---|
19 | import os |
---|
20 | import sys |
---|
21 | from contextlib import contextmanager |
---|
22 | from os.path import join as joinpath |
---|
23 | from os.path import abspath, dirname |
---|
24 | |
---|
25 | |
---|
26 | def addpath(path): |
---|
27 | """ |
---|
28 | Add a directory to the python path environment, and to the PYTHONPATH |
---|
29 | environment variable for subprocesses. |
---|
30 | """ |
---|
31 | path = abspath(path) |
---|
32 | if 'PYTHONPATH' in os.environ: |
---|
33 | PYTHONPATH = path + os.pathsep + os.environ['PYTHONPATH'] |
---|
34 | else: |
---|
35 | PYTHONPATH = path |
---|
36 | os.environ['PYTHONPATH'] = PYTHONPATH |
---|
37 | sys.path.insert(0, path) |
---|
38 | |
---|
39 | |
---|
40 | @contextmanager |
---|
41 | def cd(path): |
---|
42 | """ |
---|
43 | Change directory for duration of "with" context. |
---|
44 | """ |
---|
45 | old_dir = os.getcwd() |
---|
46 | os.chdir(path) |
---|
47 | yield |
---|
48 | os.chdir(old_dir) |
---|
49 | |
---|
50 | |
---|
51 | def import_package(modname, path): |
---|
52 | """Import a package into a particular point in the python namespace""" |
---|
53 | mod = imp.load_source(modname, abspath(joinpath(path, '__init__.py'))) |
---|
54 | sys.modules[modname] = mod |
---|
55 | mod.__path__ = [abspath(path)] |
---|
56 | return mod |
---|
57 | |
---|
58 | |
---|
59 | def import_dll(modname, build_path): |
---|
60 | """Import a DLL from the build directory""" |
---|
61 | import sysconfig |
---|
62 | ext = sysconfig.get_config_var('SO') |
---|
63 | # build_path comes from context |
---|
64 | path = joinpath(build_path, *modname.split('.')) + ext |
---|
65 | # print "importing", modname, "from", path |
---|
66 | return imp.load_dynamic(modname, path) |
---|
67 | |
---|
68 | |
---|
69 | def prepare(): |
---|
70 | # Don't create *.pyc files |
---|
71 | sys.dont_write_bytecode = True |
---|
72 | |
---|
73 | # Debug numpy warnings |
---|
74 | #import numpy; numpy.seterr(all='raise') |
---|
75 | |
---|
76 | # find the directories for the source and build |
---|
77 | from distutils.util import get_platform |
---|
78 | root = abspath(dirname(__file__)) |
---|
79 | platform = '%s-%s' % (get_platform(), sys.version[:3]) |
---|
80 | build_path = joinpath(root, 'build', 'lib.' + platform) |
---|
81 | |
---|
82 | # Notify the help menu that the Sphinx documentation is in a different |
---|
83 | # place than it otherwise would be. |
---|
84 | os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc") |
---|
85 | |
---|
86 | # Make sure that we have a private version of mplconfig |
---|
87 | #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig') |
---|
88 | #os.environ['MPLCONFIGDIR'] = mplconfig |
---|
89 | #if not os.path.exists(mplconfig): os.mkdir(mplconfig) |
---|
90 | #import matplotlib |
---|
91 | # matplotlib.use('Agg') |
---|
92 | # print matplotlib.__file__ |
---|
93 | #import pylab; pylab.hold(False) |
---|
94 | # add periodictable to the path |
---|
95 | try: |
---|
96 | import periodictable |
---|
97 | except: |
---|
98 | addpath(joinpath(root, '..', 'periodictable')) |
---|
99 | |
---|
100 | try: |
---|
101 | import bumps |
---|
102 | except: |
---|
103 | addpath(joinpath(root, '..', 'bumps')) |
---|
104 | |
---|
105 | # select wx version |
---|
106 | #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython')) |
---|
107 | |
---|
108 | # Build project if the build directory does not already exist. |
---|
109 | if not os.path.exists(build_path): |
---|
110 | import subprocess |
---|
111 | with cd(root): |
---|
112 | subprocess.call((sys.executable, "setup.py", "build"), shell=False) |
---|
113 | |
---|
114 | # Put the source trees on the path |
---|
115 | addpath(joinpath(root, 'src')) |
---|
116 | |
---|
117 | # sasmodels on the path |
---|
118 | addpath(joinpath(root, '../sasmodels/')) |
---|
119 | |
---|
120 | # Import the sasview package from root/sasview as sas.sasview. It would |
---|
121 | # be better to just store the package in src/sas/sasview. |
---|
122 | import sas |
---|
123 | sas.sasview = import_package('sas.sasview', joinpath(root, 'sasview')) |
---|
124 | |
---|
125 | # The sas.models package Compiled Model files should be pulled in from the build directory even though |
---|
126 | # the source is stored in src/sas/models. |
---|
127 | |
---|
128 | # Compiled modules need to be pulled from the build directory. |
---|
129 | # Some packages are not where they are needed, so load them explicitly. |
---|
130 | import sas.sascalc.pr |
---|
131 | sas.sascalc.pr.core = import_package('sas.sascalc.pr.core', |
---|
132 | joinpath(build_path, 'sas', 'sascalc', 'pr', 'core')) |
---|
133 | |
---|
134 | # Compiled modules need to be pulled from the build directory. |
---|
135 | # Some packages are not where they are needed, so load them explicitly. |
---|
136 | import sas.sascalc.file_converter |
---|
137 | sas.sascalc.file_converter.core = import_package('sas.sascalc.file_converter.core', |
---|
138 | joinpath(build_path, 'sas', 'sascalc', 'file_converter', 'core')) |
---|
139 | |
---|
140 | # Compiled modules need to be pulled from the build directory. |
---|
141 | # Some packages are not where they are needed, so load them explicitly. |
---|
142 | import sas.sascalc.calculator |
---|
143 | sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core', |
---|
144 | joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core')) |
---|
145 | |
---|
146 | sys.path.append(build_path) |
---|
147 | |
---|
148 | # print "\n".join(sys.path) |
---|
149 | |
---|
150 | |
---|
151 | if __name__ == "__main__": |
---|
152 | # Need to add absolute path before actual prepare call, |
---|
153 | # so logging can be done during initialization process too |
---|
154 | root = abspath(dirname(__file__)) |
---|
155 | addpath(joinpath(root, 'sasview')) |
---|
156 | from logger_config import SetupLogger |
---|
157 | logger = SetupLogger(__name__).config_development() |
---|
158 | |
---|
159 | logger.debug("Starting SASVIEW in debug mode.") |
---|
160 | prepare() |
---|
161 | from sas.sasview.sasview import run |
---|
162 | run() |
---|
163 | logger.debug("Ending SASVIEW in debug mode.") |
---|