[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 |
---|
[bbd97e5] | 19 | from contextlib import contextmanager |
---|
| 20 | from os.path import abspath, dirname, join as joinpath |
---|
[a3e5455] | 21 | |
---|
| 22 | |
---|
| 23 | def addpath(path): |
---|
| 24 | """ |
---|
| 25 | Add a directory to the python path environment, and to the PYTHONPATH |
---|
| 26 | environment variable for subprocesses. |
---|
| 27 | """ |
---|
[bbd97e5] | 28 | path = abspath(path) |
---|
[a3e5455] | 29 | if 'PYTHONPATH' in os.environ: |
---|
| 30 | PYTHONPATH = path + os.pathsep + os.environ['PYTHONPATH'] |
---|
| 31 | else: |
---|
| 32 | PYTHONPATH = path |
---|
| 33 | os.environ['PYTHONPATH'] = PYTHONPATH |
---|
| 34 | sys.path.insert(0, path) |
---|
| 35 | |
---|
| 36 | @contextmanager |
---|
| 37 | def cd(path): |
---|
| 38 | """ |
---|
| 39 | Change directory for duration of "with" context. |
---|
| 40 | """ |
---|
| 41 | old_dir = os.getcwd() |
---|
| 42 | os.chdir(path) |
---|
| 43 | yield |
---|
| 44 | os.chdir(old_dir) |
---|
| 45 | |
---|
| 46 | def import_package(modname, path): |
---|
| 47 | """Import a package into a particular point in the python namespace""" |
---|
[bbd97e5] | 48 | mod = imp.load_source(modname, abspath(joinpath(path,'__init__.py'))) |
---|
[a3e5455] | 49 | sys.modules[modname] = mod |
---|
[bbd97e5] | 50 | mod.__path__ = [abspath(path)] |
---|
[a3e5455] | 51 | return mod |
---|
| 52 | |
---|
[499639c] | 53 | def import_dll(modname, build_path): |
---|
[a3e5455] | 54 | """Import a DLL from the build directory""" |
---|
[499639c] | 55 | import sysconfig |
---|
| 56 | ext = sysconfig.get_config_var('SO') |
---|
[a3e5455] | 57 | # build_path comes from context |
---|
[499639c] | 58 | path = joinpath(build_path, *modname.split('.'))+ext |
---|
[a3e5455] | 59 | return imp.load_dynamic(modname, path) |
---|
| 60 | |
---|
[bbd97e5] | 61 | def prepare(): |
---|
| 62 | # Don't create *.pyc files |
---|
| 63 | sys.dont_write_bytecode = True |
---|
| 64 | |
---|
| 65 | # Debug numpy warnings |
---|
| 66 | #import numpy; numpy.seterr(all='raise') |
---|
| 67 | |
---|
| 68 | # find the directories for the source and build |
---|
| 69 | from distutils.util import get_platform |
---|
| 70 | root = abspath(dirname(__file__)) |
---|
| 71 | platform = '%s-%s'%(get_platform(),sys.version[:3]) |
---|
| 72 | build_path = joinpath(root, 'build','lib.'+platform) |
---|
[70a9d1c] | 73 | |
---|
| 74 | # Notify the help menu that the Sphinx documentation is in a different |
---|
| 75 | # place than it otherwise would be. |
---|
[c3437260] | 76 | os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc") |
---|
| 77 | |
---|
[bbd97e5] | 78 | # add periodictable to the path |
---|
| 79 | try: import periodictable |
---|
| 80 | except: addpath(joinpath(root, '..','periodictable')) |
---|
| 81 | |
---|
[95d58d3] | 82 | try: import bumps |
---|
| 83 | except: addpath(joinpath(root, '..','bumps')) |
---|
| 84 | |
---|
[bbd97e5] | 85 | # Build project if the build directory does not already exist. |
---|
| 86 | if not os.path.exists(build_path): |
---|
| 87 | import subprocess |
---|
| 88 | with cd(root): |
---|
| 89 | subprocess.call((sys.executable, "setup.py", "build"), shell=False) |
---|
| 90 | |
---|
| 91 | # Put the source trees on the path |
---|
| 92 | addpath(joinpath(root, 'src')) |
---|
| 93 | |
---|
[0e4e554] | 94 | # sasmodels on the path |
---|
| 95 | addpath(joinpath(root, '../sasmodels/')) |
---|
| 96 | |
---|
[3a39c2e] | 97 | # Import the sasview package from root/sasview as sas.sasview. It would |
---|
| 98 | # be better to just store the package in src/sas/sasview. |
---|
| 99 | import sas |
---|
| 100 | sas.sasview = import_package('sas.sasview', joinpath(root,'sasview')) |
---|
[bbd97e5] | 101 | |
---|
| 102 | # Compiled modules need to be pulled from the build directory. |
---|
| 103 | # Some packages are not where they are needed, so load them explicitly. |
---|
[b699768] | 104 | import sas.sascalc.pr |
---|
| 105 | sas.sascalc.pr.core = import_package('sas.sascalc.pr.core', |
---|
| 106 | joinpath(build_path, 'sas', 'sascalc', 'pr', 'core')) |
---|
[bbd97e5] | 107 | |
---|
[9e531f2] | 108 | # Compiled modules need to be pulled from the build directory. |
---|
| 109 | # Some packages are not where they are needed, so load them explicitly. |
---|
| 110 | import sas.sascalc.calculator |
---|
| 111 | sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core', |
---|
| 112 | joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core')) |
---|
[bbd97e5] | 113 | |
---|
| 114 | sys.path.append(build_path) |
---|
| 115 | |
---|
| 116 | if __name__ == "__main__": |
---|
| 117 | prepare() |
---|
[31c5b58] | 118 | from sas.qtgui.MainWindow import run |
---|
[6fe5100] | 119 | run() |
---|