source: install/bootstrap.py @ 3025

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

work on installation issues

File size: 14.1 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# patch: switch GSAS-II location if switched to 2frame version (removed August 2017)
228if '2frame' in res:
229    print('Switching previous 2frame install to trunk\n\thttps://subversion.xray.aps.anl.gov/pyGSAS')
230    cmd = [svn, 'switch',g2home + '/trunk',gsaspath,
231           '--non-interactive','--trust-server-cert',
232           '--accept','theirs-conflict','--force','--ignore-ancestry']
233    if proxycmds: cmd += proxycmds
234    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
235    res,err = p.communicate()
236    if err:
237        print('Please report this error to toby@anl.gov:')
238        print(err)
239        print(res)
240
241cmd = [svn, 'co', g2home+ 'trunk/', gsaspath, '--non-interactive', '--trust-server-cert']
242if proxycmds: cmd += proxycmds
243msg = 'Now preparing to install GSAS-II'
244print('\n'+75*'*')
245print(msg + u' from ' + cmd[2])
246print 'svn load command:'
247for item in cmd: print item,
248print ""
249s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
250print('\nsubversion output:')
251out,err = s.communicate()
252if err:
253    print('subversion returned an error:')
254    print(err)
255    print('Retrying with command for older svn version...')
256    cmd = [svn, 'co', g2home+ 'trunk/', gsaspath]
257    if proxycmds: cmd += proxycmds
258    for item in cmd: print item,
259    print ""
260    s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
261    out,err = s.communicate()
262    if err:
263        print('subversion returned an error:')
264        print(err)
265        print('  *** GSAS-II failed to be installed: you likely have a network access')
266        print('  *** problem, most commonly because you need to use a network proxy. Please')
267        print('  *** check with a network administrator or use http://www.whatismyproxy.com/\n')
268        sys.exit()
269print('\n'+75*'*')
270
271try:
272    import GSASIIpath
273    print('import of GSASIIpath completed')
274except Exception as err:
275    print('\n'+75*'=')
276    print('Failed with import of GSASIIpath. This is unexpected.')
277    print('GSAS-II will not run without correcting this. Contact toby@anl.gov')
278    print(err)
279    print(75*'=')
280    sys.exit()
281
282for a in sys.argv[1:]:
283    if 'allbin' in a.lower() or 'server' in a.lower():
284        print('Loading all binaries with command...')
285        if not GSASIIpath.svnSwitchDir('AllBinaries','',g2home+ 'Binaries/',None,True):
286            print('Binary load failed')
287            sys.exit()
288        break
289else:
290    GSASIIpath.DownloadG2Binaries(g2home)
291       
292#===========================================================================
293# test if the compiled files load correctly
294#===========================================================================
295
296script = """  # commands that test each module can at least be loaded & run something in pyspg
297try:
298    import GSASIIpath
299    GSASIIpath.SetBinaryPath()
300    import pyspg
301    import histogram2d
302    import polymask
303    import pypowder
304    import pytexture
305    pyspg.sgforpy('P -1')
306    print('==OK==')
307except:
308    pass
309"""
310p = subprocess.Popen([sys.executable,'-c',script],stdout=subprocess.PIPE,stderr=subprocess.PIPE,
311                     cwd=gsaspath)
312res,err = p.communicate()
313if '==OK==' not in res or p.returncode != 0:
314    print('\n'+75*'=')
315    print('Failed when testing the GSAS-II compiled files. GSAS-II will not run')
316    print('without correcting this.')
317    #print(res)
318    #print(err)
319    #print('\nAttempting to open a web page on compiling GSAS-II...')
320    print('Please see web page\nhttps://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII')
321    #import webbrowser
322    #webbrowser.open_new('https://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII')
323    print(75*'=')
324    if '86' in platform.machine() and (sys.platform.startswith('linux')
325                                        or sys.platform == "darwin"
326                                        or sys.platform.startswith('win')):
327        print('Platform '+sys.platform+' with processor type '+platform.machine()+' is supported')
328    else:
329        print('Platform '+sys.platform+' with processor type '+platform.machine()+' not is supported')
330    sys.exit()
331else:
332    print 'Successfully tested compiled routines'
333#===========================================================================
334# import all .py files so that .pyc files get created
335print 'Byte-compiling all .py files...',
336import compileall
337compileall.compile_dir(gsaspath,quiet=True)
338print 'done'
339#===========================================================================
340# platform-dependent stuff
341#===========================================================================
342# on Windows, make a batch file with Python and GSAS-II location hard-coded
343if sys.platform.startswith('win') and os.path.exists(
344    os.path.join(gsaspath,"makeBat.py")):
345    execfile(os.path.join(gsaspath,"makeBat.py"))
346#===========================================================================
347# on a Mac, make an applescript
348elif sys.platform.startswith('darwin') and os.path.exists(
349    os.path.join(gsaspath,"makeMacApp.py")):
350    sys.argv = [os.path.join(gsaspath,"makeMacApp.py")]
351    print(u'running '+sys.argv[0])
352    execfile(sys.argv[0])
353#===========================================================================
354# On linux, make desktop icon
355elif sys.platform.startswith('linux'):
356    desktop_template = """
357[Desktop Entry]
358Encoding=UTF-8
359Version=1.0
360Type=Application
361Terminal=false
362Exec=xterm -hold -e %s
363Name=GSAS-II
364Icon=%s
365"""
366    loc = '~/Desktop/'
367    eloc = os.path.expanduser(loc)
368    dfile = os.path.join(eloc,'GSASII.desktop')
369    icon = os.path.join(gsaspath, 'gsas2.png')
370    script = os.path.join(gsaspath, 'GSASII.py')
371    os.chmod(script, # make the .py file executable and readable
372             stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH)
373    if os.path.exists(eloc):
374        open(dfile,'w').write(desktop_template % (script,icon))
375        os.chmod(
376            dfile,
377            stat.S_IWUSR | stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH)
378        print("created GNOME desktop shortcut "+dfile) 
379
Note: See TracBrowser for help on using the repository browser.