source: sasview/run.py @ 38beeab

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 38beeab was 38beeab, checked in by Ricardo Ferraz Leal <ricleal@…>, 7 years ago

Logger is now a separate file

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