source: sasview/run.py @ 0765c00

Last change on this file since 0765c00 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
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 os
17import sys
18import imp
19from contextlib import contextmanager
20from os.path import abspath, dirname, join as joinpath
21
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)
36
37def addpath(path):
38    """
39    Add a directory to the python path environment, and to the PYTHONPATH
40    environment variable for subprocesses.
41    """
42    path = abspath(path)
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"""
62    mod = imp.load_source(modname, abspath(joinpath(path,'__init__.py')))
63    sys.modules[modname] = mod
64    mod.__path__ = [abspath(path)]
65    return mod
66
67def import_dll(modname, build_path):
68    """Import a DLL from the build directory"""
69    import sysconfig
70    ext = sysconfig.get_config_var('SO')
71    # build_path comes from context
72    path = joinpath(build_path, *modname.split('.'))+ext
73    #print "importing", modname, "from", path
74    return imp.load_dynamic(modname, path)
75
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)
88
89    # Notify the help menu that the Sphinx documentation is in a different
90    # place than it otherwise would be.
91    os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc")
92
93    # Make sure that we have a private version of mplconfig
94    #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
95    #os.environ['MPLCONFIGDIR'] = mplconfig
96    #if not os.path.exists(mplconfig): os.mkdir(mplconfig)
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
105    try: import bumps
106    except: addpath(joinpath(root, '..','bumps'))
107
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
120    # sasmodels on the path
121    addpath(joinpath(root, '../sasmodels/'))
122
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'))
127
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.
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.
133    import sas.sascalc.pr
134    sas.sascalc.pr.core = import_package('sas.sascalc.pr.core',
135                                  joinpath(build_path, 'sas', 'sascalc', 'pr', 'core'))
136
137    # Compiled modules need to be pulled from the build directory.
138    # Some packages are not where they are needed, so load them explicitly.
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.
145    import sas.sascalc.calculator
146    sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core',
147                                  joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core'))
148
149    sys.path.append(build_path)
150
151    #print "\n".join(sys.path)
152
153if __name__ == "__main__":
154    prepare()
155    tee_logging()
156    from sas.sasview.sasview import run
157    run()
Note: See TracBrowser for help on using the repository browser.