source: sasview/run.py @ 7f75a3f

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 7f75a3f was 05a9d29, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

show logs on the console as well as in sasview.log when starting via run.py

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