source: sasview/run.py @ 6f87cc1

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 6f87cc1 was 499639c, checked in by pkienzle, 10 years ago

use platform specific filename for compiled extension in run.py rather than generic glob expansion

  • Property mode set to 100755
File size: 4.3 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, build_path):
55    """Import a DLL from the build directory"""
56    import sysconfig
57    ext = sysconfig.get_config_var('SO')
58    # build_path comes from context
59    path = joinpath(build_path, *modname.split('.'))+ext
60    #print "importing", modname, "from", path
61    return imp.load_dynamic(modname, path)
62
63def prepare():
64    # Don't create *.pyc files
65    sys.dont_write_bytecode = True
66
67    # Debug numpy warnings
68    #import numpy; numpy.seterr(all='raise')
69
70    # find the directories for the source and build
71    from distutils.util import get_platform
72    root = abspath(dirname(__file__))
73    platform = '%s-%s'%(get_platform(),sys.version[:3])
74    build_path = joinpath(root, 'build','lib.'+platform)
75
76    # Make sure that we have a private version of mplconfig
77    mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
78    os.environ['MPLCONFIGDIR'] = mplconfig
79    if not os.path.exists(mplconfig): os.mkdir(mplconfig)
80    #import matplotlib
81    #matplotlib.use('Agg')
82    #print matplotlib.__file__
83    #import pylab; pylab.hold(False)
84    # add periodictable to the path
85    try: import periodictable
86    except: addpath(joinpath(root, '..','periodictable'))
87
88    try: import bumps
89    except: addpath(joinpath(root, '..','bumps'))
90
91    # select wx version
92    #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython'))
93
94    # Build project if the build directory does not already exist.
95    if not os.path.exists(build_path):
96        import subprocess
97        with cd(root):
98            subprocess.call((sys.executable, "setup.py", "build"), shell=False)
99
100    # Put the source trees on the path
101    addpath(joinpath(root, 'src'))
102    addpath(joinpath(root, 'park-1.2.1'))
103
104    # Import the sansview package from root/sansview as sans.sansview.  It would
105    # be better to just store the package in src/sans/sansview.
106    import sans
107    sans.sansview = import_package('sans.sansview', joinpath(root,'sansview'))
108
109    # The sans.models package Compiled Model files should be pulled in from the build directory even though
110    # the source is stored in src/sans/models.
111
112    # Compiled modules need to be pulled from the build directory.
113    # Some packages are not where they are needed, so load them explicitly.
114    import sans.pr
115    sans.pr.core = import_package('sans.pr.core',
116                                  joinpath(build_path, 'sans', 'pr', 'core'))
117    #import_dll('park._modeling', build_path)
118
119    #park = import_package('park',os.path.join(build_path,'park'))
120
121    # Pull the entire sans.models package from the build directory since it contains
122    # a mixture of autogenerated python and C.  Any changes in models will require
123    # a rebuild with setup.py build
124    sans.models = import_package('sans.models', joinpath(build_path, 'sans', 'models'))
125
126    sys.path.append(build_path)
127
128    #print "\n".join(sys.path)
129    #from sans.models import SphereModel
130
131if __name__ == "__main__":
132    prepare()
133    from sans.sansview.sansview import run
134    run()
Note: See TracBrowser for help on using the repository browser.