source: install/bootstrap.py @ 2952

Last change on this file since 2952 was 2952, checked in by toby, 4 years ago

GSASII installation: support downloading of all binaries

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