source: install/bootstrap.py @ 3476

Last change on this file since 3476 was 3201, checked in by toby, 6 years ago

update bootstrap for Py3

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