source: sasview/run.py @ 7fb59b2

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

Character encoding added

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