source: sasview/run.py @ c3437260

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 c3437260 was c3437260, checked in by Peter Parker, 9 years ago

Refs #221 - Add extra help menu item if wxPython 2.9 or higher is present. This button just opens up a viewer on the index.html page of the Sphinx documentation. Note the slight implementation detail of using os.environ to track whether or not run.py has been used to run SasView? 'in-place'. In this case the documentation is in a different place relative to the main sansview.py entry point of the program.

  • Property mode set to 100755
File size: 4.4 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 contextlib import contextmanager
20from os.path import abspath, dirname, join as joinpath
21
22
23def 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
37def 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
46def 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
53def 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
62def 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    # Notify the help menu that the Sphinx documentation is in a different
70    # place than it otherwise would be.
71
72   
73
74    # find the directories for the source and build
75    from distutils.util import get_platform
76    root = abspath(dirname(__file__))
77    platform = '%s-%s'%(get_platform(),sys.version[:3])
78    build_path = joinpath(root, 'build','lib.'+platform)
79
80    os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc")
81
82    # Make sure that we have a private version of mplconfig
83    #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
84    #os.environ['MPLCONFIGDIR'] = mplconfig
85    #if not os.path.exists(mplconfig): os.mkdir(mplconfig)
86    #import matplotlib
87    #matplotlib.use('Agg')
88    #print matplotlib.__file__
89    #import pylab; pylab.hold(False)
90    # add periodictable to the path
91    try: import periodictable
92    except: addpath(joinpath(root, '..','periodictable'))
93
94    try: import bumps
95    except: addpath(joinpath(root, '..','bumps'))
96
97    # select wx version
98    #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython'))
99
100    # Build project if the build directory does not already exist.
101    if not os.path.exists(build_path):
102        import subprocess
103        with cd(root):
104            subprocess.call((sys.executable, "setup.py", "build"), shell=False)
105
106    # Put the source trees on the path
107    addpath(joinpath(root, 'src'))
108    addpath(joinpath(root, 'park-1.2.1'))
109
110    # Import the sansview package from root/sansview as sans.sansview.  It would
111    # be better to just store the package in src/sans/sansview.
112    import sans
113    sans.sansview = import_package('sans.sansview', joinpath(root,'sansview'))
114
115    # The sans.models package Compiled Model files should be pulled in from the build directory even though
116    # the source is stored in src/sans/models.
117
118    # Compiled modules need to be pulled from the build directory.
119    # Some packages are not where they are needed, so load them explicitly.
120    import sans.pr
121    sans.pr.core = import_package('sans.pr.core',
122                                  joinpath(build_path, 'sans', 'pr', 'core'))
123    #import_dll('park._modeling', build_path)
124
125    #park = import_package('park',os.path.join(build_path,'park'))
126
127    # Pull the entire sans.models package from the build directory since it contains
128    # a mixture of autogenerated python and C.  Any changes in models will require
129    # a rebuild with setup.py build
130    sans.models = import_package('sans.models', joinpath(build_path, 'sans', 'models'))
131
132    sys.path.append(build_path)
133
134    #print "\n".join(sys.path)
135    #from sans.models import SphereModel
136
137if __name__ == "__main__":
138    prepare()
139    from sans.sansview.sansview import run
140    run()
Note: See TracBrowser for help on using the repository browser.