source: sasview/run.py @ 168d359

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

Log binnning working for SectorQ

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