[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 |
---|
| 5 | first. A rebuild is still necessary when working on sans models |
---|
| 6 | or c modules. |
---|
| 7 | |
---|
| 8 | Usage: |
---|
| 9 | |
---|
| 10 | ./run.py [args] |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | import os |
---|
| 14 | import sys |
---|
| 15 | import imp |
---|
| 16 | from glob import glob |
---|
| 17 | from distutils.util import get_platform |
---|
| 18 | |
---|
| 19 | # find the directories for the source and build |
---|
| 20 | root = os.path.abspath(os.path.dirname(__file__)) |
---|
| 21 | platform = '%s-%s'%(get_platform(),sys.version[:3]) |
---|
| 22 | build_path = os.path.join(root, 'build','lib.'+platform) |
---|
| 23 | |
---|
| 24 | # Make sure that we have a private version of mplconfig |
---|
| 25 | mplconfig = os.path.join(os.getcwd(), '.mplconfig') |
---|
| 26 | os.environ['MPLCONFIGDIR'] = mplconfig |
---|
| 27 | if not os.path.exists(mplconfig): os.mkdir(mplconfig) |
---|
| 28 | #import matplotlib |
---|
| 29 | #matplotlib.use('Agg') |
---|
| 30 | #print matplotlib.__file__ |
---|
| 31 | #import pylab; pylab.hold(False) |
---|
| 32 | |
---|
| 33 | def addpath(path): |
---|
| 34 | """ |
---|
| 35 | Add a directory to the python path environment, and to the PYTHONPATH |
---|
| 36 | environment variable for subprocesses. |
---|
| 37 | """ |
---|
| 38 | path = os.path.abspath(path) |
---|
| 39 | if 'PYTHONPATH' in os.environ: |
---|
| 40 | PYTHONPATH = path + os.pathsep + os.environ['PYTHONPATH'] |
---|
| 41 | else: |
---|
| 42 | PYTHONPATH = path |
---|
| 43 | os.environ['PYTHONPATH'] = PYTHONPATH |
---|
| 44 | sys.path.insert(0, path) |
---|
| 45 | |
---|
| 46 | from contextlib import contextmanager |
---|
| 47 | @contextmanager |
---|
| 48 | def cd(path): |
---|
| 49 | """ |
---|
| 50 | Change directory for duration of "with" context. |
---|
| 51 | """ |
---|
| 52 | old_dir = os.getcwd() |
---|
| 53 | os.chdir(path) |
---|
| 54 | yield |
---|
| 55 | os.chdir(old_dir) |
---|
| 56 | |
---|
| 57 | def import_package(modname, path): |
---|
| 58 | """Import a package into a particular point in the python namespace""" |
---|
| 59 | mod = imp.load_source(modname, os.path.abspath(os.path.join(path,'__init__.py'))) |
---|
| 60 | sys.modules[modname] = mod |
---|
| 61 | mod.__path__ = [os.path.abspath(path)] |
---|
| 62 | return mod |
---|
| 63 | |
---|
| 64 | def import_dll(modname): |
---|
| 65 | """Import a DLL from the build directory""" |
---|
| 66 | # build_path comes from context |
---|
| 67 | path = glob(os.path.join(build_path, *modname.split('.'))+'.*')[0] |
---|
| 68 | #print "importing", modname, "from", path |
---|
| 69 | return imp.load_dynamic(modname, path) |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | # Don't create *.pyc files |
---|
| 73 | sys.dont_write_bytecode = True |
---|
| 74 | |
---|
| 75 | # Debug numpy warnings |
---|
| 76 | #import numpy; numpy.seterr(all='raise') |
---|
| 77 | |
---|
| 78 | # add periodictable to the path |
---|
| 79 | try: import periodictable |
---|
| 80 | except: addpath(os.path.join(root, '..','periodictable')) |
---|
| 81 | |
---|
| 82 | # select wx version |
---|
| 83 | #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython')) |
---|
| 84 | |
---|
| 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(os.path.join(root, 'src')) |
---|
| 93 | addpath(os.path.join(root, 'park-1.2.1')) |
---|
| 94 | |
---|
| 95 | # Import the sansview package from root/sansview as sans.sansview. It would |
---|
| 96 | # be better to just store the package in src/sans/sansview. |
---|
| 97 | import sans |
---|
| 98 | sans.sansview = import_package('sans.sansview', os.path.join(root,'sansview')) |
---|
| 99 | |
---|
| 100 | # The sans.models package Compiled Model files should be pulled in from the build directory even though |
---|
| 101 | # the source is stored in src/sans/models. |
---|
| 102 | |
---|
| 103 | # Compiled modules need to be pulled from the build directory. |
---|
| 104 | # Some packages are not where they are needed, so load them explicitly. |
---|
| 105 | import sans.pr |
---|
| 106 | sans.pr.core = import_package('sans.pr.core', |
---|
| 107 | os.path.join(build_path, 'sans', 'pr', 'core')) |
---|
| 108 | import_dll('park._modeling') |
---|
| 109 | |
---|
| 110 | # Pull the entire sans.models package from the build directory since it contains |
---|
| 111 | # a mixture of autogenerated python and C. Any changes in models will require |
---|
| 112 | # a rebuild with setup.py build |
---|
| 113 | sans.models = import_package('sans.models', os.path.join(build_path, 'sans', 'models')) |
---|
| 114 | |
---|
| 115 | #print "\n".join(sys.path) |
---|
| 116 | #from sans.models import SphereModel |
---|
| 117 | |
---|
| 118 | # Won't need freeze support when running from the source tree |
---|
| 119 | #import multiprocessing |
---|
| 120 | #multiprocessing.freeze_support() |
---|
| 121 | |
---|
| 122 | # start sasview |
---|
[71aa9ac] | 123 | from sans.sansview.sansview import SasView |
---|
| 124 | SasView() |
---|