source: branch/2frame/GSASIIpath.py @ 2927

Last change on this file since 2927 was 2927, checked in by vondreele, 5 years ago

set up a local version of basinhopping.py in MCSA routine
implement frame position saving - NB: doesn't check if frame outside screen!
fix display of image controls after calibrate/recalibrate, etc.
occupancy --> site fraction in Afrac display
replace a bunch of TextCtrls? with ValidatedTxtCtrls? in phaseGUI

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