[a3e5455] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | Run sasview in place. This allows sasview to use the python |
---|
| 4 | files in the source tree without having to call setup.py install |
---|
[3a39c2e] | 5 | first. A rebuild is still necessary when working on sas models |
---|
[a3e5455] | 6 | or c modules. |
---|
| 7 | |
---|
| 8 | Usage: |
---|
| 9 | |
---|
[6fe5100] | 10 | ./run.py [(module|script) args...] |
---|
| 11 | |
---|
| 12 | Without arguments run.py runs sasview. With arguments, run.py will run |
---|
| 13 | the given module or script. |
---|
[a3e5455] | 14 | """ |
---|
| 15 | |
---|
| 16 | import os |
---|
| 17 | import sys |
---|
| 18 | import imp |
---|
[64ca561] | 19 | import logging |
---|
| 20 | import logging.config |
---|
| 21 | |
---|
[bbd97e5] | 22 | from contextlib import contextmanager |
---|
| 23 | from os.path import abspath, dirname, join as joinpath |
---|
[a3e5455] | 24 | |
---|
| 25 | |
---|
[64ca561] | 26 | LOGGER_CONFIG_FILE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'sasview/logging.ini') |
---|
| 27 | logging.config.fileConfig(LOGGER_CONFIG_FILE, disable_existing_loggers=True) |
---|
| 28 | logger = logging.getLogger(__name__) |
---|
| 29 | |
---|
| 30 | def 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] | 39 | def 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 |
---|
| 53 | def 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 | |
---|
| 62 | def import_package(modname, path): |
---|
| 63 | """Import a package into a particular point in the python namespace""" |
---|
[bbd97e5] | 64 | mod = imp.load_source(modname, abspath(joinpath(path,'__init__.py'))) |
---|
[a3e5455] | 65 | sys.modules[modname] = mod |
---|
[bbd97e5] | 66 | mod.__path__ = [abspath(path)] |
---|
[a3e5455] | 67 | return mod |
---|
| 68 | |
---|
[499639c] | 69 | def import_dll(modname, build_path): |
---|
[a3e5455] | 70 | """Import a DLL from the build directory""" |
---|
[499639c] | 71 | import sysconfig |
---|
| 72 | ext = sysconfig.get_config_var('SO') |
---|
[a3e5455] | 73 | # build_path comes from context |
---|
[499639c] | 74 | path = joinpath(build_path, *modname.split('.'))+ext |
---|
[a3e5455] | 75 | #print "importing", modname, "from", path |
---|
| 76 | return imp.load_dynamic(modname, path) |
---|
| 77 | |
---|
[bbd97e5] | 78 | def prepare(): |
---|
| 79 | # Don't create *.pyc files |
---|
| 80 | sys.dont_write_bytecode = True |
---|
| 81 | |
---|
| 82 | # Debug numpy warnings |
---|
| 83 | #import numpy; numpy.seterr(all='raise') |
---|
| 84 | |
---|
| 85 | # find the directories for the source and build |
---|
| 86 | from distutils.util import get_platform |
---|
| 87 | root = abspath(dirname(__file__)) |
---|
| 88 | platform = '%s-%s'%(get_platform(),sys.version[:3]) |
---|
| 89 | build_path = joinpath(root, 'build','lib.'+platform) |
---|
[18e7309] | 90 | |
---|
| 91 | # Notify the help menu that the Sphinx documentation is in a different |
---|
[70a9d1c] | 92 | # place than it otherwise would be. |
---|
[c3437260] | 93 | os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc") |
---|
| 94 | |
---|
[bbd97e5] | 95 | # Make sure that we have a private version of mplconfig |
---|
[278e86f] | 96 | #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig') |
---|
| 97 | #os.environ['MPLCONFIGDIR'] = mplconfig |
---|
| 98 | #if not os.path.exists(mplconfig): os.mkdir(mplconfig) |
---|
[bbd97e5] | 99 | #import matplotlib |
---|
| 100 | #matplotlib.use('Agg') |
---|
| 101 | #print matplotlib.__file__ |
---|
| 102 | #import pylab; pylab.hold(False) |
---|
| 103 | # add periodictable to the path |
---|
| 104 | try: import periodictable |
---|
| 105 | except: addpath(joinpath(root, '..','periodictable')) |
---|
| 106 | |
---|
[95d58d3] | 107 | try: import bumps |
---|
| 108 | except: addpath(joinpath(root, '..','bumps')) |
---|
| 109 | |
---|
[bbd97e5] | 110 | # select wx version |
---|
| 111 | #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython')) |
---|
| 112 | |
---|
| 113 | # Build project if the build directory does not already exist. |
---|
| 114 | if not os.path.exists(build_path): |
---|
| 115 | import subprocess |
---|
| 116 | with cd(root): |
---|
| 117 | subprocess.call((sys.executable, "setup.py", "build"), shell=False) |
---|
| 118 | |
---|
| 119 | # Put the source trees on the path |
---|
| 120 | addpath(joinpath(root, 'src')) |
---|
| 121 | |
---|
[0e4e554] | 122 | # sasmodels on the path |
---|
| 123 | addpath(joinpath(root, '../sasmodels/')) |
---|
| 124 | |
---|
[3a39c2e] | 125 | # Import the sasview package from root/sasview as sas.sasview. It would |
---|
| 126 | # be better to just store the package in src/sas/sasview. |
---|
| 127 | import sas |
---|
| 128 | sas.sasview = import_package('sas.sasview', joinpath(root,'sasview')) |
---|
[bbd97e5] | 129 | |
---|
[3a39c2e] | 130 | # The sas.models package Compiled Model files should be pulled in from the build directory even though |
---|
| 131 | # the source is stored in src/sas/models. |
---|
[bbd97e5] | 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. |
---|
[b699768] | 135 | import sas.sascalc.pr |
---|
| 136 | sas.sascalc.pr.core = import_package('sas.sascalc.pr.core', |
---|
| 137 | joinpath(build_path, 'sas', 'sascalc', 'pr', 'core')) |
---|
[bbd97e5] | 138 | |
---|
[9e531f2] | 139 | # Compiled modules need to be pulled from the build directory. |
---|
| 140 | # Some packages are not where they are needed, so load them explicitly. |
---|
[18e7309] | 141 | import sas.sascalc.file_converter |
---|
| 142 | sas.sascalc.file_converter.core = import_package('sas.sascalc.file_converter.core', |
---|
| 143 | joinpath(build_path, 'sas', 'sascalc', 'file_converter', 'core')) |
---|
| 144 | |
---|
| 145 | # Compiled modules need to be pulled from the build directory. |
---|
| 146 | # Some packages are not where they are needed, so load them explicitly. |
---|
[9e531f2] | 147 | import sas.sascalc.calculator |
---|
| 148 | sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core', |
---|
| 149 | joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core')) |
---|
[bbd97e5] | 150 | |
---|
| 151 | sys.path.append(build_path) |
---|
| 152 | |
---|
| 153 | #print "\n".join(sys.path) |
---|
| 154 | |
---|
| 155 | if __name__ == "__main__": |
---|
[64ca561] | 156 | update_all_logs_to_debug(logger) |
---|
[bbd97e5] | 157 | prepare() |
---|
[3a39c2e] | 158 | from sas.sasview.sasview import run |
---|
[6fe5100] | 159 | run() |
---|
[64ca561] | 160 | |
---|