source: sasview/run.py @ 6f10243

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 6f10243 was 3a39c2e, checked in by krzywon, 10 years ago

Next iteration of the SANS→SAS is complete.

  • Property mode set to 100755
File size: 4.4 KB
RevLine 
[a3e5455]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
[3a39c2e]5first.  A rebuild is still necessary when working on sas models
[a3e5455]6or c modules.
7
8Usage:
9
[6fe5100]10./run.py [(module|script) args...]
11
12Without arguments run.py runs sasview.  With arguments, run.py will run
13the given module or script.
[a3e5455]14"""
15
16import os
17import sys
18import imp
[bbd97e5]19from contextlib import contextmanager
20from os.path import abspath, dirname, join as joinpath
[a3e5455]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    """
[bbd97e5]28    path = abspath(path)
[a3e5455]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"""
[bbd97e5]48    mod = imp.load_source(modname, abspath(joinpath(path,'__init__.py')))
[a3e5455]49    sys.modules[modname] = mod
[bbd97e5]50    mod.__path__ = [abspath(path)]
[a3e5455]51    return mod
52
[499639c]53def import_dll(modname, build_path):
[a3e5455]54    """Import a DLL from the build directory"""
[499639c]55    import sysconfig
56    ext = sysconfig.get_config_var('SO')
[a3e5455]57    # build_path comes from context
[499639c]58    path = joinpath(build_path, *modname.split('.'))+ext
[a3e5455]59    #print "importing", modname, "from", path
60    return imp.load_dynamic(modname, path)
61
[bbd97e5]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    # 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)
[70a9d1c]74   
75    # Notify the help menu that the Sphinx documentation is in a different
76    # place than it otherwise would be.
[c3437260]77    os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc")
78
[bbd97e5]79    # Make sure that we have a private version of mplconfig
[278e86f]80    #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
81    #os.environ['MPLCONFIGDIR'] = mplconfig
82    #if not os.path.exists(mplconfig): os.mkdir(mplconfig)
[bbd97e5]83    #import matplotlib
84    #matplotlib.use('Agg')
85    #print matplotlib.__file__
86    #import pylab; pylab.hold(False)
87    # add periodictable to the path
88    try: import periodictable
89    except: addpath(joinpath(root, '..','periodictable'))
90
[95d58d3]91    try: import bumps
92    except: addpath(joinpath(root, '..','bumps'))
93
[bbd97e5]94    # select wx version
95    #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython'))
96
97    # Build project if the build directory does not already exist.
98    if not os.path.exists(build_path):
99        import subprocess
100        with cd(root):
101            subprocess.call((sys.executable, "setup.py", "build"), shell=False)
102
103    # Put the source trees on the path
104    addpath(joinpath(root, 'src'))
105    addpath(joinpath(root, 'park-1.2.1'))
106
[3a39c2e]107    # Import the sasview package from root/sasview as sas.sasview.  It would
108    # be better to just store the package in src/sas/sasview.
109    import sas
110    sas.sasview = import_package('sas.sasview', joinpath(root,'sasview'))
[bbd97e5]111
[3a39c2e]112    # The sas.models package Compiled Model files should be pulled in from the build directory even though
113    # the source is stored in src/sas/models.
[bbd97e5]114
115    # Compiled modules need to be pulled from the build directory.
116    # Some packages are not where they are needed, so load them explicitly.
[3a39c2e]117    import sas.pr
118    sas.pr.core = import_package('sas.pr.core',
119                                  joinpath(build_path, 'sas', 'pr', 'core'))
[499639c]120    #import_dll('park._modeling', build_path)
[bbd97e5]121
122    #park = import_package('park',os.path.join(build_path,'park'))
123
[3a39c2e]124    # Pull the entire sas.models package from the build directory since it contains
[bbd97e5]125    # a mixture of autogenerated python and C.  Any changes in models will require
126    # a rebuild with setup.py build
[3a39c2e]127    sas.models = import_package('sas.models', joinpath(build_path, 'sas', 'models'))
[bbd97e5]128
129    sys.path.append(build_path)
130
131    #print "\n".join(sys.path)
[3a39c2e]132    #from sas.models import SphereModel
[bbd97e5]133
134if __name__ == "__main__":
135    prepare()
[3a39c2e]136    from sas.sasview.sasview import run
[6fe5100]137    run()
Note: See TracBrowser for help on using the repository browser.