Changeset 3514 for install/bootstrap.py


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/bootstrap.py

    r3201 r3514  
    22# Installs GSAS-II from network using subversion and creates platform-specific shortcuts.
    33# works for Mac & Linux & Windows
    4 import os, stat, sys, platform, subprocess
    5 
     4import os, stat, sys, platform, subprocess, datetime
     5
     6version = "$Id: 35xx $"
    67g2home = 'https://subversion.xray.aps.anl.gov/pyGSAS/'
    78path2GSAS2 = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    8 print('Running bootstrap from {}'.format(path2GSAS2))
     9now = str(datetime.datetime.now())
     10print('Running bootstrap from {} at {}\n\tId: {}'.format(path2GSAS2,now,version))
     11fp = open(os.path.join(path2GSAS2,'bootstrap.log'),'a')
     12fp.write('Running bootstrap from {} at {}\n\tId: {}\n'.format(path2GSAS2,now,version))
     13fp.close()
    914################################################################################
    1015################################################################################
     16def GetConfigValue(*args): return True
    1117# routines copied from GSASIIpath.py
    1218proxycmds = []
     
    1521'Cached location of svn to avoid multiple searches for it'
    1622
     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       
    1777def whichsvn():
    1878    '''Returns a path to the subversion exe file, if any is found.
     
    3090    svnprog = 'svn'
    3191    if sys.platform.startswith('win'): svnprog += '.exe'
    32     gsaspath = os.path.split(__file__)[0]
    33     # check for a proxy
    34     proxyinfo = os.path.join(gsaspath,"proxyinfo.txt")
    35     if os.path.exists(proxyinfo):
    36         global proxycmds
    37         proxycmds = []
    38         fp = open(proxyinfo,'r')
    39         host = fp.readline().strip()
    40         port = fp.readline().strip()
    41         fp.close()
    42         proxycmds.append('--config-option')
    43         proxycmds.append('servers:global:http-proxy-host='+host)
    44         proxycmds.append('--config-option')
    45         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))
    4695    # add likely places to find subversion when installed with GSAS-II
    4796    pathlist = os.environ["PATH"].split(os.pathsep)
    48     pathlist.append(os.path.split(sys.executable)[0])
    49     pathlist.append(gsaspath)
     97    pathlist.insert(0,os.path.split(sys.executable)[0])
     98    pathlist.insert(1,path2GSAS2)
    5099    for rpt in ('..','bin'),('..','Library','bin'),('svn','bin'),('svn',),('.'):
    51         pt = os.path.normpath(os.path.join(gsaspath,*rpt))
     100        pt = os.path.normpath(os.path.join(path2GSAS2,*rpt))
    52101        if os.path.exists(pt):
    53102            pathlist.insert(0,pt)   
     
    79128    s = subprocess.Popen(cmd,
    80129                         stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    81     out,err = s.communicate()
    82     if err:
    83         print('subversion error!\nout= '+out)
    84         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)
    85137        return None
    86138    return out.strip()
     
    165217            sys.exit()
    166218
     219host = None
     220port = '80'
    167221print('\nChecking for subversion...')
    168 svn = whichsvn()
     222svn = whichsvn() # resets host & port if proxyinfo.txt is found
    169223if not svn:
    170224    print("Sorry, subversion (svn) could not be found on your system.")
     
    182236proxycmds = []
    183237proxyinfo = os.path.join(path2GSAS2,"proxyinfo.txt")
    184 host = None
    185 port = '8080'
    186238if os.path.exists(proxyinfo):
    187239    fp = open(proxyinfo,'r')
     
    195247else:
    196248    getinput = input
     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:
     257    for i in os.environ.keys():
     258        if 'http_proxy' == i.lower():
     259            key = i
     260            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]
     268    else:
     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 = ""
     277    else:
     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
     283        port = ans
     284except EOFError:
     285    host = ""
     286    port = ""
    197287if host:
    198     ans = getinput("Enter the proxy address (type none to remove) ["+host+"]: ")
    199     if ans.strip().lower() == "none": host = ans = ""
    200 else:
    201     ans = getinput("Enter the proxy address [none needed]: ")
    202 if ans.strip() != "" or host:
    203288    proxycmds.append('--config-option')
    204     if ans.strip() == "": ans=host
    205     proxycmds.append('servers:global:http-proxy-host='+ans.strip())
     289    proxycmds.append('servers:global:http-proxy-host='+host.strip())
     290    if port:
     291        proxycmds.append('--config-option')
     292        proxycmds.append('servers:global:http-proxy-port='+port.strip())
    206293    fp = open(proxyinfo,'w')
    207     fp.write(ans.strip()+'\n')
    208     ans = getinput("Enter the proxy port ["+port+"]: ")
    209     if ans.strip() == "": ans=port
    210     proxycmds.append('--config-option')
    211     proxycmds.append('servers:global:http-proxy-port='+ans.strip())
    212     fp.write(ans.strip()+'\n')
     294    fp.write(host.strip()+'\n')
     295    fp.write(port.strip()+'\n')
     296    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))
    213300    fp.close()
    214301
     
    242329        print(res)
    243330
    244 cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2, '--non-interactive', '--trust-server-cert']
    245 if proxycmds: cmd += proxycmds
    246 msg = 'Now preparing to install GSAS-II'
    247331print('\n'+75*'*')
    248 print(msg + u' from ' + cmd[2])
    249 msg = 'svn load command: '
    250 for item in cmd: msg += " "+item
    251 print(msg)
    252 s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
    253 print('\nsubversion output:')
    254 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
    255361if err:
    256     print('subversion returned an error:')
    257     print(err)
    258     print('Retrying with a cleanup and a command for older svn version...')
    259     cmd = [svn, 'cleanup', path2GSAS2]
    260     s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
    261     out,err = s.communicate()   
     362    print('Retrying with a command for older svn version...')
    262363    cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2]
    263364    if proxycmds: cmd += proxycmds
    264365    msg = ""
    265     for item in cmd: msg += item
     366    for item in cmd: msg += " " + item
    266367    print(msg)
    267368    s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
     
    270371        print('subversion returned an error:')
    271372        print(err)
    272         print('  *** GSAS-II failed to be installed. In case the cleanup worked, try running this again')
    273         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')
    274374        print('  *** problem, most commonly because you need to use a network proxy. Please')
    275375        print('  *** check with a network administrator or use http://www.whatismyproxy.com/\n')
Note: See TracChangeset for help on using the changeset viewer.