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