1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | """ |
---|
4 | Run sasview in place. This allows sasview to use the python |
---|
5 | files in the source tree without having to call setup.py install |
---|
6 | first. A rebuild is still necessary when working on sas models |
---|
7 | or c modules. |
---|
8 | |
---|
9 | Usage: |
---|
10 | |
---|
11 | ./run.py [(module|script) args...] |
---|
12 | |
---|
13 | Without arguments run.py runs sasview. With arguments, run.py will run |
---|
14 | the given module or script. |
---|
15 | """ |
---|
16 | from __future__ import print_function |
---|
17 | |
---|
18 | import imp |
---|
19 | import os |
---|
20 | import sys |
---|
21 | from contextlib import contextmanager |
---|
22 | from os.path import join as joinpath |
---|
23 | from os.path import abspath, dirname |
---|
24 | |
---|
25 | def addpath(path): |
---|
26 | """ |
---|
27 | Add a directory to the python path environment, and to the PYTHONPATH |
---|
28 | environment variable for subprocesses. |
---|
29 | """ |
---|
30 | path = abspath(path) |
---|
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 | |
---|
38 | |
---|
39 | @contextmanager |
---|
40 | def 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 | |
---|
49 | |
---|
50 | def import_package(modname, path): |
---|
51 | """Import a package into a particular point in the python namespace""" |
---|
52 | #logger.debug("Dynamicly importing: %s", path) |
---|
53 | mod = imp.load_source(modname, abspath(joinpath(path, '__init__.py'))) |
---|
54 | sys.modules[modname] = mod |
---|
55 | mod.__path__ = [abspath(path)] |
---|
56 | return mod |
---|
57 | |
---|
58 | |
---|
59 | def import_dll(modname, build_path): |
---|
60 | """Import a DLL from the build directory""" |
---|
61 | import sysconfig |
---|
62 | ext = sysconfig.get_config_var('SO') |
---|
63 | # build_path comes from context |
---|
64 | path = joinpath(build_path, *modname.split('.')) + ext |
---|
65 | # print "importing", modname, "from", path |
---|
66 | return imp.load_dynamic(modname, path) |
---|
67 | |
---|
68 | |
---|
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 |
---|
78 | root = abspath(dirname(__file__)) |
---|
79 | platform = '%s-%s' % (get_platform(), sys.version[:3]) |
---|
80 | build_path = joinpath(root, 'build', 'lib.' + platform) |
---|
81 | |
---|
82 | # Notify the help menu that the Sphinx documentation is in a different |
---|
83 | # place than it otherwise would be. |
---|
84 | os.environ['SASVIEW_DOC_PATH'] = joinpath(build_path, "doc") |
---|
85 | |
---|
86 | # Make sure that we have a private version of mplconfig |
---|
87 | #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig') |
---|
88 | #os.environ['MPLCONFIGDIR'] = mplconfig |
---|
89 | #if not os.path.exists(mplconfig): os.mkdir(mplconfig) |
---|
90 | #import matplotlib |
---|
91 | # matplotlib.use('Agg') |
---|
92 | # print matplotlib.__file__ |
---|
93 | #import pylab; pylab.hold(False) |
---|
94 | # add periodictable to the path |
---|
95 | try: |
---|
96 | import periodictable |
---|
97 | except: |
---|
98 | addpath(joinpath(root, '..', 'periodictable')) |
---|
99 | |
---|
100 | try: |
---|
101 | import bumps |
---|
102 | except: |
---|
103 | addpath(joinpath(root, '..', 'bumps')) |
---|
104 | |
---|
105 | try: |
---|
106 | import tinycc |
---|
107 | except: |
---|
108 | addpath(joinpath(root, '../tinycc/build/lib')) |
---|
109 | |
---|
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 | # PAK: with "update" we can always build since it is fast |
---|
115 | if True or not os.path.exists(build_path): |
---|
116 | import subprocess |
---|
117 | build_cmd = [sys.executable, "setup.py", "build", "update"] |
---|
118 | if os.name == 'nt': |
---|
119 | build_cmd.append('--compiler=tinycc') |
---|
120 | # need shell=True on windows to keep console box from popping up |
---|
121 | shell = (os.name == 'nt') |
---|
122 | with cd(root): |
---|
123 | subprocess.call(build_cmd, shell=shell) |
---|
124 | |
---|
125 | # Put the source trees on the path |
---|
126 | addpath(joinpath(root, 'src')) |
---|
127 | |
---|
128 | # sasmodels on the path |
---|
129 | addpath(joinpath(root, '../sasmodels/')) |
---|
130 | |
---|
131 | # The sas.models package Compiled Model files should be pulled in from the build directory even though |
---|
132 | # the source is stored in src/sas/models. |
---|
133 | |
---|
134 | # Compiled modules need to be pulled from the build directory. |
---|
135 | # Some packages are not where they are needed, so load them explicitly. |
---|
136 | import sas.sascalc.pr |
---|
137 | sas.sascalc.pr.core = import_package('sas.sascalc.pr.core', |
---|
138 | joinpath(build_path, 'sas', 'sascalc', 'pr', 'core')) |
---|
139 | |
---|
140 | # Compiled modules need to be pulled from the build directory. |
---|
141 | # Some packages are not where they are needed, so load them explicitly. |
---|
142 | import sas.sascalc.file_converter |
---|
143 | sas.sascalc.file_converter.core = import_package('sas.sascalc.file_converter.core', |
---|
144 | joinpath(build_path, 'sas', 'sascalc', 'file_converter', 'core')) |
---|
145 | |
---|
146 | import sas.sascalc.calculator |
---|
147 | sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core', |
---|
148 | joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core')) |
---|
149 | |
---|
150 | sys.path.append(build_path) |
---|
151 | |
---|
152 | set_git_tag() |
---|
153 | # print "\n".join(sys.path) |
---|
154 | |
---|
155 | def set_git_tag(): |
---|
156 | try: |
---|
157 | import subprocess |
---|
158 | import os |
---|
159 | import platform |
---|
160 | FNULL = open(os.devnull, 'w') |
---|
161 | if platform.system() == "Windows": |
---|
162 | args = ['git', 'describe', '--tags'] |
---|
163 | else: |
---|
164 | args = ['git describe --tags'] |
---|
165 | git_revision = subprocess.check_output(args, stderr=FNULL, shell=True) |
---|
166 | import sas.sasview |
---|
167 | sas.sasview.__build__ = str(git_revision).strip() |
---|
168 | except subprocess.CalledProcessError as cpe: |
---|
169 | get_logger().warning("Error while determining build number\n Using command:\n %s \n Output:\n %s"% (cpe.cmd,cpe.output)) |
---|
170 | |
---|
171 | _logger = None |
---|
172 | def get_logger(): |
---|
173 | global _logger |
---|
174 | if _logger is None: |
---|
175 | from sas.logger_config import SetupLogger |
---|
176 | _logger = SetupLogger(__name__).config_development() |
---|
177 | return _logger |
---|
178 | |
---|
179 | if __name__ == "__main__": |
---|
180 | # Need to add absolute path before actual prepare call, |
---|
181 | # so logging can be done during initialization process too |
---|
182 | root = abspath(dirname(__file__)) |
---|
183 | addpath(joinpath(root, 'src')) |
---|
184 | |
---|
185 | get_logger().debug("Starting SASVIEW in debug mode.") |
---|
186 | prepare() |
---|
187 | from sas.sasview.sasview import run_cli, run_gui |
---|
188 | if len(sys.argv) == 1: |
---|
189 | run_gui() |
---|
190 | else: |
---|
191 | run_cli() |
---|
192 | get_logger().debug("Ending SASVIEW in debug mode.") |
---|