[7fb59b2] | 1 | # -*- coding: utf-8 -*- |
---|
[a3e5455] | 2 | #!/usr/bin/env python |
---|
[f36e01f] | 3 | |
---|
[a3e5455] | 4 | """ |
---|
| 5 | Run sasview in place. This allows sasview to use the python |
---|
| 6 | files in the source tree without having to call setup.py install |
---|
[3a39c2e] | 7 | first. A rebuild is still necessary when working on sas models |
---|
[a3e5455] | 8 | or c modules. |
---|
| 9 | |
---|
| 10 | Usage: |
---|
| 11 | |
---|
[6fe5100] | 12 | ./run.py [(module|script) args...] |
---|
| 13 | |
---|
| 14 | Without arguments run.py runs sasview. With arguments, run.py will run |
---|
| 15 | the given module or script. |
---|
[a3e5455] | 16 | """ |
---|
| 17 | |
---|
| 18 | import imp |
---|
| 19 | import os |
---|
| 20 | import sys |
---|
[bbd97e5] | 21 | from contextlib import contextmanager |
---|
[38beeab] | 22 | from os.path import join as joinpath |
---|
[2a8bd705] | 23 | from os.path import abspath, dirname, realpath |
---|
[a3e5455] | 24 | |
---|
| 25 | |
---|
| 26 | def addpath(path): |
---|
| 27 | """ |
---|
| 28 | Add a directory to the python path environment, and to the PYTHONPATH |
---|
| 29 | environment variable for subprocesses. |
---|
| 30 | """ |
---|
[bbd97e5] | 31 | path = abspath(path) |
---|
[a3e5455] | 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 | |
---|
[f36e01f] | 39 | |
---|
[a3e5455] | 40 | @contextmanager |
---|
| 41 | def 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 | |
---|
[f36e01f] | 50 | |
---|
[a3e5455] | 51 | def import_package(modname, path): |
---|
| 52 | """Import a package into a particular point in the python namespace""" |
---|
[f94a935] | 53 | #logger.debug("Dynamicly importing: %s", path) |
---|
[f36e01f] | 54 | mod = imp.load_source(modname, abspath(joinpath(path, '__init__.py'))) |
---|
[a3e5455] | 55 | sys.modules[modname] = mod |
---|
[bbd97e5] | 56 | mod.__path__ = [abspath(path)] |
---|
[a3e5455] | 57 | return mod |
---|
| 58 | |
---|
[f36e01f] | 59 | |
---|
[499639c] | 60 | def import_dll(modname, build_path): |
---|
[a3e5455] | 61 | """Import a DLL from the build directory""" |
---|
[499639c] | 62 | import sysconfig |
---|
| 63 | ext = sysconfig.get_config_var('SO') |
---|
[a3e5455] | 64 | # build_path comes from context |
---|
[f36e01f] | 65 | path = joinpath(build_path, *modname.split('.')) + ext |
---|
[a3e5455] | 66 | return imp.load_dynamic(modname, path) |
---|
| 67 | |
---|
[f36e01f] | 68 | |
---|
[bbd97e5] | 69 | def prepare(): |
---|
| 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 |
---|
[2a8bd705] | 78 | root = abspath(dirname(sys.argv[0])) |
---|
| 79 | |
---|
[f36e01f] | 80 | platform = '%s-%s' % (get_platform(), sys.version[:3]) |
---|
| 81 | build_path = joinpath(root, 'build', 'lib.' + platform) |
---|
[18e7309] | 82 | |
---|
| 83 | # Notify the help menu that the Sphinx documentation is in a different |
---|
[70a9d1c] | 84 | # place than it otherwise would be. |
---|
[c3437260] | 85 | os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc") |
---|
| 86 | |
---|
[bbd97e5] | 87 | # Make sure that we have a private version of mplconfig |
---|
[278e86f] | 88 | #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig') |
---|
| 89 | #os.environ['MPLCONFIGDIR'] = mplconfig |
---|
| 90 | #if not os.path.exists(mplconfig): os.mkdir(mplconfig) |
---|
[bbd97e5] | 91 | #import matplotlib |
---|
[f36e01f] | 92 | # matplotlib.use('Agg') |
---|
| 93 | # print matplotlib.__file__ |
---|
[bbd97e5] | 94 | #import pylab; pylab.hold(False) |
---|
| 95 | # add periodictable to the path |
---|
[f36e01f] | 96 | try: |
---|
| 97 | import periodictable |
---|
| 98 | except: |
---|
| 99 | addpath(joinpath(root, '..', 'periodictable')) |
---|
[bbd97e5] | 100 | |
---|
[f36e01f] | 101 | try: |
---|
| 102 | import bumps |
---|
| 103 | except: |
---|
| 104 | addpath(joinpath(root, '..', 'bumps')) |
---|
[95d58d3] | 105 | |
---|
[bbd97e5] | 106 | # Build project if the build directory does not already exist. |
---|
| 107 | if not os.path.exists(build_path): |
---|
| 108 | import subprocess |
---|
| 109 | with cd(root): |
---|
| 110 | subprocess.call((sys.executable, "setup.py", "build"), shell=False) |
---|
| 111 | |
---|
| 112 | # Put the source trees on the path |
---|
| 113 | addpath(joinpath(root, 'src')) |
---|
| 114 | |
---|
[0e4e554] | 115 | # sasmodels on the path |
---|
| 116 | addpath(joinpath(root, '../sasmodels/')) |
---|
| 117 | |
---|
[3a39c2e] | 118 | # Import the sasview package from root/sasview as sas.sasview. It would |
---|
| 119 | # be better to just store the package in src/sas/sasview. |
---|
[4992ff2] | 120 | #import sas |
---|
| 121 | #sas.sasview = import_package('sas.sasview', joinpath(root, 'src','sas','sasview')) |
---|
[bbd97e5] | 122 | |
---|
| 123 | # Compiled modules need to be pulled from the build directory. |
---|
| 124 | # Some packages are not where they are needed, so load them explicitly. |
---|
[b699768] | 125 | import sas.sascalc.pr |
---|
| 126 | sas.sascalc.pr.core = import_package('sas.sascalc.pr.core', |
---|
[f36e01f] | 127 | joinpath(build_path, 'sas', 'sascalc', 'pr', 'core')) |
---|
[bbd97e5] | 128 | |
---|
[9e531f2] | 129 | # Compiled modules need to be pulled from the build directory. |
---|
| 130 | # Some packages are not where they are needed, so load them explicitly. |
---|
[18e7309] | 131 | import sas.sascalc.file_converter |
---|
| 132 | sas.sascalc.file_converter.core = import_package('sas.sascalc.file_converter.core', |
---|
[f36e01f] | 133 | joinpath(build_path, 'sas', 'sascalc', 'file_converter', 'core')) |
---|
[18e7309] | 134 | |
---|
| 135 | # Compiled modules need to be pulled from the build directory. |
---|
| 136 | # Some packages are not where they are needed, so load them explicitly. |
---|
[9e531f2] | 137 | import sas.sascalc.calculator |
---|
| 138 | sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core', |
---|
[4992ff2] | 139 | joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core')) |
---|
[bbd97e5] | 140 | |
---|
| 141 | sys.path.append(build_path) |
---|
| 142 | |
---|
[856cf59] | 143 | # Run the UI conversion tool |
---|
| 144 | import sas.qtgui.convertUI |
---|
| 145 | |
---|
| 146 | |
---|
[bbd97e5] | 147 | if __name__ == "__main__": |
---|
[f36e01f] | 148 | # Need to add absolute path before actual prepare call, |
---|
| 149 | # so logging can be done during initialization process too |
---|
[2a8bd705] | 150 | root = abspath(dirname(realpath(sys.argv[0]))) |
---|
[f4a1433] | 151 | addpath(joinpath(root, 'src','sas')) |
---|
[d9df833] | 152 | from logger_config import SetupLogger |
---|
| 153 | logger = SetupLogger(__name__).config_development() |
---|
| 154 | |
---|
[38beeab] | 155 | logger.debug("Starting SASVIEW in debug mode.") |
---|
[bbd97e5] | 156 | prepare() |
---|
[83eb5208] | 157 | from sas.qtgui.MainWindow.MainWindow import run |
---|
[6fe5100] | 158 | run() |
---|
[38beeab] | 159 | logger.debug("Ending SASVIEW in debug mode.") |
---|