1 |
#!@PYTHON@ |
2 |
# ^ python path substituted here is not depended-upon in Windows |
3 |
|
4 |
import os.path, sys, platform |
5 |
|
6 |
restart = 0 |
7 |
|
8 |
# This script launched ASCEND, after first ensuring that all necessary |
9 |
# variables are present in the 'environment', and adding them and |
10 |
# restarting the process if necessary. You can override values for these |
11 |
# env vars by setting them before this script is run. |
12 |
|
13 |
# Note that as well as env vars, ASCEND PyGTK is also controllable using the |
14 |
# .ascend.ini file in the user's home directory. |
15 |
|
16 |
def path_absolute(p): |
17 |
if os.path.isabs(p): |
18 |
return p |
19 |
else: |
20 |
return os.path.join(sys.path[0],p) |
21 |
|
22 |
#----------------------------------------------------------------------- |
23 |
# Get locations of the installed files. On windows, these are defined in |
24 |
# in the registry. On other systems, these are defined at compile time |
25 |
# by @VARNAME@ substitution. |
26 |
|
27 |
SEP = ":" |
28 |
PYTHON="@PYTHON@" |
29 |
GTKLIBS = "" # assume the GTK will be in the standard library path |
30 |
|
31 |
if platform.system()=="Windows": |
32 |
SEP=";" |
33 |
LDPATHVAR = 'PATH' |
34 |
PYTHON=os.path.join(sys.prefix,"pythonw.exe") |
35 |
|
36 |
import _winreg as wreg |
37 |
k = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, "SOFTWARE\ASCEND") |
38 |
|
39 |
# on Windows, we should be able to *assume* relative paths... |
40 |
|
41 |
# for some reason, QueryValue doesn't work on Py 2.4, need to use QueryValueEx. |
42 |
INSTALL_SOLVERS,t = wreg.QueryValueEx(k,"INSTALL_SOLVERS") |
43 |
INSTALL_MODELS,t = wreg.QueryValueEx(k,"INSTALL_MODELS") |
44 |
INSTALL_LIB,t = wreg.QueryValueEx(k,"INSTALL_LIB") |
45 |
INSTALL_ASCDATA,t = wreg.QueryValueEx(k,"INSTALL_ASCDATA") |
46 |
GTKLIBS,t = wreg.QueryValueEx(k,"GTKLIBS"); |
47 |
DEFAULT_ASCENDLIBRARY="%s;%s" % (INSTALL_SOLVERS,INSTALL_MODELS) |
48 |
|
49 |
INSTALL_PYTHON = os.path.join(INSTALL_ASCDATA,"python") |
50 |
PYVERSION = "@PYVERSION@" |
51 |
|
52 |
elif platform.system()=="Darwin": |
53 |
|
54 |
LDPATHVAR = 'DYLD_LIBRARY_PATH' |
55 |
|
56 |
# FIXME |
57 |
#GTKLIBS="/Users/john/gtk/inst/lib" |
58 |
|
59 |
INSTALL_LIB = path_absolute(".") |
60 |
INSTALL_ASCDATA = path_absolute("Resources") |
61 |
INSTALL_MODELS = path_absolute("""@ASC_LIBRARY_REL_DIST@""") |
62 |
INSTALL_SOLVERS = path_absolute("""@ASC_SOLVERS_REL_DIST@""") |
63 |
INSTALL_PYTHON = path_absolute("Python") |
64 |
DEFAULT_ASCENDLIBRARY="%s;%s" % (INSTALL_SOLVERS,INSTALL_MODELS) |
65 |
else: |
66 |
LDPATHVAR = 'LD_LIBRARY_PATH' |
67 |
|
68 |
# FIXME for possible case of relative paths... |
69 |
|
70 |
INSTALL_LIB="@INSTALL_LIB@" |
71 |
INSTALL_ASCDATA="@INSTALL_ASCDATA@" |
72 |
INSTALL_MODELS = "@INSTALL_MODELS@" |
73 |
INSTALL_SOLVERS = "@INSTALL_SOLVERS@" |
74 |
INSTALL_PYTHON = "@INSTALL_PYTHON@" |
75 |
DEFAULT_ASCENDLIBRARY="""@DEFAULT_ASCENDLIBRARY@""" |
76 |
|
77 |
#----------------------------------------------------------------------- |
78 |
|
79 |
if os.environ.get(LDPATHVAR): |
80 |
LDPATH = os.environ.get(LDPATHVAR).split(SEP) |
81 |
else: |
82 |
LDPATH = [] |
83 |
|
84 |
if not os.environ.get('ASC_GDB'): |
85 |
# restarting messes up GDB so don't allow it |
86 |
|
87 |
if platform.system()=="Windows" : |
88 |
# Put INSTALL_LIB then GTK at start of path |
89 |
if GTKLIBS not in LDPATH: |
90 |
restart = 1 |
91 |
if INSTALL_LIB not in LDPATH: |
92 |
restart = 1 |
93 |
if GTKLIBS in LDPATH: |
94 |
LDPATH.remove(GTKLIBS) |
95 |
LDPATH=[INSTALL_LIB,GTKLIBS]+LDPATH |
96 |
os.environ[LDPATHVAR] = SEP.join(LDPATH) |
97 |
elif INSTALL_LIB != "/usr/lib" and not INSTALL_LIB in LDPATH: |
98 |
# don't worry about GTK location; just ensure that LDPATH includes INSTALL_LIB |
99 |
LDPATH = [INSTALL_LIB] + LDPATH |
100 |
os.environ[LDPATHVAR] = SEP.join(LDPATH) |
101 |
restart = 1 |
102 |
|
103 |
# if ASCENDLIBRARY has been specified, make sure it's including the Model Library |
104 |
if os.environ.get('ASCENDLIBRARY') or platform.system()=="Darwin": |
105 |
if not 'ASCENDLIBRARY' in os.environ: |
106 |
os.environ['ASCENDLIBRARY'] = "" |
107 |
envmodels = [os.path.abspath(i) for i in os.environ['ASCENDLIBRARY'].split(SEP)] |
108 |
if not INSTALL_MODELS in envmodels: |
109 |
envmodels.append(INSTALL_MODELS) |
110 |
os.environ['ASCENDLIBRARY'] = SEP.join(envmodels) |
111 |
restart = 1 |
112 |
|
113 |
# if ASCENDSOLVERS has been specified, make sure it includes the standard solvers |
114 |
if os.environ.get('ASCENDSOLVERS') or platform.system()=="Darwin": |
115 |
if not 'ASCENDSOLVERS' in os.environ: |
116 |
os.environ['ASCENDSOLVERS'] = "" |
117 |
envsolvers = [os.path.abspath(i) for i in os.environ['ASCENDSOLVERS'].split(SEP)] |
118 |
if not INSTALL_SOLVERS in envsolvers: |
119 |
envsolvers.append(INSTALL_SOLVERS) |
120 |
os.environ['ASCENDSOLVERS'] = SEP.join(envsolvers) |
121 |
restart = 1 |
122 |
|
123 |
# don't need to restart process on Windows as env vars update immediately |
124 |
if restart and platform.system()!="Windows": |
125 |
print "Restarting with corrected environment..." |
126 |
if os.environ.get(LDPATHVAR): |
127 |
print " %s = %s" % (LDPATHVAR,os.environ.get(LDPATHVAR)) |
128 |
if os.environ.get('ASCENDLIBRARY'): |
129 |
print " ASCENDLIBRARY = %s" % os.environ.get('ASCENDLIBRARY') |
130 |
if os.environ.get('ASCENDSOLVERS'): |
131 |
print " ASCENDSOLVERS = %s" % os.environ.get('ASCENDSOLVERS') |
132 |
script = os.path.join(sys.path[0],"ascend") |
133 |
print "PYTHON =",PYTHON |
134 |
print "script =",script |
135 |
os.execve(PYTHON,[script] + sys.argv, os.environ) |
136 |
|
137 |
print "Running with..." |
138 |
print " %s = %s" % (LDPATHVAR, os.environ.get(LDPATHVAR)) |
139 |
print " sys.path = %s" % sys.path |
140 |
print " argv = %s" % sys.argv |
141 |
|
142 |
if os.environ.get('ASCENDLIBRARY'): |
143 |
ASCENDLIBRARY = os.environ.get('ASCENDLIBRARY') |
144 |
print " ASCENDLIBRARY = %s" % ASCENDLIBRARY |
145 |
|
146 |
if os.environ.get('ASCENDSOLVERS'): |
147 |
ASCENDSOLVERS = os.environ.get('ASCENDSOLVERS') |
148 |
print " ASCENDSOLVERS = %s" % ASCENDLIBRARY |
149 |
|
150 |
print "sys.path[0] = %s" % sys.path[0] |
151 |
|
152 |
if not INSTALL_PYTHON in sys.path: |
153 |
print "Adding INSTALL_PYTHON=%s to python path" % INSTALL_PYTHON |
154 |
sys.path.append(INSTALL_PYTHON) |
155 |
|
156 |
if os.environ.get('OSTYPE')=='cygwin': |
157 |
print "CYGWIN..." |
158 |
elif os.environ.get('OSTYPE')=='msys': |
159 |
print "MSYS..." |
160 |
|
161 |
if os.environ.get('ASC_GDB'): |
162 |
args = sys.argv |
163 |
args.pop(0) |
164 |
cmd = ["gdb","--args","python",os.path.join(INSTALL_PYTHON,"gtkbrowser.py")]+args |
165 |
print cmd |
166 |
os.execv("/usr/bin/gdb",cmd) |
167 |
else: |
168 |
import ascpy |
169 |
olddir = os.getcwd() |
170 |
print "CWD =",olddir |
171 |
os.chdir(INSTALL_ASCDATA) |
172 |
from gtkbrowser import * |
173 |
os.chdir(olddir) |
174 |
B = Browser( |
175 |
librarypath=os.environ.get('ASCENDLIBRARY') |
176 |
,assetspath=os.path.join(INSTALL_ASCDATA,"glade") |
177 |
) |
178 |
B.run() |