1 | # -*- coding: utf-8 -*- |
---|
2 | ''' |
---|
3 | *GSASIIpath: locations & updates* |
---|
4 | --------------------------------- |
---|
5 | |
---|
6 | Routines for dealing with file locations, etc. |
---|
7 | |
---|
8 | Determines the location of the compiled (.pyd or .so) libraries. |
---|
9 | |
---|
10 | Interfaces with subversion (svn): |
---|
11 | Determine the subversion release number by determining the highest version number |
---|
12 | where :func:`SetVersionNumber` is called (best done in every GSASII file). |
---|
13 | Other routines will update GSASII from the subversion server if svn can be |
---|
14 | found. |
---|
15 | |
---|
16 | Accesses configuration options, as defined in config.py |
---|
17 | ''' |
---|
18 | |
---|
19 | import os |
---|
20 | import sys |
---|
21 | import platform |
---|
22 | import glob |
---|
23 | import subprocess |
---|
24 | import numpy as np |
---|
25 | |
---|
26 | g2home = 'https://subversion.xray.aps.anl.gov/pyGSAS' |
---|
27 | 'Define the location of the GSAS-II subversion repository' |
---|
28 | |
---|
29 | path2GSAS2 = os.path.dirname(os.path.realpath(__file__)) # location of this file; save before any changes in pwd |
---|
30 | |
---|
31 | def GetConfigValue(key,default=None): |
---|
32 | '''Return the configuration file value for key or a default value if not present |
---|
33 | |
---|
34 | :param str key: a value to be found in the configuration (config.py) file |
---|
35 | :param default: a value to be supplied is none is in the config file or |
---|
36 | the config file is not found. Defaults to None |
---|
37 | :returns: the value found or the default. |
---|
38 | ''' |
---|
39 | return configDict.get(key,default) |
---|
40 | |
---|
41 | def SetConfigValue(parmdict): |
---|
42 | '''Set configuration variables from a dictionary where elements are lists |
---|
43 | First item in list is the default value and second is the value to use. |
---|
44 | ''' |
---|
45 | global configDict |
---|
46 | for var in parmdict: |
---|
47 | if var in configDict: |
---|
48 | del configDict[var] |
---|
49 | if parmdict[var][1] is None: continue |
---|
50 | if parmdict[var][1] == '': continue |
---|
51 | if parmdict[var][0] == parmdict[var][1]: continue |
---|
52 | configDict[var] = parmdict[var][1] |
---|
53 | |
---|
54 | # routines for looking a version numbers in files |
---|
55 | version = -1 |
---|
56 | def SetVersionNumber(RevString): |
---|
57 | '''Set the subversion version number |
---|
58 | |
---|
59 | :param str RevString: something like "$Revision: 2903 $" |
---|
60 | that is set by subversion when the file is retrieved from subversion. |
---|
61 | |
---|
62 | Place ``GSASIIpath.SetVersionNumber("$Revision: 2903 $")`` in every python |
---|
63 | file. |
---|
64 | ''' |
---|
65 | try: |
---|
66 | RevVersion = int(RevString.split(':')[1].split()[0]) |
---|
67 | global version |
---|
68 | version = max(version,RevVersion) |
---|
69 | except: |
---|
70 | pass |
---|
71 | |
---|
72 | def GetVersionNumber(): |
---|
73 | '''Return the maximum version number seen in :func:`SetVersionNumber` |
---|
74 | ''' |
---|
75 | if version > 1000: |
---|
76 | return version |
---|
77 | else: |
---|
78 | return "unknown" |
---|
79 | |
---|
80 | def LoadConfigFile(filename): |
---|
81 | '''Read a GSAS-II configuration file. |
---|
82 | Comments (starting with "%") are removed, as are empty lines |
---|
83 | |
---|
84 | :param str filename: base file name (such as 'file.dat'). Files with this name |
---|
85 | are located from the path and the contents of each are concatenated. |
---|
86 | :returns: a list containing each non-empty (after removal of comments) line |
---|
87 | found in every matching config file. |
---|
88 | ''' |
---|
89 | info = [] |
---|
90 | for path in sys.path: |
---|
91 | fil = os.path.join(path,filename) |
---|
92 | if not os.path.exists(fil): continue |
---|
93 | try: |
---|
94 | i = 0 |
---|
95 | fp = open(fil,'r') |
---|
96 | for line in fp: |
---|
97 | expr = line.split('#')[0].strip() |
---|
98 | if expr: |
---|
99 | info.append(expr) |
---|
100 | i += 1 |
---|
101 | print(str(i)+' lines read from config file '+fil) |
---|
102 | finally: |
---|
103 | fp.close() |
---|
104 | return info |
---|
105 | |
---|
106 | |
---|
107 | # routines to interface with subversion |
---|
108 | proxycmds = [] |
---|
109 | 'Used to hold proxy information for subversion, set if needed in whichsvn' |
---|
110 | svnLocCache = None |
---|
111 | 'Cached location of svn to avoid multiple searches for it' |
---|
112 | |
---|
113 | def whichsvn(): |
---|
114 | '''Returns a path to the subversion exe file, if any is found. |
---|
115 | Searches the current path after adding likely places where GSAS-II |
---|
116 | might install svn. |
---|
117 | |
---|
118 | :returns: None if svn is not found or an absolute path to the subversion |
---|
119 | executable file. |
---|
120 | ''' |
---|
121 | # use a previosuly cached svn location |
---|
122 | global svnLocCache |
---|
123 | if svnLocCache: return svnLocCache |
---|
124 | # prepare to find svn |
---|
125 | is_exe = lambda fpath: os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
---|
126 | svnprog = 'svn' |
---|
127 | if sys.platform.startswith('win'): svnprog += '.exe' |
---|
128 | gsaspath = os.path.split(__file__)[0] |
---|
129 | # check for a proxy |
---|
130 | proxyinfo = os.path.join(gsaspath,"proxyinfo.txt") |
---|
131 | if os.path.exists(proxyinfo): |
---|
132 | global proxycmds |
---|
133 | proxycmds = [] |
---|
134 | fp = open(proxyinfo,'r') |
---|
135 | host = fp.readline().strip() |
---|
136 | port = fp.readline().strip() |
---|
137 | fp.close() |
---|
138 | proxycmds.append('--config-option') |
---|
139 | proxycmds.append('servers:global:http-proxy-host='+host) |
---|
140 | proxycmds.append('--config-option') |
---|
141 | proxycmds.append('servers:global:http-proxy-port='+port) |
---|
142 | # add likely places to find subversion when installed with GSAS-II |
---|
143 | pathlist = os.environ["PATH"].split(os.pathsep) |
---|
144 | pathlist.append(os.path.split(sys.executable)[0]) |
---|
145 | pathlist.append(gsaspath) |
---|
146 | for rpt in ('..','bin'),('..','Library','bin'),('svn','bin'),('svn',),('.'): |
---|
147 | pt = os.path.normpath(os.path.join(gsaspath,*rpt)) |
---|
148 | if os.path.exists(pt): |
---|
149 | pathlist.insert(0,pt) |
---|
150 | # search path for svn or svn.exe |
---|
151 | for path in pathlist: |
---|
152 | exe_file = os.path.join(path, svnprog) |
---|
153 | if is_exe(exe_file): |
---|
154 | try: |
---|
155 | p = subprocess.Popen([exe_file,'help'],stdout=subprocess.PIPE) |
---|
156 | res = p.stdout.read() |
---|
157 | p.communicate() |
---|
158 | svnLocCache = os.path.abspath(exe_file) |
---|
159 | return svnLocCache |
---|
160 | except: |
---|
161 | pass |
---|
162 | svnLocCache = None |
---|
163 | |
---|
164 | def svnVersion(svn=None): |
---|
165 | '''Get the version number of the current subversion executable |
---|
166 | |
---|
167 | :returns: a string with a version number such as "1.6.6" or None if |
---|
168 | subversion is not found. |
---|
169 | |
---|
170 | ''' |
---|
171 | if not svn: svn = whichsvn() |
---|
172 | if not svn: return |
---|
173 | |
---|
174 | cmd = [svn,'--version','--quiet'] |
---|
175 | s = subprocess.Popen(cmd, |
---|
176 | stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
177 | out,err = s.communicate() |
---|
178 | if err: |
---|
179 | print 'subversion error!\nout=',out |
---|
180 | print 'err=',err |
---|
181 | return None |
---|
182 | return out.strip() |
---|
183 | |
---|
184 | def svnVersionNumber(svn=None): |
---|
185 | '''Get the version number of the current subversion executable |
---|
186 | |
---|
187 | :returns: a fractional version number such as 1.6 or None if |
---|
188 | subversion is not found. |
---|
189 | |
---|
190 | ''' |
---|
191 | ver = svnVersion(svn) |
---|
192 | if not ver: return |
---|
193 | M,m = ver.split('.')[:2] |
---|
194 | return int(M)+int(m)/10. |
---|
195 | |
---|
196 | def svnGetLog(fpath=os.path.split(__file__)[0],version=None): |
---|
197 | '''Get the revision log information for a specific version of the specified package |
---|
198 | |
---|
199 | :param str fpath: path to repository dictionary, defaults to directory where |
---|
200 | the current file is located. |
---|
201 | :param int version: the version number to be looked up or None (default) |
---|
202 | for the latest version. |
---|
203 | |
---|
204 | :returns: a dictionary with keys (one hopes) 'author', 'date', 'msg', and 'revision' |
---|
205 | |
---|
206 | ''' |
---|
207 | import xml.etree.ElementTree as ET |
---|
208 | svn = whichsvn() |
---|
209 | if not svn: return |
---|
210 | if version is not None: |
---|
211 | vstr = '-r'+str(version) |
---|
212 | else: |
---|
213 | vstr = '-rHEAD' |
---|
214 | |
---|
215 | cmd = [svn,'log',fpath,'--xml',vstr] |
---|
216 | if proxycmds: cmd += proxycmds |
---|
217 | s = subprocess.Popen(cmd, |
---|
218 | stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
219 | out,err = s.communicate() |
---|
220 | if err: |
---|
221 | print 'out=',out |
---|
222 | print 'err=',err |
---|
223 | return None |
---|
224 | x = ET.fromstring(out) |
---|
225 | d = {} |
---|
226 | for i in x.iter('logentry'): |
---|
227 | d = {'revision':i.attrib.get('revision','?')} |
---|
228 | for j in i: |
---|
229 | d[j.tag] = j.text |
---|
230 | break # only need the first |
---|
231 | return d |
---|
232 | |
---|
233 | def svnGetRev(fpath=os.path.split(__file__)[0],local=True): |
---|
234 | '''Obtain the version number for the either the last update of the local version |
---|
235 | or contacts the subversion server to get the latest update version (# of Head). |
---|
236 | |
---|
237 | :param str fpath: path to repository dictionary, defaults to directory where |
---|
238 | the current file is located |
---|
239 | :param bool local: determines the type of version number, where |
---|
240 | True (default): returns the latest installed update |
---|
241 | False: returns the version number of Head on the server |
---|
242 | |
---|
243 | :Returns: the version number as an str or |
---|
244 | None if there is a subversion error (likely because the path is |
---|
245 | not a repository or svn is not found) |
---|
246 | ''' |
---|
247 | |
---|
248 | import xml.etree.ElementTree as ET |
---|
249 | svn = whichsvn() |
---|
250 | if not svn: return |
---|
251 | if local: |
---|
252 | cmd = [svn,'info',fpath,'--xml'] |
---|
253 | else: |
---|
254 | cmd = [svn,'info',fpath,'--xml','-rHEAD'] |
---|
255 | if svnVersionNumber() >= 1.6: |
---|
256 | cmd += ['--non-interactive', '--trust-server-cert'] |
---|
257 | if proxycmds: cmd += proxycmds |
---|
258 | s = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
259 | out,err = s.communicate() |
---|
260 | if err: |
---|
261 | print 'svn failed\n',out |
---|
262 | print 'err=',err |
---|
263 | return None |
---|
264 | x = ET.fromstring(out) |
---|
265 | for i in x.iter('entry'): |
---|
266 | rev = i.attrib.get('revision') |
---|
267 | if rev: return rev |
---|
268 | |
---|
269 | def svnFindLocalChanges(fpath=os.path.split(__file__)[0]): |
---|
270 | '''Returns a list of files that were changed locally. If no files are changed, |
---|
271 | the list has length 0 |
---|
272 | |
---|
273 | :param fpath: path to repository dictionary, defaults to directory where |
---|
274 | the current file is located |
---|
275 | |
---|
276 | :returns: None if there is a subversion error (likely because the path is |
---|
277 | not a repository or svn is not found) |
---|
278 | |
---|
279 | ''' |
---|
280 | import xml.etree.ElementTree as ET |
---|
281 | svn = whichsvn() |
---|
282 | if not svn: return |
---|
283 | cmd = [svn,'status',fpath,'--xml'] |
---|
284 | if proxycmds: cmd += proxycmds |
---|
285 | s = subprocess.Popen(cmd, |
---|
286 | stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
287 | out,err = s.communicate() |
---|
288 | if err: return None |
---|
289 | x = ET.fromstring(out) |
---|
290 | changed = [] |
---|
291 | for i in x.iter('entry'): |
---|
292 | if i.find('wc-status').attrib.get('item') == 'modified': |
---|
293 | changed.append(i.attrib.get('path')) |
---|
294 | return changed |
---|
295 | |
---|
296 | def svnUpdateDir(fpath=os.path.split(__file__)[0],version=None): |
---|
297 | '''This performs an update of the files in a local directory from a server. |
---|
298 | |
---|
299 | :param str fpath: path to repository dictionary, defaults to directory where |
---|
300 | the current file is located |
---|
301 | :param version: the number of the version to be loaded. Used only |
---|
302 | cast as a string, but should be an integer or something that corresponds to a |
---|
303 | string representation of an integer value when cast. A value of None (default) |
---|
304 | causes the latest version on the server to be used. |
---|
305 | ''' |
---|
306 | svn = whichsvn() |
---|
307 | if not svn: return |
---|
308 | if version: |
---|
309 | verstr = '-r' + str(version) |
---|
310 | else: |
---|
311 | verstr = '-rHEAD' |
---|
312 | cmd = [svn,'update',fpath,verstr, |
---|
313 | '--non-interactive', |
---|
314 | '--accept','theirs-conflict','--force'] |
---|
315 | if svnVersionNumber() >= 1.6: |
---|
316 | cmd += ['--trust-server-cert'] |
---|
317 | if proxycmds: cmd += proxycmds |
---|
318 | s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
319 | out,err = s.communicate() |
---|
320 | if err: |
---|
321 | print(60*"=") |
---|
322 | print ("****** An error was noted, see below *********") |
---|
323 | print(60*"=") |
---|
324 | print err |
---|
325 | sys.exit() |
---|
326 | |
---|
327 | def svnUpgrade(fpath=os.path.split(__file__)[0]): |
---|
328 | '''This reformats subversion files, which may be needed if an upgrade of subversion is |
---|
329 | done. |
---|
330 | |
---|
331 | :param str fpath: path to repository dictionary, defaults to directory where |
---|
332 | the current file is located |
---|
333 | ''' |
---|
334 | svn = whichsvn() |
---|
335 | if not svn: return |
---|
336 | cmd = [svn,'upgrade',fpath,'--non-interactive'] |
---|
337 | if proxycmds: cmd += proxycmds |
---|
338 | s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
339 | out,err = s.communicate() |
---|
340 | if err: |
---|
341 | print("svn upgrade did not happen (this is probably OK). Messages:") |
---|
342 | print err |
---|
343 | |
---|
344 | def svnUpdateProcess(version=None,projectfile=None): |
---|
345 | '''perform an update of GSAS-II in a separate python process''' |
---|
346 | if not projectfile: |
---|
347 | projectfile = '' |
---|
348 | else: |
---|
349 | projectfile = os.path.realpath(projectfile) |
---|
350 | print 'restart using',projectfile |
---|
351 | if not version: |
---|
352 | version = '' |
---|
353 | else: |
---|
354 | version = str(version) |
---|
355 | # start the upgrade in a separate interpreter (avoids loading .pyd files) |
---|
356 | subprocess.Popen([sys.executable,__file__,projectfile,version]) |
---|
357 | sys.exit() |
---|
358 | |
---|
359 | def svnSwitchDir(rpath,filename,baseURL,loadpath=None,verbose=True): |
---|
360 | '''This performs a switch command to move files between subversion trees. |
---|
361 | Note that if the files were previously downloaded, |
---|
362 | the switch command will update the files to the newest version. |
---|
363 | |
---|
364 | :param str rpath: path to locate files, relative to the GSAS-II |
---|
365 | installation path (defaults to path2GSAS2) |
---|
366 | :param str URL: the repository URL |
---|
367 | :param str loadpath: the prefix for the path, if specified. Defaults to path2GSAS2 |
---|
368 | :param bool verbose: if True (default) diagnostics are printed |
---|
369 | ''' |
---|
370 | svn = whichsvn() |
---|
371 | if not svn: return |
---|
372 | URL = baseURL[:] |
---|
373 | if baseURL[-1] != '/': |
---|
374 | URL = baseURL + '/' + filename |
---|
375 | else: |
---|
376 | URL = baseURL + filename |
---|
377 | if loadpath: |
---|
378 | fpath = os.path.join(loadpath,rpath,filename) |
---|
379 | else: |
---|
380 | fpath = os.path.join(path2GSAS2,rpath,filename) |
---|
381 | cmd = [svn,'switch',URL,fpath, |
---|
382 | '--non-interactive','--trust-server-cert', |
---|
383 | '--accept','theirs-conflict','--force'] |
---|
384 | if svnVersionNumber(svn) > 1.6: cmd += ['--ignore-ancestry'] |
---|
385 | if proxycmds: cmd += proxycmds |
---|
386 | if verbose: print(u"Loading files to "+fpath+u"\n from "+URL) |
---|
387 | s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
388 | out,err = s.communicate() |
---|
389 | if err: |
---|
390 | print(60*"=") |
---|
391 | print ("****** An error was noted, see below *********") |
---|
392 | print(60*"=") |
---|
393 | print 'out=',out |
---|
394 | print 'err=',err |
---|
395 | return False |
---|
396 | if verbose: |
---|
397 | print('=== Output from svn switch'+(43*'=')) |
---|
398 | print(out.strip()) |
---|
399 | print((70*'=')+'\n') |
---|
400 | return True |
---|
401 | |
---|
402 | def svnInstallDir(URL,loadpath): |
---|
403 | '''Load a subversion tree into a specified directory |
---|
404 | |
---|
405 | :param str URL: the repository URL |
---|
406 | :param str loadpath: path to locate files |
---|
407 | |
---|
408 | ''' |
---|
409 | svn = whichsvn() |
---|
410 | if not svn: return |
---|
411 | cmd = [svn,'co',URL,loadpath,'--non-interactive'] |
---|
412 | if svnVersionNumber() >= 1.6: cmd += ['--trust-server-cert'] |
---|
413 | print("Loading files from "+URL) |
---|
414 | if proxycmds: cmd += proxycmds |
---|
415 | s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
416 | out,err = s.communicate() #this fails too easily |
---|
417 | if err: |
---|
418 | print(60*"=") |
---|
419 | print ("****** An error was noted, see below *********") |
---|
420 | print(60*"=") |
---|
421 | print err |
---|
422 | return False |
---|
423 | print ("Files installed at: "+loadpath) |
---|
424 | return True |
---|
425 | |
---|
426 | def DownloadG2Binaries(g2home,verbose=True): |
---|
427 | '''Download GSAS-II binaries from appropriate section of the |
---|
428 | GSAS-II svn repository based on the platform, numpy and Python |
---|
429 | version |
---|
430 | ''' |
---|
431 | # convert version numbers as '1.2.3' to integers (1002) and back (to 1.2) |
---|
432 | fmtver = lambda v: str(v//1000)+'.'+str(v%1000) |
---|
433 | intver = lambda vs: sum([int(i) for i in vs.split('.')[0:2]]*np.array((1000,1))) |
---|
434 | |
---|
435 | if sys.platform == "win32": |
---|
436 | prefix = 'win' |
---|
437 | elif sys.platform == "darwin": |
---|
438 | prefix = 'mac' |
---|
439 | elif sys.platform == "linux2": |
---|
440 | prefix = 'linux' |
---|
441 | else: |
---|
442 | print(u'Unknown platform: '+sys.platform) |
---|
443 | raise Exception('Unknown platform') |
---|
444 | if platform.architecture()[0] == '64bit': |
---|
445 | bits = '64' |
---|
446 | else: |
---|
447 | bits = '32' |
---|
448 | |
---|
449 | # format current python & numpy versions |
---|
450 | pyver = 'p{}.{}'.format(*sys.version_info[0:2]) |
---|
451 | npver = 'n{}.{}'.format(*np.__version__.split('.')[0:2]) |
---|
452 | inpver = intver(np.__version__) |
---|
453 | |
---|
454 | items = [prefix,bits,pyver] |
---|
455 | bindir = '_'.join(items) |
---|
456 | |
---|
457 | svn = whichsvn() |
---|
458 | if not svn: |
---|
459 | print('**** unable to load files: svn not found ****') |
---|
460 | return '' |
---|
461 | # get binaries matching the required type -- other than for the numpy version |
---|
462 | cmd = [svn, 'list', g2home + '/Binaries/'] |
---|
463 | if proxycmds: cmd += proxycmds |
---|
464 | p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
465 | res,err = p.communicate() |
---|
466 | versions = {} |
---|
467 | for d in res.split(): |
---|
468 | if d.startswith(bindir): |
---|
469 | v = intver(d.rstrip('/').split('_')[3].lstrip('n')) |
---|
470 | versions[v] = d |
---|
471 | intVersionsList = sorted(versions.keys()) |
---|
472 | if inpver < min(intVersionsList): |
---|
473 | vsel = min(intVersionsList) |
---|
474 | print('Warning: The current numpy version, {}, is older than\n\tthe oldest dist version, {}' |
---|
475 | .format(np.__version__,fmtver(vsel))) |
---|
476 | elif inpver >= max(intVersionsList): |
---|
477 | vsel = max(intVersionsList) |
---|
478 | if verbose: print( |
---|
479 | 'FYI: The current numpy version, {}, is newer than the newest dist version {}' |
---|
480 | .format(np.__version__,fmtver(vsel))) |
---|
481 | else: |
---|
482 | vsel = min(intVersionsList) |
---|
483 | for v in intVersionsList: |
---|
484 | if v <= inpver: |
---|
485 | vsel = v |
---|
486 | else: |
---|
487 | if verbose: print( |
---|
488 | 'FYI: Selecting dist version {} as the current numpy version, {},\n\tis older than the next dist version {}' |
---|
489 | .format(fmtver(vsel),np.__version__,fmtver(v))) |
---|
490 | break |
---|
491 | distdir = g2home + '/Binaries/' + versions[vsel] |
---|
492 | # switch reset command: distdir = g2home + '/trunk/bindist' |
---|
493 | svnSwitchDir('bindist','',distdir,verbose=verbose) |
---|
494 | return os.path.join(path2GSAS2,'bindist') |
---|
495 | |
---|
496 | def IPyBreak_base(): |
---|
497 | '''A routine that invokes an IPython session at the calling location |
---|
498 | This routine is only used when debug=True is set in config.py |
---|
499 | ''' |
---|
500 | savehook = sys.excepthook # save the exception hook |
---|
501 | try: |
---|
502 | from IPython.terminal.embed import InteractiveShellEmbed |
---|
503 | except ImportError: |
---|
504 | try: |
---|
505 | # try the IPython 0.12 approach |
---|
506 | from IPython.frontend.terminal.embed import InteractiveShellEmbed |
---|
507 | except ImportError: |
---|
508 | print 'IPython InteractiveShellEmbed not found' |
---|
509 | return |
---|
510 | import inspect |
---|
511 | ipshell = InteractiveShellEmbed() |
---|
512 | |
---|
513 | frame = inspect.currentframe().f_back |
---|
514 | msg = 'Entering IPython console inside {0.f_code.co_filename} at line {0.f_lineno}\n'.format(frame) |
---|
515 | ipshell(msg,stack_depth=2) # Go up one level, to see the calling routine |
---|
516 | sys.excepthook = savehook # reset IPython's change to the exception hook |
---|
517 | |
---|
518 | def exceptHook(*args): |
---|
519 | '''A routine to be called when an exception occurs. It prints the traceback |
---|
520 | with fancy formatting and then calls an IPython shell with the environment |
---|
521 | of the exception location. |
---|
522 | |
---|
523 | This routine is only used when debug=True is set in config.py |
---|
524 | ''' |
---|
525 | from IPython.core import ultratb |
---|
526 | if 'win' in sys.platform: |
---|
527 | ultratb.FormattedTB(call_pdb=False,color_scheme='NoColor')(*args) |
---|
528 | else: |
---|
529 | ultratb.FormattedTB(call_pdb=False,color_scheme='LightBG')(*args) |
---|
530 | try: |
---|
531 | from IPython.terminal.embed import InteractiveShellEmbed |
---|
532 | except ImportError: |
---|
533 | try: |
---|
534 | # try the IPython 0.12 approach |
---|
535 | from IPython.frontend.terminal.embed import InteractiveShellEmbed |
---|
536 | except ImportError: |
---|
537 | print 'IPython InteractiveShellEmbed not found' |
---|
538 | return |
---|
539 | import inspect |
---|
540 | frame = inspect.getinnerframes(args[2])[-1][0] |
---|
541 | msg = 'Entering IPython console at {0.f_code.co_filename} at line {0.f_lineno}\n'.format(frame) |
---|
542 | savehook = sys.excepthook # save the exception hook |
---|
543 | try: |
---|
544 | InteractiveShellEmbed(banner1=msg)(local_ns=frame.f_locals,global_ns=frame.f_globals) |
---|
545 | except: # use a different call for IPython 5 |
---|
546 | class c(object): pass |
---|
547 | pseudomod = c() # create something that acts like a module |
---|
548 | pseudomod.__dict__ = frame.f_locals |
---|
549 | InteractiveShellEmbed(banner1=msg)(module=pseudomod,global_ns=frame.f_globals) |
---|
550 | sys.excepthook = savehook # reset IPython's change to the exception hook |
---|
551 | |
---|
552 | def DoNothing(): |
---|
553 | '''A routine that does nothing. This is called in place of IPyBreak and pdbBreak |
---|
554 | except when the debug option is set True in config.py |
---|
555 | ''' |
---|
556 | pass |
---|
557 | |
---|
558 | IPyBreak = DoNothing |
---|
559 | pdbBreak = DoNothing |
---|
560 | def InvokeDebugOpts(): |
---|
561 | 'Called in GSASII.py to set up debug options' |
---|
562 | if GetConfigValue('debug'): |
---|
563 | print 'Debug on: IPython: Exceptions and G2path.IPyBreak(); pdb: G2path.pdbBreak()' |
---|
564 | sys.excepthook = exceptHook |
---|
565 | import pdb |
---|
566 | global pdbBreak |
---|
567 | pdbBreak = pdb.set_trace |
---|
568 | global IPyBreak |
---|
569 | IPyBreak = IPyBreak_base |
---|
570 | |
---|
571 | def TestSPG(fpth): |
---|
572 | '''Test if pyspg.[so,.pyd] can be run from a location in the path |
---|
573 | ''' |
---|
574 | if not os.path.exists(fpth): return False |
---|
575 | if not glob.glob(os.path.join(fpth,'pyspg.*')): return False |
---|
576 | savpath = sys.path[:] |
---|
577 | sys.path = [fpth] |
---|
578 | # test to see if a shared library can be used |
---|
579 | try: |
---|
580 | import pyspg |
---|
581 | pyspg.sgforpy('P -1') |
---|
582 | except Exception as err: |
---|
583 | print(70*'=') |
---|
584 | print('Failed to run pyspg in {}\nerror: {}'.format(fpth,err)) |
---|
585 | print(70*'=') |
---|
586 | sys.path = savpath |
---|
587 | return False |
---|
588 | sys.path = savpath |
---|
589 | return True |
---|
590 | |
---|
591 | # see if a directory for local modifications is defined. If so, stick that in the path |
---|
592 | if os.path.exists(os.path.expanduser('~/.G2local/')): |
---|
593 | sys.path.insert(0,os.path.expanduser('~/.G2local/')) |
---|
594 | import glob |
---|
595 | fl = glob.glob(os.path.expanduser('~/.G2local/GSASII*.py*')) |
---|
596 | files = "" |
---|
597 | prev = None |
---|
598 | for f in sorted(fl): # make a list of files, dropping .pyc files where a .py exists |
---|
599 | f = os.path.split(f)[1] |
---|
600 | if os.path.splitext(f)[0] == prev: continue |
---|
601 | prev = os.path.splitext(f)[0] |
---|
602 | if files: files += ", " |
---|
603 | files += f |
---|
604 | if files: |
---|
605 | print("*"*75) |
---|
606 | print("Warning: the following source files are locally overridden in "+os.path.expanduser('~/.G2local/')) |
---|
607 | print(" "+files) |
---|
608 | print("*"*75) |
---|
609 | ################################################################################ |
---|
610 | # commands below are executed during the first import of this file |
---|
611 | ################################################################################ |
---|
612 | # Add location of GSAS-II shared libraries (binaries: .so or .pyd files) to path |
---|
613 | binpath = None |
---|
614 | for loc in os.path.abspath(sys.path[0]),os.path.abspath(os.path.split(__file__)[0]): |
---|
615 | # Look at bin directory (created by a local compile) before standard dist |
---|
616 | # that at the top of the path |
---|
617 | for d in 'bin','bindist': |
---|
618 | if not d: continue |
---|
619 | fpth = os.path.join(loc,d) |
---|
620 | if TestSPG(fpth): |
---|
621 | binpath = fpth |
---|
622 | break |
---|
623 | if binpath: break |
---|
624 | if binpath: # were GSAS-II binaries found |
---|
625 | sys.path.insert(0,binpath) |
---|
626 | print('GSAS-II binary directory: {}'.format(binpath)) |
---|
627 | else: # try loading them |
---|
628 | print('Attempting to download GSAS-II binary files...') |
---|
629 | try: |
---|
630 | binpath = DownloadG2Binaries(g2home) |
---|
631 | except AttributeError: |
---|
632 | print('Problem with download') |
---|
633 | if TestSPG(binpath): |
---|
634 | print('GSAS-II binary directory: {}'.format(binpath)) |
---|
635 | sys.path.insert(0,binpath) |
---|
636 | #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
637 | #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
638 | # patch: use old location based on the host OS and the python version, |
---|
639 | # path is relative to location of the script that is called as well as this file |
---|
640 | # this must be imported before anything that imports any .pyd/.so file for GSASII |
---|
641 | else: |
---|
642 | print('\n'+75*'*') |
---|
643 | print(' Warning. Using an old-style GSAS-II binary library. This is unexpected') |
---|
644 | print(' and will break in future GSAS-II versions. Please contact toby@anl.gov') |
---|
645 | print(' so we can learn what is not working on your installation.') |
---|
646 | bindir = None |
---|
647 | if sys.platform == "win32": |
---|
648 | if platform.architecture()[0] == '64bit': |
---|
649 | bindir = 'binwin64-%d.%d' % sys.version_info[0:2] |
---|
650 | else: |
---|
651 | bindir = 'binwin%d.%d' % sys.version_info[0:2] |
---|
652 | elif sys.platform == "darwin": |
---|
653 | if platform.architecture()[0] == '64bit': |
---|
654 | bindir = 'binmac64-%d.%d' % sys.version_info[0:2] |
---|
655 | else: |
---|
656 | bindir = 'binmac%d.%d' % sys.version_info[0:2] |
---|
657 | #if platform.mac_ver()[0].startswith('10.5.'): |
---|
658 | # bindir += '_10.5' |
---|
659 | elif sys.platform == "linux2": |
---|
660 | if platform.architecture()[0] == '64bit': |
---|
661 | bindir = 'binlinux64-%d.%d' % sys.version_info[0:2] |
---|
662 | else: |
---|
663 | bindir = 'binlinux%d.%d' % sys.version_info[0:2] |
---|
664 | for loc in os.path.abspath(sys.path[0]),os.path.abspath(os.path.split(__file__)[0]): |
---|
665 | # Look at bin directory (created by a local compile) before standard dist |
---|
666 | # that at the top of the path |
---|
667 | fpth = os.path.join(loc,bindir) |
---|
668 | if TestSPG(fpth): |
---|
669 | binpath = fpth |
---|
670 | sys.path.insert(0,binpath) |
---|
671 | print('GSAS-II binary directory: {}'.format(binpath)) |
---|
672 | print(75*'*') |
---|
673 | break |
---|
674 | else: |
---|
675 | #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
676 | raise Exception,"**** ERROR GSAS-II binary libraries not found, GSAS-II cannot run ****" |
---|
677 | |
---|
678 | # add the data import and export directory to the search path |
---|
679 | newpath = os.path.join(path2GSAS2,'imports') |
---|
680 | if newpath not in sys.path: sys.path.append(newpath) |
---|
681 | newpath = os.path.join(path2GSAS2,'exports') |
---|
682 | if newpath not in sys.path: sys.path.append(newpath) |
---|
683 | |
---|
684 | # setup read of config.py, if present |
---|
685 | try: |
---|
686 | import config |
---|
687 | configDict = config.__dict__ |
---|
688 | import inspect |
---|
689 | vals = [True for i in inspect.getmembers(config) if '__' not in i[0]] |
---|
690 | print str(len(vals))+' values read from config file '+os.path.abspath(config.__file__) |
---|
691 | except ImportError: |
---|
692 | configDict = {'Clip_on':True} |
---|
693 | except Exception as err: |
---|
694 | print("Error importing config.py file: "+str(err)) |
---|
695 | configDict = {'Clip_on':True} |
---|
696 | |
---|
697 | if __name__ == '__main__': |
---|
698 | '''What follows is called to update (or downdate) GSAS-II in a separate process. |
---|
699 | ''' |
---|
700 | import time |
---|
701 | time.sleep(1) # delay to give the main process a chance to exit |
---|
702 | # perform an update and restart GSAS-II |
---|
703 | project,version = sys.argv[1:3] |
---|
704 | loc = os.path.dirname(__file__) |
---|
705 | if version: |
---|
706 | print("Regress to version "+str(version)) |
---|
707 | svnUpdateDir(loc,version=version) |
---|
708 | else: |
---|
709 | print("Update to current version") |
---|
710 | svnUpdateDir(loc) |
---|
711 | if project: |
---|
712 | print("Restart GSAS-II with project file "+str(project)) |
---|
713 | subprocess.Popen([sys.executable,os.path.join(loc,'GSASII.py'),project]) |
---|
714 | else: |
---|
715 | print("Restart GSAS-II without a project file ") |
---|
716 | subprocess.Popen([sys.executable,os.path.join(loc,'GSASII.py')]) |
---|
717 | print 'exiting update process' |
---|
718 | sys.exit() |
---|