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

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

latest builds from jenkins & update to build g2conda

  • Property svn:eol-style set to native
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
197try:
198    if host:
199        ans = getinput("Enter the proxy address (type none to remove) ["+host+"]: ")
200        if ans.strip().lower() == "none": host = ans = ""
201    else:
202        ans = getinput("Enter your proxy address [none needed]: ")
203except EOFError:
204    ans = ""
205if ans.strip() != "" or host:
206    proxycmds.append('--config-option')
207    if ans.strip() == "": ans=host
208    proxycmds.append('servers:global:http-proxy-host='+ans.strip())
209    fp = open(proxyinfo,'w')
210    fp.write(ans.strip()+'\n')
211    ans = getinput("Enter the proxy port ["+port+"]: ")
212    if ans.strip() == "": ans=port
213    proxycmds.append('--config-option')
214    proxycmds.append('servers:global:http-proxy-port='+ans.strip())
215    fp.write(ans.strip()+'\n')
216    fp.close()
217
218# patch: switch GSAS-II location if linked to XOR server (removed May/June 2017)
219cmd = [svn, 'info']
220p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
221res,err = p.communicate()
222if '.xor.' in str(res):
223    print('Switching previous install with .xor. download location to\n\thttps://subversion.xray.aps.anl.gov/pyGSAS')
224    cmd = [svn, 'switch','--relocate','https://subversion.xor.aps.anl.gov/pyGSAS',
225           'https://subversion.xray.aps.anl.gov/pyGSAS']
226    if proxycmds: cmd += proxycmds
227    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
228    res,err = p.communicate()
229    if err:
230        print('Please report this error to toby@anl.gov:')
231        print(err)
232        print(res)
233# patch: switch GSAS-II location if switched to 2frame version (removed August 2017)
234if '2frame' in str(res):
235    print('Switching previous 2frame install to trunk\n\thttps://subversion.xray.aps.anl.gov/pyGSAS')
236    cmd = [svn, 'switch',g2home + '/trunk',path2GSAS2,
237           '--non-interactive','--trust-server-cert',
238           '--accept','theirs-conflict','--force','--ignore-ancestry']
239    if proxycmds: cmd += proxycmds
240    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
241    res,err = p.communicate()
242    if err:
243        print('Please report this error to toby@anl.gov:')
244        print(err)
245        print(res)
246
247cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2, '--non-interactive', '--trust-server-cert']
248if proxycmds: cmd += proxycmds
249msg = 'Now preparing to install GSAS-II'
250print('\n'+75*'*')
251print(msg + u' from ' + cmd[2])
252msg = 'svn load command: '
253for item in cmd: msg += " "+item
254print(msg)
255s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
256print('\nsubversion output:')
257out,err = s.communicate()
258if err:
259    print('subversion returned an error:')
260    print(err)
261    print('Retrying with a cleanup and a command for older svn version...')
262    cmd = [svn, 'cleanup', path2GSAS2]
263    s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
264    out,err = s.communicate()   
265    cmd = [svn, 'co', g2home+ 'trunk/', path2GSAS2]
266    if proxycmds: cmd += proxycmds
267    msg = ""
268    for item in cmd: msg += item
269    print(msg)
270    s = subprocess.Popen(cmd,stderr=subprocess.PIPE)
271    out,err = s.communicate()
272    if err:
273        print('subversion returned an error:')
274        print(err)
275        print('  *** GSAS-II failed to be installed. In case the cleanup worked, try running this again')
276        print('  *** If this installation continues to fail, a likely reason is a network access')
277        print('  *** problem, most commonly because you need to use a network proxy. Please')
278        print('  *** check with a network administrator or use http://www.whatismyproxy.com/\n')
279        sys.exit()
280print('\n'+75*'*')
281
282try:
283    import GSASIIpath
284    print('import of GSASIIpath completed')
285except Exception as err:
286    print('\n'+75*'=')
287    print('Failed with import of GSASIIpath. This is unexpected.')
288    print('GSAS-II will not run without correcting this. Contact toby@anl.gov')
289    print(err)
290    print(75*'=')
291    sys.exit()
292
293for a in sys.argv[1:]:
294    if 'allbin' in a.lower() or 'server' in a.lower():
295        print('Loading all binaries with command...')
296        if not GSASIIpath.svnSwitchDir('AllBinaries','',g2home+ 'Binaries/',None,True):
297            print('Binary load failed')
298            sys.exit()
299        break
300else:
301    GSASIIpath.DownloadG2Binaries(g2home)
302       
303#===========================================================================
304# test if the compiled files load correctly
305#===========================================================================
306
307script = """  # commands that test each module can at least be loaded & run something in pyspg
308try:
309    import GSASIIpath
310    GSASIIpath.SetBinaryPath()
311    import pyspg
312    import histogram2d
313    import polymask
314    import pypowder
315    import pytexture
316    pyspg.sgforpy('P -1')
317    print('==OK==')
318except:
319    pass
320"""
321p = subprocess.Popen([sys.executable,'-c',script],stdout=subprocess.PIPE,stderr=subprocess.PIPE,
322                     cwd=path2GSAS2)
323res,err = p.communicate()
324if '==OK==' not in str(res) or p.returncode != 0:
325    print('\n'+75*'=')
326    print('Failed when testing the GSAS-II compiled files. GSAS-II will not run')
327    print('without correcting this.\n\nError message:')
328    #print(res)
329    print(err)
330    #print('\nAttempting to open a web page on compiling GSAS-II...')
331    print('Please see web page\nhttps://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII')
332    #import webbrowser
333    #webbrowser.open_new('https://subversion.xray.aps.anl.gov/trac/pyGSAS/wiki/CompileGSASII')
334    print(75*'=')
335#    if '86' in platform.machine() and (sys.platform.startswith('linux')
336#                                        or sys.platform == "darwin"
337#                                        or sys.platform.startswith('win')):
338#        print('Platform '+sys.platform+' with processor type '+platform.machine()+' is supported')
339#    else:
340#        print('Platform '+sys.platform+' with processor type '+platform.machine()+' not is supported')
341    sys.exit()
342else:
343    print('Successfully tested compiled routines')
344#===========================================================================
345# import all .py files so that .pyc files get created
346print('Byte-compiling all .py files...')
347import compileall
348compileall.compile_dir(path2GSAS2,quiet=True)
349print('done')
350#===========================================================================
351# platform-dependent stuff
352#===========================================================================
353if sys.version_info[0] > 2:
354    def execfile(file):
355        with open(file) as source_file:
356            exec(source_file.read())
357# on Windows, make a batch file with Python and GSAS-II location hard-coded
358if sys.platform.startswith('win') and os.path.exists(
359    os.path.join(path2GSAS2,"makeBat.py")):
360    execfile(os.path.join(path2GSAS2,"makeBat.py"))
361#===========================================================================
362# on a Mac, make an applescript
363elif sys.platform.startswith('darwin') and os.path.exists(
364    os.path.join(path2GSAS2,"makeMacApp.py")):
365    sys.argv = [os.path.join(path2GSAS2,"makeMacApp.py")]
366    print(u'running '+sys.argv[0])
367    execfile(sys.argv[0])
368#===========================================================================
369# On linux, make desktop icon
370elif sys.platform.startswith('linux') and os.path.exists(
371    os.path.join(path2GSAS2,"makeLinux.py")):
372    sys.argv = [os.path.join(path2GSAS2,"makeLinux.py")]
373    print(u'running '+sys.argv[0])
374    execfile(sys.argv[0])
375
Note: See TracBrowser for help on using the repository browser.