source: trunk/GSASIIpath.py @ 2925

Last change on this file since 2925 was 2925, checked in by toby, 6 years ago

add switch to 2frame; allow for latest G2path

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 28.8 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 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
55version = -1
56def SetVersionNumber(RevString):
57    '''Set the subversion version number
58
59    :param str RevString: something like "$Revision: 2925 $"
60      that is set by subversion when the file is retrieved from subversion.
61
62    Place ``GSASIIpath.SetVersionNumber("$Revision: 2925 $")`` 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       
72def 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
80def 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
108proxycmds = []
109'Used to hold proxy information for subversion, set if needed in whichsvn'
110svnLocCache = None
111'Cached location of svn to avoid multiple searches for it'
112
113def 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
164def 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
184def 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
196def 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
233svnLastError = ''
234def svnGetRev(fpath=os.path.split(__file__)[0],local=True):
235    '''Obtain the version number for the either the last update of the local version
236    or contacts the subversion server to get the latest update version (# of Head).
237
238    :param str fpath: path to repository dictionary, defaults to directory where
239       the current file is located
240    :param bool local: determines the type of version number, where
241       True (default): returns the latest installed update
242       False: returns the version number of Head on the server
243
244    :Returns: the version number as an str or
245       None if there is a subversion error (likely because the path is
246       not a repository or svn is not found). The error message is placed in
247       global variable svnLastError
248    '''
249
250    import xml.etree.ElementTree as ET
251    svn = whichsvn()
252    if not svn: return
253    if local:
254        cmd = [svn,'info',fpath,'--xml']
255    else:
256        cmd = [svn,'info',fpath,'--xml','-rHEAD']
257    if svnVersionNumber() >= 1.6:
258        cmd += ['--non-interactive', '--trust-server-cert']
259    if proxycmds: cmd += proxycmds
260    s = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
261    out,err = s.communicate()
262    if err:
263        print 'svn failed\n',out
264        print 'err=',err
265        global svnLastError
266        svnLastError = err
267        return None
268    x = ET.fromstring(out)
269    for i in x.iter('entry'):
270        rev = i.attrib.get('revision')
271        if rev: return rev
272
273def svnFindLocalChanges(fpath=os.path.split(__file__)[0]):
274    '''Returns a list of files that were changed locally. If no files are changed,
275       the list has length 0
276
277    :param fpath: path to repository dictionary, defaults to directory where
278       the current file is located
279
280    :returns: None if there is a subversion error (likely because the path is
281       not a repository or svn is not found)
282
283    '''
284    import xml.etree.ElementTree as ET
285    svn = whichsvn()
286    if not svn: return
287    cmd = [svn,'status',fpath,'--xml']
288    if proxycmds: cmd += proxycmds
289    s = subprocess.Popen(cmd,
290                         stdout=subprocess.PIPE,stderr=subprocess.PIPE)
291    out,err = s.communicate()
292    if err: return None
293    x = ET.fromstring(out)
294    changed = []
295    for i in x.iter('entry'):
296        if i.find('wc-status').attrib.get('item') == 'modified': 
297            changed.append(i.attrib.get('path'))
298    return changed
299
300def svnUpdateDir(fpath=os.path.split(__file__)[0],version=None):
301    '''This performs an update of the files in a local directory from a server.
302
303    :param str fpath: path to repository dictionary, defaults to directory where
304       the current file is located
305    :param version: the number of the version to be loaded. Used only
306       cast as a string, but should be an integer or something that corresponds to a
307       string representation of an integer value when cast. A value of None (default)
308       causes the latest version on the server to be used.
309    '''
310    svn = whichsvn()
311    if not svn: return
312    if version:
313        verstr = '-r' + str(version)
314    else:
315        verstr = '-rHEAD'
316    cmd = [svn,'update',fpath,verstr,
317           '--non-interactive',
318           '--accept','theirs-conflict','--force']
319    if svnVersionNumber() >= 1.6:
320        cmd += ['--trust-server-cert']
321    if proxycmds: cmd += proxycmds
322    s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
323    out,err = s.communicate()
324    if err:
325        print(60*"=")
326        print ("****** An error was noted, see below *********")
327        print(60*"=")
328        print err
329        sys.exit()
330
331def svnUpgrade(fpath=os.path.split(__file__)[0]):
332    '''This reformats subversion files, which may be needed if an upgrade of subversion is
333    done.
334
335    :param str fpath: path to repository dictionary, defaults to directory where
336       the current file is located
337    '''
338    svn = whichsvn()
339    if not svn: return
340    cmd = [svn,'upgrade',fpath,'--non-interactive']
341    if proxycmds: cmd += proxycmds
342    s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
343    out,err = s.communicate()
344    if err:
345        print("svn upgrade did not happen (this is probably OK). Messages:")
346        print err
347           
348def svnUpdateProcess(version=None,projectfile=None,branch=None):
349    '''perform an update of GSAS-II in a separate python process'''
350    if not projectfile:
351        projectfile = ''
352    else:
353        projectfile = os.path.realpath(projectfile)
354        print 'restart using',projectfile
355    if branch:
356        version = branch
357    elif not version:
358        version = ''
359    else:
360        version = str(version)
361    # start the upgrade in a separate interpreter (avoids loading .pyd files)
362    subprocess.Popen([sys.executable,__file__,projectfile,version])
363    sys.exit()
364
365def svnSwitchDir(rpath,filename,baseURL,loadpath=None,verbose=True):
366    '''This performs a switch command to move files between subversion trees.
367    Note that if the files were previously downloaded,
368    the switch command will update the files to the newest version.
369   
370    :param str rpath: path to locate files, relative to the GSAS-II
371      installation path (defaults to path2GSAS2)
372    :param str URL: the repository URL
373    :param str loadpath: the prefix for the path, if specified. Defaults to path2GSAS2
374    :param bool verbose: if True (default) diagnostics are printed
375    '''
376    svn = whichsvn()
377    if not svn: return
378    URL = baseURL[:]
379    if baseURL[-1] != '/':
380        URL = baseURL + '/' + filename
381    else:
382        URL = baseURL + filename
383    if loadpath:
384        fpath = os.path.join(loadpath,rpath,filename)
385    else:
386        fpath = os.path.join(path2GSAS2,rpath,filename)
387    cmd = [svn,'switch',URL,fpath,
388           '--non-interactive','--trust-server-cert',
389           '--accept','theirs-conflict','--force']
390    if svnVersionNumber(svn) > 1.6: cmd += ['--ignore-ancestry']
391    if proxycmds: cmd += proxycmds
392    if verbose: print(u"Loading files to "+fpath+u"\n  from "+URL)
393    s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
394    out,err = s.communicate()
395    if err:
396        print(60*"=")
397        print ("****** An error was noted, see below *********")
398        print(60*"=")
399        print 'out=',out
400        print 'err=',err
401        return False
402    if verbose:
403        print('=== Output from svn switch'+(43*'='))
404        print(out.strip())
405        print((70*'=')+'\n')
406    return True
407
408def svnInstallDir(URL,loadpath):
409    '''Load a subversion tree into a specified directory
410
411    :param str URL: the repository URL
412    :param str loadpath: path to locate files
413
414    '''
415    svn = whichsvn()
416    if not svn: return
417    cmd = [svn,'co',URL,loadpath,'--non-interactive']
418    if svnVersionNumber() >= 1.6: cmd += ['--trust-server-cert']
419    print("Loading files from "+URL)
420    if proxycmds: cmd += proxycmds
421    s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
422    out,err = s.communicate()   #this fails too easily
423    if err:
424        print(60*"=")
425        print ("****** An error was noted, see below *********")
426        print(60*"=")
427        print err
428        return False
429    print ("Files installed at: "+loadpath)
430    return True
431           
432def DownloadG2Binaries(g2home,verbose=True):
433    '''Download GSAS-II binaries from appropriate section of the
434    GSAS-II svn repository based on the platform, numpy and Python
435    version
436    '''
437    # convert version numbers as '1.2.3' to integers (1002) and back (to 1.2)
438    fmtver = lambda v: str(v//1000)+'.'+str(v%1000)
439    intver = lambda vs: sum([int(i) for i in vs.split('.')[0:2]]*np.array((1000,1)))
440   
441    if sys.platform == "win32":
442        prefix = 'win'
443    elif sys.platform == "darwin":
444        prefix = 'mac'
445    elif sys.platform == "linux2":
446        prefix = 'linux'
447    else:
448        print(u'Unknown platform: '+sys.platform)
449        raise Exception('Unknown platform')
450    if platform.architecture()[0] == '64bit':
451        bits = '64'
452    else:
453        bits = '32'
454
455    # format current python & numpy versions
456    pyver = 'p{}.{}'.format(*sys.version_info[0:2])
457    npver = 'n{}.{}'.format(*np.__version__.split('.')[0:2])
458    inpver = intver(np.__version__)
459
460    items = [prefix,bits,pyver]
461    bindir = '_'.join(items)
462
463    svn = whichsvn()
464    if not svn:
465        print('**** unable to load files: svn not found ****')
466        return ''
467    # get binaries matching the required type -- other than for the numpy version
468    cmd = [svn, 'list', g2home + '/Binaries/']
469    if proxycmds: cmd += proxycmds
470    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
471    res,err = p.communicate()
472    versions = {}
473    for d in res.split():
474        if d.startswith(bindir):
475            v = intver(d.rstrip('/').split('_')[3].lstrip('n'))
476            versions[v] = d
477    intVersionsList = sorted(versions.keys())
478    if inpver < min(intVersionsList):
479        vsel = min(intVersionsList)
480        print('Warning: The current numpy version, {}, is older than\n\tthe oldest dist version, {}'
481              .format(np.__version__,fmtver(vsel)))
482    elif inpver >= max(intVersionsList):
483        vsel = max(intVersionsList)
484        if verbose: print(
485                'FYI: The current numpy version, {}, is newer than the newest dist version {}'
486                .format(np.__version__,fmtver(vsel)))
487    else:
488        vsel = min(intVersionsList)
489        for v in intVersionsList:
490            if v <= inpver:
491                vsel = v
492            else:
493                if verbose: print(
494                        'FYI: Selecting dist version {} as the current numpy version, {},\n\tis older than the next dist version {}'
495                        .format(fmtver(vsel),np.__version__,fmtver(v)))
496                break
497    distdir = g2home + '/Binaries/' + versions[vsel]
498    # switch reset command: distdir = g2home + '/trunk/bindist'
499    svnSwitchDir('bindist','',distdir,verbose=verbose)
500    return os.path.join(path2GSAS2,'bindist')
501
502def svnTestBranch(loc=None):
503    '''Returns the name of the branch directory if the installation has been switched.
504    Returns none, if not a branch
505    the test 2frame branch. False otherwise
506    '''
507    if loc is None: loc = path2GSAS2
508    svn = whichsvn()
509    if not svn:
510        print('**** unable to load files: svn not found ****')
511        return ''
512    cmd = [svn, 'info', loc]
513    if proxycmds: cmd += proxycmds
514    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
515    res,err = p.communicate()
516    for l in res.split('\n'):
517        if "Relative URL:" in l: break
518    if "/branch/" in l:
519        return l[l.find("/branch/")+8:].strip()
520    else:
521        return None
522   
523def svnSwitch2branch(branch=None,loc=None,svnHome=None):
524    '''Switch to a subversion branch if specified. Switches to trunk otherwise.
525    '''
526    if svnHome is None: svnHome = g2home
527    svnURL = svnHome + '/trunk'
528    if branch:
529        if svnHome.endswith('/'):
530            svnURL = svnHome[:-1]
531        else:
532            svnURL = svnHome
533        if branch.startswith('/'):
534            svnURL += branch
535        else:
536            svnURL += '/' + branch
537    svnSwitchDir('','',svnURL,loadpath=loc)
538   
539
540def IPyBreak_base():
541    '''A routine that invokes an IPython session at the calling location
542    This routine is only used when debug=True is set in config.py
543    '''
544    savehook = sys.excepthook # save the exception hook
545    try: 
546        from IPython.terminal.embed import InteractiveShellEmbed
547    except ImportError:
548        try:
549            # try the IPython 0.12 approach
550            from IPython.frontend.terminal.embed import InteractiveShellEmbed
551        except ImportError:
552            print 'IPython InteractiveShellEmbed not found'
553            return
554    import inspect
555    ipshell = InteractiveShellEmbed()
556
557    frame = inspect.currentframe().f_back
558    msg   = 'Entering IPython console inside {0.f_code.co_filename} at line {0.f_lineno}\n'.format(frame)
559    ipshell(msg,stack_depth=2) # Go up one level, to see the calling routine
560    sys.excepthook = savehook # reset IPython's change to the exception hook
561
562def exceptHook(*args):
563    '''A routine to be called when an exception occurs. It prints the traceback
564    with fancy formatting and then calls an IPython shell with the environment
565    of the exception location.
566   
567    This routine is only used when debug=True is set in config.py   
568    '''
569    from IPython.core import ultratb
570    if 'win' in sys.platform:
571        ultratb.FormattedTB(call_pdb=False,color_scheme='NoColor')(*args)
572    else:
573        ultratb.FormattedTB(call_pdb=False,color_scheme='LightBG')(*args)
574    try: 
575        from IPython.terminal.embed import InteractiveShellEmbed
576    except ImportError:
577        try:
578            # try the IPython 0.12 approach
579            from IPython.frontend.terminal.embed import InteractiveShellEmbed
580        except ImportError:
581            print 'IPython InteractiveShellEmbed not found'
582            return
583    import inspect
584    frame = inspect.getinnerframes(args[2])[-1][0]
585    msg   = 'Entering IPython console at {0.f_code.co_filename} at line {0.f_lineno}\n'.format(frame)
586    savehook = sys.excepthook # save the exception hook
587    try:
588        InteractiveShellEmbed(banner1=msg)(local_ns=frame.f_locals,global_ns=frame.f_globals)
589    except: # use a different call for IPython 5
590        class c(object): pass
591        pseudomod = c() # create something that acts like a module
592        pseudomod.__dict__ = frame.f_locals
593        InteractiveShellEmbed(banner1=msg)(module=pseudomod,global_ns=frame.f_globals)
594    sys.excepthook = savehook # reset IPython's change to the exception hook
595
596def DoNothing():
597    '''A routine that does nothing. This is called in place of IPyBreak and pdbBreak
598    except when the debug option is set True in config.py
599    '''
600    pass 
601
602IPyBreak = DoNothing
603pdbBreak = DoNothing
604def InvokeDebugOpts():
605    'Called in GSASII.py to set up debug options'
606    if GetConfigValue('debug'):
607        print 'Debug on: IPython: Exceptions and G2path.IPyBreak(); pdb: G2path.pdbBreak()'
608        sys.excepthook = exceptHook
609        import pdb
610        global pdbBreak
611        pdbBreak = pdb.set_trace
612        global IPyBreak
613        IPyBreak = IPyBreak_base
614
615def TestSPG(fpth):
616    '''Test if pyspg.[so,.pyd] can be run from a location in the path
617    '''
618    if not os.path.exists(fpth): return False
619    if not glob.glob(os.path.join(fpth,'pyspg.*')): return False
620    savpath = sys.path[:]
621    sys.path = [fpth]
622    # test to see if a shared library can be used
623    try:
624        import pyspg
625        pyspg.sgforpy('P -1')
626    except Exception as err:
627        print(70*'=')
628        print('Failed to run pyspg in {}\nerror: {}'.format(fpth,err))
629        print(70*'=')
630        sys.path = savpath
631        return False
632    sys.path = savpath
633    return True
634   
635# see if a directory for local modifications is defined. If so, stick that in the path
636if os.path.exists(os.path.expanduser('~/.G2local/')):
637    sys.path.insert(0,os.path.expanduser('~/.G2local/'))
638    import glob
639    fl = glob.glob(os.path.expanduser('~/.G2local/GSASII*.py*'))
640    files = ""
641    prev = None
642    for f in sorted(fl): # make a list of files, dropping .pyc files where a .py exists
643        f = os.path.split(f)[1]
644        if os.path.splitext(f)[0] == prev: continue
645        prev = os.path.splitext(f)[0]
646        if files: files += ", "
647        files += f
648    if files:
649        print("*"*75)
650        print("Warning: the following source files are locally overridden in "+os.path.expanduser('~/.G2local/'))
651        print("  "+files)
652        print("*"*75)
653
654BinaryPathLoaded = False
655def SetBinaryPath():
656    '''
657    Add location of GSAS-II shared libraries (binaries: .so or .pyd files) to path
658   
659    This routine must be executed after GSASIIpath is imported and before any other
660    GSAS-II imports are done.
661    '''
662    # do this only once no matter how many times it is called
663    global BinaryPathLoaded
664    if BinaryPathLoaded: return
665    BinaryPathLoaded = True
666    binpath = None
667    for loc in os.path.abspath(sys.path[0]),os.path.abspath(os.path.split(__file__)[0]):
668        # Look at bin directory (created by a local compile) before standard dist
669        # that at the top of the path
670        for d in 'bin','bindist':
671            if not d: continue
672            fpth = os.path.join(loc,d)
673            if TestSPG(fpth):
674                binpath = fpth
675                break       
676        if binpath: break
677    if binpath:                                            # were GSAS-II binaries found
678        sys.path.insert(0,binpath)
679        print('GSAS-II binary directory: {}'.format(binpath))
680    else:                                                  # try loading them
681        print('Attempting to download GSAS-II binary files...')
682        try:
683            binpath = DownloadG2Binaries(g2home)
684        except AttributeError:   # this happens when building in Read The Docs
685            print('Problem with download')
686        if binpath and TestSPG(binpath):
687            print('GSAS-II binary directory: {}'.format(binpath))
688            sys.path.insert(0,binpath)
689        # this must be imported before anything that imports any .pyd/.so file for GSASII
690        else:
691            #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
692            #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
693            # patch: use old location based on the host OS and the python version, 
694            # path is relative to location of the script that is called as well as this file
695            bindir = None
696            if sys.platform == "win32":
697                if platform.architecture()[0] == '64bit':
698                    bindir = 'binwin64-%d.%d' % sys.version_info[0:2]
699                else:
700                    bindir = 'binwin%d.%d' % sys.version_info[0:2]
701            elif sys.platform == "darwin":
702                if platform.architecture()[0] == '64bit':
703                    bindir = 'binmac64-%d.%d' % sys.version_info[0:2]
704                else:
705                    bindir = 'binmac%d.%d' % sys.version_info[0:2]
706                #if platform.mac_ver()[0].startswith('10.5.'):
707                #    bindir += '_10.5'
708            elif sys.platform == "linux2":
709                if platform.architecture()[0] == '64bit':
710                    bindir = 'binlinux64-%d.%d' % sys.version_info[0:2]
711                else:
712                    bindir = 'binlinux%d.%d' % sys.version_info[0:2]
713            for loc in os.path.abspath(sys.path[0]),os.path.abspath(os.path.split(__file__)[0]):
714            # Look at bin directory (created by a local compile) before standard dist
715            # that at the top of the path
716                fpth = os.path.join(loc,bindir)
717                binpath = fpth
718                if TestSPG(fpth):
719                    sys.path.insert(0,binpath)
720                    print('\n'+75*'*')
721                    print('  Warning. Using an old-style GSAS-II binary library. This is unexpected')
722                    print('  and will break in future GSAS-II versions. Please contact toby@anl.gov')
723                    print('  so we can learn what is not working on your installation.')
724                    print('GSAS-II binary directory: {}'.format(binpath))
725                    print(75*'*')
726                    break
727            else:
728            # end patch
729            #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
730            #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
731                print(75*'*')
732                print('Use of GSAS-II binary directory {} failed!'.format(binpath))
733                print(75*'*')
734                raise Exception,"**** ERROR GSAS-II binary libraries not found, GSAS-II cannot run ****"
735
736    # add the data import and export directory to the search path
737    newpath = os.path.join(path2GSAS2,'imports')
738    if newpath not in sys.path: sys.path.append(newpath)
739    newpath = os.path.join(path2GSAS2,'exports')
740    if newpath not in sys.path: sys.path.append(newpath)
741
742    # setup read of config.py, if present
743    global configDict
744    try:
745        import config
746        configDict = config.__dict__
747        import inspect
748        vals = [True for i in inspect.getmembers(config) if '__' not in i[0]]
749        print str(len(vals))+' values read from config file '+os.path.abspath(config.__file__)
750    except ImportError:
751        configDict = {'Clip_on':True}
752    except Exception as err:
753        print("Error importing config.py file: "+str(err))
754        configDict = {'Clip_on':True}
755
756if __name__ == '__main__':
757    '''What follows is called to update (or downdate) GSAS-II in a separate process.
758    '''
759    import time
760    time.sleep(1) # delay to give the main process a chance to exit
761    # perform an update and restart GSAS-II
762    project,version = sys.argv[1:3]
763    loc = os.path.dirname(__file__)
764    if version == 'trunk':
765        svnSwitch2branch('')
766    elif '/' in version:
767        svnSwitch2branch(version)
768    elif version:
769        print("Regress to version "+str(version))
770        svnUpdateDir(loc,version=version)
771    else:
772        print("Update to current version")
773        svnUpdateDir(loc)
774    ex = sys.executable
775    if sys.platform == "darwin": # mac requires pythonw which is not always reported as sys.executable
776        if os.path.exists(ex+'w'): ex += 'w'
777    if project:
778        print("Restart GSAS-II with project file "+str(project))
779        subprocess.Popen([ex,os.path.join(loc,'GSASII.py'),project])
780    else:
781        print("Restart GSAS-II without a project file ")
782        subprocess.Popen([ex,os.path.join(loc,'GSASII.py')])
783    print 'exiting update process'
784    sys.exit()
Note: See TracBrowser for help on using the repository browser.