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