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