Changeset 3514 for install/g2pkg


Ignore:
Timestamp:
Jul 29, 2018 9:03:43 PM (3 years ago)
Author:
svnjenkins
Message:

fix bug with proxy input

File:
1 edited

Legend:

Unmodified
Added
Removed
  • install/g2pkg/src/bootstrap.py

    r3431 r3514  
    44import os, stat, sys, platform, subprocess, datetime
    55
     6version = "$Id: 35xx $"
    67g2home = 'https://subversion.xray.aps.anl.gov/pyGSAS/'
    78path2GSAS2 = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    89now = str(datetime.datetime.now())
    9 print('Running bootstrap from {} at {}'.format(path2GSAS2,now))
     10print('Running bootstrap from {} at {}\n\tId: {}'.format(path2GSAS2,now,version))
    1011fp = open(os.path.join(path2GSAS2,'bootstrap.log'),'a')
    11 fp.write('Running bootstrap from {} at {}\n'.format(path2GSAS2,now))
     12fp.write('Running bootstrap from {} at {}\n\tId: {}\n'.format(path2GSAS2,now,version))
    1213fp.close()
    1314################################################################################
    1415################################################################################
     16def GetConfigValue(*args): return True
    1517# routines copied from GSASIIpath.py
    1618proxycmds = []
     
    1921'Cached location of svn to avoid multiple searches for it'
    2022
     23def MakeByte2str(arg):
     24    '''Convert output from subprocess pipes (bytes) to str (unicode) in Python 3.
     25    In Python 2: Leaves output alone (already str).
     26    Leaves stuff of other types alone (including unicode in Py2)
     27    Works recursively for string-like stuff in nested loops and tuples.
     28
     29    typical use::
     30
     31        out = MakeByte2str(out)
     32
     33    or::
     34
     35        out,err = MakeByte2str(s.communicate())
     36   
     37    '''
     38    if isinstance(arg,str): return arg
     39    if isinstance(arg,bytes): return arg.decode()
     40    if isinstance(arg,list):
     41        return [MakeByte2str(i) for i in arg]
     42    if isinstance(arg,tuple):
     43        return tuple([MakeByte2str(i) for i in arg])
     44    return arg
     45
     46def getsvnProxy():
     47    '''Loads a proxy for subversion from the file created by bootstrap.py
     48    '''
     49    proxyinfo = os.path.join(path2GSAS2,"proxyinfo.txt")
     50    if os.path.exists(proxyinfo):
     51        global proxycmds
     52        global host,port  # only in bootstrap.py
     53        proxycmds = []
     54        fp = open(proxyinfo,'r')
     55        host = fp.readline().strip()
     56        port = fp.readline().strip()
     57        fp.close()
     58        setsvnProxy(host,port)
     59        if not host.strip(): return '',''
     60        return host,port
     61    return '',''
     62
     63def setsvnProxy(host,port):
     64    '''Sets the svn commands needed to use a proxy
     65    '''
     66    global proxycmds
     67    proxycmds = []
     68    host = host.strip()
     69    port = port.strip()
     70    if not host.strip(): return
     71    proxycmds.append('--config-option')
     72    proxycmds.append('servers:global:http-proxy-host='+host)
     73    if port.strip():
     74        proxycmds.append('--config-option')
     75        proxycmds.append('servers:global:http-proxy-port='+port)
     76       
    2177def whichsvn():
    2278    '''Returns a path to the subversion exe file, if any is found.
     
    3490    svnprog = 'svn'
    3591    if sys.platform.startswith('win'): svnprog += '.exe'
    36     gsaspath = os.path.split(__file__)[0]
    37     # check for a proxy
    38     proxyinfo = os.path.join(gsaspath,"proxyinfo.txt")
    39     if os.path.exists(proxyinfo):
    40         global proxycmds
    41         proxycmds = []
    42         fp = open(proxyinfo,'r')
    43         host = fp.readline().strip()
    44         port = fp.readline().strip()
    45         fp.close()
    46         proxycmds.append('--config-option')
    47         proxycmds.append('servers:global:http-proxy-host='+host)
    48         proxycmds.append('--config-option')
    49         proxycmds.append('servers:global:http-proxy-port='+port)
     92    host,port = getsvnProxy()
     93    if GetConfigValue('debug') and host:
     94        print('DBG_Using proxy host {} port {}'.format(host,port))
    5095    # add likely places to find subversion when installed with GSAS-II
    5196    pathlist = os.environ["PATH"].split(os.pathsep)
    52     pathlist.append(os.path.split(sys.executable)[0])
    53     pathlist.append(gsaspath)
     97    pathlist.insert(0,os.path.split(sys.executable)[0])
     98    pathlist.insert(1,path2GSAS2)
    5499    for rpt in ('..','bin'),('..','Library','bin'),('svn','bin'),('svn',),('.'):
    55         pt = os.path.normpath(os.path.join(gsaspath,*rpt))
     100        pt = os.path.normpath(os.path.join(path2GSAS2,*rpt))
    56101        if os.path.exists(pt):
    57102            pathlist.insert(0,pt)   
     
    83128    s = subprocess.Popen(cmd,
    84129                         stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    85     out,err = s.communicate()
    86     if err:
    87         print('subversion error!\nout= '+out)
    88         print('err= '+err)
     130    out,err = MakeByte2str(s.communicate())
     131    if err:
     132        print ('subversion error!\nout=%s'%out)
     133        print ('err=%s'%err)
     134        s = '\nsvn command:  '
     135        for i in cmd: s += i + ' '
     136        print(s)
    89137        return None
    90138    return out.strip()
     
    169217            sys.exit()
    170218
     219host = None
     220port = '80'
    171221print('\nChecking for subversion...')
    172 svn = whichsvn()
     222svn = whichsvn() # resets host & port if proxyinfo.txt is found
    173223if not svn:
    174224    print("Sorry, subversion (svn) could not be found on your system.")
     
    186236proxycmds = []
    187237proxyinfo = os.path.join(path2GSAS2,"proxyinfo.txt")
    188 host = None
    189 port = '80'
    190238if os.path.exists(proxyinfo):
    191239    fp = open(proxyinfo,'r')
     
    199247else:
    200248    getinput = input
    201 try:
    202     key = None
     249
     250# get proxy from environment variable
     251key = None
     252for i in os.environ.keys():
     253    if 'https_proxy' == i.lower():
     254        key = i
     255        break
     256else:
    203257    for i in os.environ.keys():
    204         if 'https_proxy' == i.lower():
     258        if 'http_proxy' == i.lower():
    205259            key = i
    206260            break
     261if key:
     262    val = os.environ[key].strip()
     263    if val[-1] == '/':
     264        val = val[:-1]
     265    if len(val.split(':')) > 2:
     266        host = ':'.join(val.split(':')[:-1])
     267        port = val.split(':')[-1]
    207268    else:
    208         for i in os.environ.keys():
    209             if 'http_proxy' == i.lower():
    210                 key = i
    211                 break
    212     if key:
    213         val = os.environ[key].strip()
    214         if val[-1] == '/':
    215             val = val[:-1]
    216         if len(val.split(':')) > 2:
    217             host = ':'.join(val.split(':')[:-1])
    218             port = val.split(':')[-1]
    219         else:
    220             host = ':'.join(val.split(':')[:-1])
    221             port = val.split(':')[-1]
    222     elif host:
    223         ans = getinput("Enter the proxy address (type none to remove) ["+host+"]: ")
    224         if ans.strip().lower() == "none": host = ans = ""
     269        host = ':'.join(val.split(':')[:-1])
     270        port = val.split(':')[-1]
     271
     272# get proxy from user, if terminal available
     273try:
     274    if host:
     275        ans = getinput("Enter the proxy address (type none to remove) ["+host+"]: ").strip()
     276        if ans.lower() == "none": host = ""
    225277    else:
    226         ans = getinput("Enter your proxy address [none needed]: ")
    227     if ans.strip() != "" or host:
    228         ans = getinput("Enter the proxy port ["+port+"]: ")
    229         if ans.strip() == "": ans=port
     278        ans = getinput("Enter your proxy address [none needed]: ").strip()
     279        if ans: host = ans
     280    if host:
     281        ans = getinput("Enter the proxy port ["+port+"]: ").strip()
     282        if ans == "": ans=port
    230283        port = ans
    231284except EOFError:
     
    242295    fp.write(port.strip()+'\n')
    243296    fp.close()
     297    fp = open(os.path.join(path2GSAS2,'bootstrap.log'),'a')
     298    fp.write('Proxy info written: {} port\n'.format(host,port))
     299    print('Proxy info written: {} port'.format(host,port))
     300    fp.close()
    244301
    245302# patch: switch GSAS-II location if linked to XOR server (removed May/June 2017)
     
    272329        print(res)
    273330
    274 cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2, '--non-interactive', '--trust-server-cert']
    275 if proxycmds: cmd += proxycmds
    276 msg = 'Now preparing to install GSAS-II'
    277331print('\n'+75*'*')
    278 print(msg + u' from ' + cmd[2])
    279 msg = 'svn load command: '
    280 for item in cmd: msg += " "+item
    281 print(msg)
    282 s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
    283 print('\nsubversion output:')
    284 out,err = s.communicate()
     332print('Now preparing to install GSAS-II')
     333tryagain = True
     334err = False
     335firstPass = 0
     336while(tryagain):
     337    tryagain = False
     338    if err:
     339        print('Retrying after a cleanup...')
     340        cmd = [svn, 'cleanup', path2GSAS2]
     341        s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
     342        out,err = s.communicate()   
     343        if err:
     344            print('subversion returned an error:')
     345            print(out)
     346            print(err)
     347    cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2, '--non-interactive', '--trust-server-cert']
     348    if proxycmds: cmd += proxycmds
     349    msg = 'svn load command: '
     350    for item in cmd: msg += " "+item
     351    print(msg)
     352    s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
     353    print('\nsubversion output:')
     354    out,err = s.communicate()
     355    if err:
     356        print('subversion returned an error:')
     357        print(out)
     358        print(err)
     359        if firstPass == 0: tryagain = True
     360    firstPass += 1
    285361if err:
    286     print('subversion returned an error:')
    287     print(err)
    288     print('Retrying with a cleanup and a command for older svn version...')
    289     cmd = [svn, 'cleanup', path2GSAS2]
    290     s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
    291     out,err = s.communicate()   
     362    print('Retrying with a command for older svn version...')
    292363    cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2]
    293364    if proxycmds: cmd += proxycmds
    294365    msg = ""
    295     for item in cmd: msg += item
     366    for item in cmd: msg += " " + item
    296367    print(msg)
    297368    s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
     
    300371        print('subversion returned an error:')
    301372        print(err)
    302         print('  *** GSAS-II failed to be installed. In case the cleanup worked, try running this again')
    303         print('  *** If this installation continues to fail, a likely reason is a network access')
     373        print('  *** GSAS-II failed to be installed. A likely reason is a network access')
    304374        print('  *** problem, most commonly because you need to use a network proxy. Please')
    305375        print('  *** check with a network administrator or use http://www.whatismyproxy.com/\n')
Note: See TracChangeset for help on using the changeset viewer.