source: install/bootstrap.py @ 671

Last change on this file since 671 was 671, checked in by toby, 11 years ago

Revise GSAS-II Mac app to run in terminal (fix)

File size: 7.0 KB
Line 
1#!/usr/bin/env python
2import os, stat, sys, platform, subprocess
3home = 'https://subversion.xor.aps.anl.gov/pyGSAS/'
4print 70*'*'
5print 'Checking python packages...',
6missing = []
7for pkg in ['numpy','scipy','matplotlib','wx']:
8    try:
9        exec('import '+pkg)
10    except:
11        missing.append(pkg)
12
13if missing:
14    print """Sorry, this version of Python cannot be used
15for GSAS-II. It is missing the following package(s):
16\t""",
17    for pkg in missing: print " ",pkg,
18    print 
19    print "We suggest installing the EPDfree package at\nhttp://www.enthought.com/products/epd_free.php/"
20    sys.exit()
21
22gsaspath = os.path.split(sys.argv[0])[0]
23gsaspath = os.path.abspath(os.path.expanduser(gsaspath))
24
25print 'Checking for subversion...',
26try: 
27    #p = subprocess.call(['svn','status'],stdout=subprocess.PIPE)
28    p = subprocess.Popen(['svn','help'],stdout=subprocess.PIPE)
29except:
30    raise Exception('Subversion (svn) not found')
31res = p.stdout.read()
32p.communicate()
33print 'OK'
34
35print 'GSAS-II is being bootstrapped from repository\n\t',home,'\nto '+gsaspath
36
37print 'Determining system type...',
38if sys.platform.startswith('linux'):
39    #if platform.processor().find('86') <= -1:
40    #    ans = raw_input("Note, GSAS requires an Intel-compatible processor and 32-bit"
41    #                    "libraries.\nAre you sure want to continue? [Yes]/no: ")
42    #    if ans.lower().find('no') > -1: sys.exit()
43    repos = 'linux'
44    print 'Linux, Intel-compatible'
45elif sys.platform == "darwin" and platform.processor() == 'i386':
46    repos = 'osxi86'
47    print 'Mac OS X, Intel-compatible'
48elif sys.platform == "darwin":
49    repos = 'osxppc'
50    print 'Mac OS X, PowerPC -- you will need to run f2py on fsource files'
51else:
52    print 'Unidentifed platform -- you may need to run f2py on fsource files'
53
54cmds = (
55    (['svn', 'co', home+ 'trunk/', gsaspath],'loading GSAS-II'),
56    #(['svn', 'co', home+ 'gsas/all', gsaspath],'loading GSAS common'),
57)
58
59for cmd,msg in cmds: 
60    print 70*'*'
61    print msg + ' from ' + cmd[2]
62    for item in cmd: print item,
63    print ""
64    p = subprocess.call(cmd)
65
66#===========================================================================
67# could try to load .so files here and run scons if needed.
68
69#===========================================================================
70# on a Mac, make an applescript
71if sys.platform.startswith('darwin'):
72    script = '''
73(*   GSAS-II AppleScript by B. Toby (brian.toby@anl.gov)
74         It can launch GSAS-II by double clicking or by dropping a data file
75         or folder over the app.
76         It runs GSAS-II in a terminal window.
77         
78         Thanks to F. Farges (farges@univ-mlv.fr) for many applescript tricks
79*)
80
81
82(*
83--------------------------------------------
84Define subroutines used in later sections of code
85--------------------------------------------
86*)
87
88(*  get  directory  from a file name *)
89on GetParentPath(theFile)
90        tell application "Finder" to return container of theFile as text
91end GetParentPath
92
93(* test if a file is present and exit with an error message if it is not  *)
94on TestFilePresent(appwithpath)
95        tell application "System Events"
96                if (file appwithpath exists) then
97                else
98                        display dialog "Error: file " & appwithpath & " not found. This AppleScript must be run from a directory containing the GSAS-II python code. Did you move it?" with icon caution buttons {"Quit"}
99                        return
100                end if
101        end tell
102end TestFilePresent
103
104(*
105--------------------------------------------------------------
106this section responds to a double-click. No file is supplied to GSAS-II
107 o  First find the location of the script
108 o  then convert the colon-delimited macintosh file location to a POSIX filename
109 o  start the app with no file as argument -- opens last file or none (depending on GSAS-II preferences)
110--------------------------------------------------------------
111*)
112on run
113        set python to "%s"
114        set myPath to (path to me)
115        set ParentPath to GetParentPath(myPath)
116        set appwithpath to ParentPath & "GSASII.py"
117        TestFilePresent(appwithpath)
118        set posixapp to quoted form of the POSIX path of appwithpath
119        tell application "Terminal"
120                activate
121                do script python & " " & posixapp & "; exit"
122        end tell
123end run
124
125(*
126----------------------------------------------------------------------
127this section handles opening files dragged into the AppleScript
128 o it finds the location of the current script and finds the GSAS-II
129     executable relative to the applescript
130 o it goes through the list of files dragged in
131 o then it converts the colon-delimited macintosh file location to a POSIX filename
132 o for every non-directory file dragged into the icon, it tries to pass that file  to GSAS-II
133 o for directories select a file from the directory
134-----------------------------------------------------------------------
135*)
136
137on open names
138       
139        set python to "/Library/Frameworks/Python.framework/Versions/7.1/Resources/Python.app/Contents/MacOS/Python"
140        set myPath to (path to me)
141        set ParentPath to GetParentPath(myPath)
142        set appwithpath to ParentPath & "GSASII.py"
143        TestFilePresent(appwithpath)
144        set posixapp to the quoted form of the POSIX path of appwithpath
145        set filePath to ""
146        repeat with filename in names
147                set filestr to (filename as string)
148                if filestr ends with ":" then
149                        set myfile to choose file default location filename with prompt "Select a GSAS-II input file" multiple selections allowed no
150                        set myfile to the quoted form of the POSIX path of myfile
151                        tell application "Terminal"
152                                activate
153                                do script python & " " & posixapp & " " & myfile & "; exit"
154                        end tell
155                else
156                        (* if this is an input file, open it *)
157                        set filename to the quoted form of the POSIX path of filename
158                        tell application "Terminal"
159                                activate
160                                do script python & " " & posixapp & " " & filename & "; exit"
161                        end tell
162                end if
163        end repeat
164end open
165'''
166    app = os.path.join(gsaspath, 'GSASII.app')
167    p = subprocess.Popen(['/usr/bin/osacompile','-o',app],stdin=subprocess.PIPE)
168    p.stdin.write(script % (sys.executable,sys.executable) )
169    p.stdin.close()
170    p.communicate()
171    if os.path.exists('GSAS2.rsrc'):
172        print "Note: ignore the previous -d warning from osacompile"
173        p = subprocess.call(['/Developer/Tools/Rez','-append','GSAS2.rsrc','-o',app + "/Icon\r"])
174        p = subprocess.call(['/Developer/Tools/SetFile','-a','C',app])
175
176#===========================================================================
177# On linux, make desktop icon
178if sys.platform.startswith('linux'):
179    os.chmod(os.path.join(gsaspath, 'GSASII.py'),
180             stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH) 
181    if os.path.exists(os.path.expanduser('~/Desktop/')):
182        open(os.path.expanduser('~/Desktop/GSASII.desktop'),'w').write('''
183[Desktop Entry]
184Encoding=UTF-8
185Version=1.0
186Type=Application
187Terminal=false
188Exec=%s
189Name=GSAS-II
190Icon=%s
191''' % (os.path.join(gsaspath, 'GSASII.py'),os.path.join(gsaspath, 'gsas2.png')))
192        os.chmod(os.path.expanduser('~/Desktop/GSASII.desktop'),
193                 stat.S_IWUSR | stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH) 
Note: See TracBrowser for help on using the repository browser.