source: install/bootstrap.py @ 2887

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

setup & use bindist location for downloaded binaries

File size: 12.7 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('  *** If GSAS-II fails to be installed, you likely have a network access')
234print('  *** problem, most commonly because you need to use a network proxy. Please')
235print('  *** check with a network administrator or use http://www.whatismyproxy.com/\n')
236print 'svn load command:'
237for item in cmd: print item,
238print ""
239p = subprocess.call(cmd)
240if p:
241    print 'subversion returned an error; Retrying with command for older version...'
242    cmd = [svn, 'co', g2home+ 'trunk/', gsaspath]
243    if proxycmds: cmd += proxycmds
244    for item in cmd: print item,
245    print ""
246    p = subprocess.call(cmd)
247print('\n'+75*'*')
248
249#===========================================================================
250# test if the compiled files load correctly
251#===========================================================================
252try:
253    import GSASIIpath
254    print('import of GSASIIpath completed')
255except Exception as err:
256    print('\n'+75*'=')
257    print('Failed with import of GSASIIpath. This is unexpected.')
258    print('GSAS-II will not run without correcting this. Contact toby@anl.gov')
259    print(err)
260    print(75*'=')
261    sys.exit()
262
263script = """  # commands that test each module can at least be loaded & run something in pyspg
264try:
265    import GSASIIpath
266    import pyspg
267    import histogram2d
268    import polymask
269    import pypowder
270    import pytexture
271    pyspg.sgforpy('P -1')
272    print('==OK==')
273except:
274    pass
275"""
276p = subprocess.Popen([sys.executable,'-c',script],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
277res,err = p.communicate()
278if '==OK==' not in res or p.returncode != 0:
279    print('\n'+75*'=')
280    print('Failed when testing the GSAS-II compiled files. GSAS-II will not run without')
281    print('correcting this.')
282    #print(res)
283    #print(err)
284    if '86' in platform.machine() and (sys.platform.startswith('linux')
285                                        or sys.platform == "darwin"
286                                        or sys.platform.startswith('win')):
287        print('Platform '+sys.platform+' with processor type '+platform.machine()+' is supported')
288    else:
289        print('Platform '+sys.platform+' with processor type '+platform.machine()+' not is supported')
290    print('\nAttempting to open a web page on compiling GSAS-II...')
291    print('(https://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII)')
292    import webbrowser
293    webbrowser.open_new('https://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII')
294    print(75*'=')
295    sys.exit()
296else:
297    print 'Successfully tested compiled routines'
298#===========================================================================
299# import all .py files so that .pyc files get created
300print 'Byte-compiling all .py files...',
301import compileall
302compileall.compile_dir(gsaspath,quiet=True)
303print 'done'
304#===========================================================================
305# platform-dependent stuff
306#===========================================================================
307# on Windows, make a batch file with Python and GSAS-II location hard-coded
308if sys.platform.startswith('win') and os.path.exists(
309    os.path.join(gsaspath,"makeBat.py")):
310    execfile(os.path.join(gsaspath,"makeBat.py"))
311#===========================================================================
312# on a Mac, make an applescript
313elif sys.platform.startswith('darwin') and os.path.exists(
314    os.path.join(gsaspath,"makeMacApp.py")):
315    print('running '+os.path.join(gsaspath,"makeMacApp.py"))
316    execfile(os.path.join(gsaspath,"makeMacApp.py"))
317#===========================================================================
318# On linux, make desktop icon
319elif sys.platform.startswith('linux'):
320    desktop_template = """
321[Desktop Entry]
322Encoding=UTF-8
323Version=1.0
324Type=Application
325Terminal=false
326Exec=xterm -hold -e %s
327Name=GSAS-II
328Icon=%s
329"""
330    loc = '~/Desktop/'
331    eloc = os.path.expanduser(loc)
332    dfile = os.path.join(eloc,'GSASII.desktop')
333    icon = os.path.join(gsaspath, 'gsas2.png')
334    script = os.path.join(gsaspath, 'GSASII.py')
335    os.chmod(script, # make the .py file executable and readable
336             stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH)
337    if os.path.exists(eloc):
338        open(dfile,'w').write(desktop_template % (script,icon))
339        os.chmod(
340            dfile,
341            stat.S_IWUSR | stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH)
342        print("created GNOME desktop shortcut "+dfile) 
343
Note: See TracBrowser for help on using the repository browser.