source: install/g2pkg/src/bootstrap.py @ 3431

Last change on this file since 3431 was 3431, checked in by toby, 3 years ago

latest packaging/bootstrap mods

  • Property svn:eol-style set to native
File size: 14.8 KB
Line 
1#!/usr/bin/env python
2# Installs GSAS-II from network using subversion and creates platform-specific shortcuts.
3# works for Mac & Linux & Windows
4import os, stat, sys, platform, subprocess, datetime
5
6g2home = 'https://subversion.xray.aps.anl.gov/pyGSAS/'
7path2GSAS2 = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
8now = str(datetime.datetime.now())
9print('Running bootstrap from {} at {}'.format(path2GSAS2,now))
10fp = open(os.path.join(path2GSAS2,'bootstrap.log'),'a')
11fp.write('Running bootstrap from {} at {}\n'.format(path2GSAS2,now))
12fp.close()
13################################################################################
14################################################################################
15# routines copied from GSASIIpath.py
16proxycmds = []
17'Used to hold proxy information for subversion, set if needed in whichsvn'
18svnLocCache = None
19'Cached location of svn to avoid multiple searches for it'
20
21def whichsvn():
22    '''Returns a path to the subversion exe file, if any is found.
23    Searches the current path after adding likely places where GSAS-II
24    might install svn.
25
26    :returns: None if svn is not found or an absolute path to the subversion
27      executable file.
28    '''
29    # use a previosuly cached svn location
30    global svnLocCache
31    if svnLocCache: return svnLocCache
32    # prepare to find svn
33    is_exe = lambda fpath: os.path.isfile(fpath) and os.access(fpath, os.X_OK)
34    svnprog = 'svn'
35    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)
50    # add likely places to find subversion when installed with GSAS-II
51    pathlist = os.environ["PATH"].split(os.pathsep)
52    pathlist.append(os.path.split(sys.executable)[0])
53    pathlist.append(gsaspath)
54    for rpt in ('..','bin'),('..','Library','bin'),('svn','bin'),('svn',),('.'):
55        pt = os.path.normpath(os.path.join(gsaspath,*rpt))
56        if os.path.exists(pt):
57            pathlist.insert(0,pt)   
58    # search path for svn or svn.exe
59    for path in pathlist:
60        exe_file = os.path.join(path, svnprog)
61        if is_exe(exe_file):
62            try:
63                p = subprocess.Popen([exe_file,'help'],stdout=subprocess.PIPE)
64                res = p.stdout.read()
65                p.communicate()
66                svnLocCache = os.path.abspath(exe_file)
67                return svnLocCache
68            except:
69                pass       
70    svnLocCache = None
71
72def svnVersion(svn=None):
73    '''Get the version number of the current subversion executable
74
75    :returns: a string with a version number such as "1.6.6" or None if
76      subversion is not found.
77
78    '''
79    if not svn: svn = whichsvn()
80    if not svn: return
81
82    cmd = [svn,'--version','--quiet']
83    s = subprocess.Popen(cmd,
84                         stdout=subprocess.PIPE,stderr=subprocess.PIPE)
85    out,err = s.communicate()
86    if err:
87        print('subversion error!\nout= '+out)
88        print('err= '+err)
89        return None
90    return out.strip()
91
92def svnVersionNumber(svn=None):
93    '''Get the version number of the current subversion executable
94
95    :returns: a fractional version number such as 1.6 or None if
96      subversion is not found.
97
98    '''
99    ver = svnVersion(svn)
100    if not ver: return 
101    M,m = ver.split('.')[:2]
102    return int(M)+int(m)/10.
103
104################################################################################
105################################################################################
106print(70*'*')
107#testing for incorrect locale code'
108try:
109    import locale
110    locale.getdefaultlocale()
111except ValueError:
112    print('Your location is not set properly. This causes problems for matplotlib')
113    print('  (see https://github.com/matplotlib/matplotlib/issues/5420.)')
114    print('Will try to bypass problem by setting LC_ALL to en_US.UTF-8 (US English)')
115    os.environ['LC_ALL'] = 'en_US.UTF-8'
116    locale.getdefaultlocale()
117print('Preloading matplotlib to build fonts...')
118try:
119    import matplotlib
120except:
121    pass
122print('Checking python packages...')
123missing = []
124for pkg in ['numpy','scipy','matplotlib','wx',]:
125    try:
126        exec('import '+pkg)
127    except:
128        missing.append(pkg)
129
130if missing:
131    msg = """Sorry, this version of Python cannot be used
132for GSAS-II. It is missing the following package(s):
133\t"""
134    for pkg in missing: msg += " "+pkg
135    print(msg)
136    print("\nPlease install these package(s) and try running this again.")
137    print("Showing first error: ")
138    for pkg in ['numpy','scipy','matplotlib','wx',]:
139        exec('import '+pkg)
140    sys.exit()
141try:
142    import OpenGL
143    install_with_easyinstall = None 
144except:
145    try:                 
146            from setuptools.command import easy_install                 
147    except ImportError:
148        print('You are missing the OpenGL Python package. This can be ')
149        print('installed by this script if the setuptools package is installed')
150        print('Please install either OpenGL (pyopengl) or setuptools')
151        print("package and try running this again.")
152        sys.exit()
153    print("Missing the OpenGL Python package. Will attempt to install this later.")
154    def install_with_easyinstall(package):               
155        try:             
156            print("trying system-wide ")                 
157            easy_install.main(['-f',os.path.split(__file__)[0],package])                 
158            return               
159        except:                 
160            pass                 
161        try:             
162            print("trying user level ")                 
163            easy_install.main(['-f',os.path.split(__file__)[0],'--user',package])               
164            return               
165        except:                 
166            print("\nInstall of "+package+" failed. Error traceback follows:")
167            import traceback             
168            print(traceback.format_exc())
169            sys.exit() 
170
171print('\nChecking for subversion...')
172svn = whichsvn()
173if not svn:
174    print("Sorry, subversion (svn) could not be found on your system.")
175    print("Please install this or place in path and rerun this.")
176    #raise Exception('Subversion (svn) not found')
177    sys.exit()
178print(' found svn image: '+svn)
179
180if install_with_easyinstall:             
181    print('\nInstalling PyOpenGL. Lots of warnings will follow... ')
182    install_with_easyinstall('PyOpenGl')                 
183    print('done.')
184   
185print('Ready to bootstrap GSAS-II from repository\n\t'+g2home+'\nto '+path2GSAS2)
186proxycmds = []
187proxyinfo = os.path.join(path2GSAS2,"proxyinfo.txt")
188host = None
189port = '80'
190if os.path.exists(proxyinfo):
191    fp = open(proxyinfo,'r')
192    host = fp.readline().strip()
193    port = fp.readline().strip()
194    fp.close()
195    os.remove(proxyinfo)
196print(70*"=")
197if sys.version_info[0] == 2:
198    getinput = raw_input
199else:
200    getinput = input
201try:
202    key = None
203    for i in os.environ.keys():
204        if 'https_proxy' == i.lower():
205            key = i
206            break
207    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 = ""
225    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
230        port = ans
231except EOFError:
232    host = ""
233    port = ""
234if host:
235    proxycmds.append('--config-option')
236    proxycmds.append('servers:global:http-proxy-host='+host.strip())
237    if port:
238        proxycmds.append('--config-option')
239        proxycmds.append('servers:global:http-proxy-port='+port.strip())
240    fp = open(proxyinfo,'w')
241    fp.write(host.strip()+'\n')
242    fp.write(port.strip()+'\n')
243    fp.close()
244
245# patch: switch GSAS-II location if linked to XOR server (removed May/June 2017)
246cmd = [svn, 'info']
247p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
248res,err = p.communicate()
249if '.xor.' in str(res):
250    print('Switching previous install with .xor. download location to\n\thttps://subversion.xray.aps.anl.gov/pyGSAS')
251    cmd = [svn, 'switch','--relocate','https://subversion.xor.aps.anl.gov/pyGSAS',
252           'https://subversion.xray.aps.anl.gov/pyGSAS']
253    if proxycmds: cmd += proxycmds
254    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
255    res,err = p.communicate()
256    if err:
257        print('Please report this error to toby@anl.gov:')
258        print(err)
259        print(res)
260# patch: switch GSAS-II location if switched to 2frame version (removed August 2017)
261if '2frame' in str(res):
262    print('Switching previous 2frame install to trunk\n\thttps://subversion.xray.aps.anl.gov/pyGSAS')
263    cmd = [svn, 'switch',g2home + '/trunk',path2GSAS2,
264           '--non-interactive','--trust-server-cert',
265           '--accept','theirs-conflict','--force','--ignore-ancestry']
266    if proxycmds: cmd += proxycmds
267    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
268    res,err = p.communicate()
269    if err:
270        print('Please report this error to toby@anl.gov:')
271        print(err)
272        print(res)
273
274cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2, '--non-interactive', '--trust-server-cert']
275if proxycmds: cmd += proxycmds
276msg = 'Now preparing to install GSAS-II'
277print('\n'+75*'*')
278print(msg + u' from ' + cmd[2])
279msg = 'svn load command: '
280for item in cmd: msg += " "+item
281print(msg)
282s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
283print('\nsubversion output:')
284out,err = s.communicate()
285if 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()   
292    cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2]
293    if proxycmds: cmd += proxycmds
294    msg = ""
295    for item in cmd: msg += item
296    print(msg)
297    s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
298    out,err = s.communicate()
299    if err:
300        print('subversion returned an error:')
301        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')
304        print('  *** problem, most commonly because you need to use a network proxy. Please')
305        print('  *** check with a network administrator or use http://www.whatismyproxy.com/\n')
306        sys.exit()
307print('\n'+75*'*')
308
309try:
310    import GSASIIpath
311    print('import of GSASIIpath completed')
312except Exception as err:
313    print('\n'+75*'=')
314    print('Failed with import of GSASIIpath. This is unexpected.')
315    print('GSAS-II will not run without correcting this. Contact toby@anl.gov')
316    print(err)
317    print(75*'=')
318    sys.exit()
319
320for a in sys.argv[1:]:
321    if 'allbin' in a.lower() or 'server' in a.lower():
322        print('Loading all binaries with command...')
323        if not GSASIIpath.svnSwitchDir('AllBinaries','',g2home+ 'Binaries/',None,True):
324            print('Binary load failed')
325            sys.exit()
326        break
327else:
328    GSASIIpath.DownloadG2Binaries(g2home)
329       
330#===========================================================================
331# test if the compiled files load correctly
332#===========================================================================
333
334script = """  # commands that test each module can at least be loaded & run something in pyspg
335try:
336    import GSASIIpath
337    GSASIIpath.SetBinaryPath()
338    import pyspg
339    import histogram2d
340    import polymask
341    import pypowder
342    import pytexture
343    pyspg.sgforpy('P -1')
344    print('==OK==')
345except:
346    pass
347"""
348p = subprocess.Popen([sys.executable,'-c',script],stdout=subprocess.PIPE,stderr=subprocess.PIPE,
349                     cwd=path2GSAS2)
350res,err = p.communicate()
351if '==OK==' not in str(res) or p.returncode != 0:
352    print('\n'+75*'=')
353    print('Failed when testing the GSAS-II compiled files. GSAS-II will not run')
354    print('without correcting this.\n\nError message:')
355    #print(res)
356    print(err)
357    #print('\nAttempting to open a web page on compiling GSAS-II...')
358    print('Please see web page\nhttps://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII')
359    #import webbrowser
360    #webbrowser.open_new('https://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII')
361    print(75*'=')
362#    if '86' in platform.machine() and (sys.platform.startswith('linux')
363#                                        or sys.platform == "darwin"
364#                                        or sys.platform.startswith('win')):
365#        print('Platform '+sys.platform+' with processor type '+platform.machine()+' is supported')
366#    else:
367#        print('Platform '+sys.platform+' with processor type '+platform.machine()+' not is supported')
368    sys.exit()
369else:
370    print('Successfully tested compiled routines')
371#===========================================================================
372# import all .py files so that .pyc files get created
373print('Byte-compiling all .py files...')
374import compileall
375compileall.compile_dir(path2GSAS2,quiet=True)
376print('done')
377#===========================================================================
378# platform-dependent stuff
379#===========================================================================
380if sys.version_info[0] > 2:
381    def execfile(file):
382        with open(file) as source_file:
383            exec(source_file.read())
384# on Windows, make a batch file with Python and GSAS-II location hard-coded
385if sys.platform.startswith('win') and os.path.exists(
386    os.path.join(path2GSAS2,"makeBat.py")):
387    execfile(os.path.join(path2GSAS2,"makeBat.py"))
388#===========================================================================
389# on a Mac, make an applescript
390elif sys.platform.startswith('darwin') and os.path.exists(
391    os.path.join(path2GSAS2,"makeMacApp.py")):
392    sys.argv = [os.path.join(path2GSAS2,"makeMacApp.py")]
393    print(u'running '+sys.argv[0])
394    execfile(sys.argv[0])
395#===========================================================================
396# On linux, make desktop icon
397elif sys.platform.startswith('linux') and os.path.exists(
398    os.path.join(path2GSAS2,"makeLinux.py")):
399    sys.argv = [os.path.join(path2GSAS2,"makeLinux.py")]
400    print(u'running '+sys.argv[0])
401    execfile(sys.argv[0])
402
Note: See TracBrowser for help on using the repository browser.