source: sasview/run.py @ d9df833

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 d9df833 was d9df833, checked in by wojciech, 7 years ago

Code clean-up for logger

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