source: sasview/run.py @ fc22533

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since fc22533 was fc22533, checked in by Ricardo Ferraz Leal <ricleal@…>, 7 years ago

Added better formatter

  • Property mode set to 100755
File size: 5.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 sas 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 imp
17import logging
18import logging.config
19import os
20import sys
21from contextlib import contextmanager
22from os.path import join as joinpath
23from os.path import abspath, dirname
24
25try:
26    # running from a local computer as developer
27    from sasview.logger_config import SetupLogger
28except ImportError:
29    from logger_config import SetupLogger
30
31l = SetupLogger(__name__)
32logger = l.config_development()
33
34def addpath(path):
35    """
36    Add a directory to the python path environment, and to the PYTHONPATH
37    environment variable for subprocesses.
38    """
39    path = abspath(path)
40    if 'PYTHONPATH' in os.environ:
41        PYTHONPATH = path + os.pathsep + os.environ['PYTHONPATH']
42    else:
43        PYTHONPATH = path
44    os.environ['PYTHONPATH'] = PYTHONPATH
45    sys.path.insert(0, path)
46
47@contextmanager
48def cd(path):
49    """
50    Change directory for duration of "with" context.
51    """
52    old_dir = os.getcwd()
53    os.chdir(path)
54    yield
55    os.chdir(old_dir)
56
57def import_package(modname, path):
58    """Import a package into a particular point in the python namespace"""
59    mod = imp.load_source(modname, abspath(joinpath(path,'__init__.py')))
60    sys.modules[modname] = mod
61    mod.__path__ = [abspath(path)]
62    return mod
63
64def import_dll(modname, build_path):
65    """Import a DLL from the build directory"""
66    import sysconfig
67    ext = sysconfig.get_config_var('SO')
68    # build_path comes from context
69    path = joinpath(build_path, *modname.split('.'))+ext
70    #print "importing", modname, "from", path
71    return imp.load_dynamic(modname, path)
72
73def prepare():
74    # Don't create *.pyc files
75    sys.dont_write_bytecode = True
76
77    # Debug numpy warnings
78    #import numpy; numpy.seterr(all='raise')
79
80    # find the directories for the source and build
81    from distutils.util import get_platform
82    root = abspath(dirname(__file__))
83    platform = '%s-%s'%(get_platform(),sys.version[:3])
84    build_path = joinpath(root, 'build','lib.'+platform)
85
86    # Notify the help menu that the Sphinx documentation is in a different
87    # place than it otherwise would be.
88    os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc")
89
90    # Make sure that we have a private version of mplconfig
91    #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
92    #os.environ['MPLCONFIGDIR'] = mplconfig
93    #if not os.path.exists(mplconfig): os.mkdir(mplconfig)
94    #import matplotlib
95    #matplotlib.use('Agg')
96    #print matplotlib.__file__
97    #import pylab; pylab.hold(False)
98    # add periodictable to the path
99    try: import periodictable
100    except: addpath(joinpath(root, '..','periodictable'))
101
102    try: import bumps
103    except: addpath(joinpath(root, '..','bumps'))
104
105    # select wx version
106    #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython'))
107
108    # Build project if the build directory does not already exist.
109    if not os.path.exists(build_path):
110        import subprocess
111        with cd(root):
112            subprocess.call((sys.executable, "setup.py", "build"), shell=False)
113
114    # Put the source trees on the path
115    addpath(joinpath(root, 'src'))
116
117    # sasmodels on the path
118    addpath(joinpath(root, '../sasmodels/'))
119
120    # Import the sasview package from root/sasview as sas.sasview.  It would
121    # be better to just store the package in src/sas/sasview.
122    import sas
123    sas.sasview = import_package('sas.sasview', joinpath(root,'sasview'))
124
125    # The sas.models package Compiled Model files should be pulled in from the build directory even though
126    # the source is stored in src/sas/models.
127
128    # Compiled modules need to be pulled from the build directory.
129    # Some packages are not where they are needed, so load them explicitly.
130    import sas.sascalc.pr
131    sas.sascalc.pr.core = import_package('sas.sascalc.pr.core',
132                                  joinpath(build_path, 'sas', 'sascalc', 'pr', 'core'))
133
134    # Compiled modules need to be pulled from the build directory.
135    # Some packages are not where they are needed, so load them explicitly.
136    import sas.sascalc.file_converter
137    sas.sascalc.file_converter.core = import_package('sas.sascalc.file_converter.core',
138                                  joinpath(build_path, 'sas', 'sascalc', 'file_converter', 'core'))                   
139
140    # Compiled modules need to be pulled from the build directory.
141    # Some packages are not where they are needed, so load them explicitly.
142    import sas.sascalc.calculator
143    sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core',
144                                  joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core'))
145
146    sys.path.append(build_path)
147
148    #print "\n".join(sys.path)
149
150if __name__ == "__main__":
151    logger.debug("Starting SASVIEW in debug mode.")
152    prepare()
153    from sas.sasview.sasview import run
154    run()
155    logger.debug("Ending SASVIEW in debug mode.")
Note: See TracBrowser for help on using the repository browser.