source: sasview/run.py @ 6fe5100

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 6fe5100 was 6fe5100, checked in by pkienzle, 10 years ago

Bumps first pass. Fitting works but no pretty pictures

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