source: sasview/run.py @ 1580cc3

ticket-1094-headless
Last change on this file since 1580cc3 was 1580cc3, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

Merge branch 'master' into ticket-1094-headless

  • Property mode set to 100755
File size: 5.6 KB
RevLine 
[a3e5455]1#!/usr/bin/env python
[0225a3f]2# -*- coding: utf-8 -*-
[a3e5455]3"""
4Run sasview in place.  This allows sasview to use the python
5files in the source tree without having to call setup.py install
[3a39c2e]6first.  A rebuild is still necessary when working on sas models
[a3e5455]7or c modules.
8
9Usage:
10
[6fe5100]11./run.py [(module|script) args...]
12
13Without arguments run.py runs sasview.  With arguments, run.py will run
14the given module or script.
[a3e5455]15"""
[c6bdb3b]16from __future__ import print_function
[a3e5455]17
18import imp
19import os
20import sys
[bbd97e5]21from contextlib import contextmanager
[38beeab]22from os.path import join as joinpath
23from os.path import abspath, dirname
[a3e5455]24
25def addpath(path):
26    """
27    Add a directory to the python path environment, and to the PYTHONPATH
28    environment variable for subprocesses.
29    """
[bbd97e5]30    path = abspath(path)
[a3e5455]31    if 'PYTHONPATH' in os.environ:
32        PYTHONPATH = path + os.pathsep + os.environ['PYTHONPATH']
33    else:
34        PYTHONPATH = path
35    os.environ['PYTHONPATH'] = PYTHONPATH
36    sys.path.insert(0, path)
37
[f36e01f]38
[a3e5455]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
[f36e01f]49
[a3e5455]50def import_package(modname, path):
51    """Import a package into a particular point in the python namespace"""
[f94a935]52    #logger.debug("Dynamicly importing: %s", path)
[f36e01f]53    mod = imp.load_source(modname, abspath(joinpath(path, '__init__.py')))
[a3e5455]54    sys.modules[modname] = mod
[bbd97e5]55    mod.__path__ = [abspath(path)]
[a3e5455]56    return mod
57
[f36e01f]58
[499639c]59def import_dll(modname, build_path):
[a3e5455]60    """Import a DLL from the build directory"""
[499639c]61    import sysconfig
62    ext = sysconfig.get_config_var('SO')
[a3e5455]63    # build_path comes from context
[f36e01f]64    path = joinpath(build_path, *modname.split('.')) + ext
65    # print "importing", modname, "from", path
[a3e5455]66    return imp.load_dynamic(modname, path)
67
[f36e01f]68
[3388337]69def prepare(rebuild=True):
[bbd97e5]70    # Don't create *.pyc files
71    sys.dont_write_bytecode = True
72
73    # Debug numpy warnings
74    #import numpy; numpy.seterr(all='raise')
75
76    # find the directories for the source and build
77    from distutils.util import get_platform
78    root = abspath(dirname(__file__))
[f36e01f]79    platform = '%s-%s' % (get_platform(), sys.version[:3])
80    build_path = joinpath(root, 'build', 'lib.' + platform)
[18e7309]81
82    # Notify the help menu that the Sphinx documentation is in a different
[70a9d1c]83    # place than it otherwise would be.
[c3437260]84    os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc")
85
[bbd97e5]86    # Make sure that we have a private version of mplconfig
[278e86f]87    #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
88    #os.environ['MPLCONFIGDIR'] = mplconfig
89    #if not os.path.exists(mplconfig): os.mkdir(mplconfig)
[bbd97e5]90    #import matplotlib
[f36e01f]91    # matplotlib.use('Agg')
92    # print matplotlib.__file__
[bbd97e5]93    #import pylab; pylab.hold(False)
94    # add periodictable to the path
[f36e01f]95    try:
96        import periodictable
[952ea1f]97    except ImportError:
[f36e01f]98        addpath(joinpath(root, '..', 'periodictable'))
[bbd97e5]99
[f36e01f]100    try:
101        import bumps
[952ea1f]102    except ImportError:
[f36e01f]103        addpath(joinpath(root, '..', 'bumps'))
[95d58d3]104
[2ecb0a5]105    try:
106        import tinycc
[952ea1f]107    except ImportError:
[2ecb0a5]108        addpath(joinpath(root, '../tinycc/build/lib'))
109
[bbd97e5]110    # select wx version
111    #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython'))
112
[1580cc3]113<<<<<<< HEAD
[bbd97e5]114    # Build project if the build directory does not already exist.
[237f35e]115    # PAK: with "update" we can always build since it is fast
[3388337]116    if rebuild or not os.path.exists(build_path):
[1580cc3]117=======
[952ea1f]118    # Put the sas source tree on the path
119    addpath(joinpath(root, 'src'))
120
121    # Put sasmodels on the path
122    addpath(joinpath(root, '../sasmodels/'))
123
124    # Check if the C extensions are already built
125    try:
126        from sas.sascalc.pr import _pr_inversion
127        from sas.sascalc.calculator import _sld2i
128        from sas.sascalc.file_converter import _bsl_loader
129    except ImportError:
130        rebuild = True
131
132    # Build C extensions if necessary.  Do an inplace build to simplify path.
133    if rebuild:
[1580cc3]134>>>>>>> master
[bbd97e5]135        import subprocess
[952ea1f]136        build_cmd = [sys.executable, "setup.py", "build_ext", "--inplace", "update"]
[bc8b8a1]137        if os.name == 'nt':
138            build_cmd.append('--compiler=tinycc')
139        # need shell=True on windows to keep console box from popping up
140        shell = (os.name == 'nt')
[bbd97e5]141        with cd(root):
[bc8b8a1]142            subprocess.call(build_cmd, shell=shell)
[bbd97e5]143
[7c105e8]144    set_git_tag()
[f36e01f]145    # print "\n".join(sys.path)
146
[7c105e8]147def set_git_tag():
148    try:
149        import subprocess
150        import os
151        import platform
152        FNULL = open(os.devnull, 'w')
153        if platform.system() == "Windows":
154            args = ['git', 'describe', '--tags']
155        else:
156            args = ['git describe --tags']
157        git_revision = subprocess.check_output(args, stderr=FNULL, shell=True)
158        import sas.sasview
159        sas.sasview.__build__ = str(git_revision).strip()
160    except subprocess.CalledProcessError as cpe:
[e61f668]161        get_logger().warning("Error while determining build number\n  Using command:\n %s \n Output:\n %s"% (cpe.cmd,cpe.output))
[7c105e8]162
[e61f668]163_logger = None
164def get_logger():
165    global _logger
[7c64911]166    if _logger is None:
[e61f668]167        from sas.logger_config import SetupLogger
168        _logger = SetupLogger(__name__).config_development()
169    return _logger
[bbd97e5]170
171if __name__ == "__main__":
[f36e01f]172    # Need to add absolute path before actual prepare call,
173    # so logging can be done during initialization process too
[d9df833]174    root = abspath(dirname(__file__))
[ed03b99]175    addpath(joinpath(root, 'src'))
[d9df833]176
[e61f668]177    get_logger().debug("Starting SASVIEW in debug mode.")
[bbd97e5]178    prepare()
[2ecb0a5]179    from sas.sasview.sasview import run_cli, run_gui
180    if len(sys.argv) == 1:
181        run_gui()
182    else:
183        run_cli()
[e61f668]184    get_logger().debug("Ending SASVIEW in debug mode.")
Note: See TracBrowser for help on using the repository browser.