Changeset 1356


Ignore:
Timestamp:
May 20, 2014 3:52:26 PM (9 years ago)
Author:
toby
Message:

revised for Canopy

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/makeMacApp.py

    r1109 r1356  
    2424    sys.exit()
    2525
     26def RunPython(image,cmd):
     27    'Run a command in a python image'
     28    try:
     29        err=None
     30        p = subprocess.Popen([image,'-c',cmd],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
     31        out = p.stdout.read()
     32        err = p.stderr.read()
     33        p.communicate()
     34        return out,err
     35    except Exception(err):
     36        return '','Exception = '+err
     37
    2638project="GSAS-II"
     39AppleScript = ''
     40'''Contains an AppleScript to start GSAS-II launching python and the
     41GSAS-II python script
     42'''
     43
     44AppleScript += '''(*   GSAS-II AppleScript by B. Toby (brian.toby@anl.gov)
     45     It can launch GSAS-II by double clicking or by dropping a data file
     46     or folder over the app.
     47     It runs GSAS-II in a terminal window.
     48*)
     49
     50(* test if a file is present and exit with an error message if it is not  *)
     51on TestFilePresent(appwithpath)
     52        tell application "System Events"
     53                if (file appwithpath exists) then
     54                else
     55                        display dialog "Error: file " & appwithpath & " not found. If you have moved this file recreate the AppleScript with bootstrap.py." with icon caution buttons {{"Quit"}}
     56                        return
     57                end if
     58        end tell
     59end TestFilePresent
     60
     61(*
     62------------------------------------------------------------------------
     63this section responds to a double-click. No file is supplied to GSAS-II
     64------------------------------------------------------------------------
     65*)
     66on run
     67        set python to "{:s}"
     68        set appwithpath to "{:s}"
     69        set env to "{:s}"
     70        TestFilePresent(appwithpath)
     71        TestFilePresent(python)
     72        tell application "Terminal"
     73                activate
     74                do script env & python & " " & appwithpath & "; exit"
     75        end tell
     76end run
     77
     78(*
     79-----------------------------------------------------------------------------------------------
     80this section handles starting with files dragged into the AppleScript
     81 o it goes through the list of file(s) dragged in
     82 o then it converts the colon-delimited macintosh file location to a POSIX filename
     83 o for every non-directory file dragged into the icon, it starts GSAS-II, passing the file name
     84------------------------------------------------------------------------------------------------
     85*)
     86
     87on open names
     88        set python to "{:s}"
     89        set appwithpath to "{:s}"
     90        set env to "{:s}"
     91 
     92        TestFilePresent(appwithpath)
     93        repeat with filename in names
     94                set filestr to (filename as string)
     95                if filestr ends with ":" then
     96                        (* should not happen, skip directories *)
     97                else
     98                        (* if this is an input file, open it *)
     99                        set filename to the quoted form of the POSIX path of filename
     100                        tell application "Terminal"
     101                                activate
     102                                do script env & python & " " & appwithpath & " " & filename & "; exit"
     103                        end tell
     104                end if
     105        end repeat
     106end open
     107'''
    27108
    28109if __name__ == '__main__':
     
    59140            break
    60141        pythonpath,top = os.path.split(pythonpath)
     142        pythonapp = os.path.join(pythonpath,'Resources','Python.app','Contents','MacOS','Python')
     143        if not os.path.exists(pythonapp):
     144            print("\nSorry, failed to find a Python app in "+str(pythonapp))
     145            pythonapp = sys.executable
    61146    else:
    62147        print("\nSorry, failed to find a Resources directory associated with "+str(sys.executable))
    63         sys.exit()
    64     pythonapp = os.path.join(pythonpath,'Resources','Python.app','Contents','MacOS','Python')
    65     if not os.path.exists(pythonapp):
    66         print("\nSorry, failed to find a Python app in "+str(pythonapp))
    67         sys.exit()
    68 
    69     # new name to call python
    70     newpython =  os.path.join(apppath,"Contents","MacOS",project)
    71 
     148        pythonapp = sys.executable
     149
     150    # create a link to the python app, but named to match the project
     151    if os.path.exists('/tmp/testpython'): os.remove('/tmp/testpython')
     152    os.symlink(pythonapp,'/tmp/testpython')
     153    # test if it runs
     154    testout,errout = RunPython('/tmp/testpython','import numpy; import wx; print("OK")')
     155    os.remove('/tmp/testpython')
     156    #print testout,errout
     157    if testout.strip() != "OK":
     158        print 'Run of python app failed, assuming Canopy <=1.4'
     159        pythonapp = sys.executable
     160        # switch to pythonw
     161        if os.path.split(pythonapp)[1].lower() == 'python':
     162            pythonapp = os.path.join(os.path.split(pythonapp)[0],'pythonw')
     163        newpython = pythonapp
     164    else:
     165        # new name to call python
     166        newpython =  os.path.join(apppath,"Contents","MacOS",project)
     167
     168    print sys.executable
     169    print newpython
     170    print pythonapp
     171    #sys.exit()
     172   
    72173    if os.path.exists(apppath): # cleanup
    73174        print("\nRemoving old "+project+" app ("+str(apppath)+")")
    74175        shutil.rmtree(apppath)
    75176
    76     # create an AppleScript that launches python with the requested app
    77     AppleScript = '''(*   GSAS-II AppleScript by B. Toby (brian.toby@anl.gov)
    78      It can launch GSAS-II by double clicking or by dropping a data file
    79      or folder over the app.
    80      It runs GSAS-II in a terminal window.
    81 *)
    82 
    83 (* test if a file is present and exit with an error message if it is not  *)
    84 on TestFilePresent(appwithpath)
    85         tell application "System Events"
    86                 if (file appwithpath exists) then
    87                 else
    88                         display dialog "Error: file " & appwithpath & " not found. If you have moved this file recreate the AppleScript with bootstrap.py." with icon caution buttons {{"Quit"}}
    89                         return
    90                 end if
    91         end tell
    92 end TestFilePresent
    93 
    94 (*
    95 ------------------------------------------------------------------------
    96 this section responds to a double-click. No file is supplied to GSAS-II
    97 ------------------------------------------------------------------------
    98 *)
    99 on run
    100         set python to "{:s}"
    101         set appwithpath to "{:s}"
    102         TestFilePresent(appwithpath)
    103         TestFilePresent(python)
    104         tell application "Terminal"
    105                 activate
    106                 do script python & " " & appwithpath & "; exit"
    107         end tell
    108 end run
    109 
    110 (*
    111 -----------------------------------------------------------------------------------------------
    112 this section handles starting with files dragged into the AppleScript
    113  o it goes through the list of file(s) dragged in
    114  o then it converts the colon-delimited macintosh file location to a POSIX filename
    115  o for every non-directory file dragged into the icon, it starts GSAS-II, passing the file name
    116 ------------------------------------------------------------------------------------------------
    117 *)
    118 
    119 on open names
    120         set python to "{:s}"
    121         set appwithpath to "{:s}"
    122         TestFilePresent(appwithpath)
    123         repeat with filename in names
    124                 set filestr to (filename as string)
    125                 if filestr ends with ":" then
    126                         (* should not happen, skip directories *)
    127                 else
    128                         (* if this is an input file, open it *)
    129                         set filename to the quoted form of the POSIX path of filename
    130                         tell application "Terminal"
    131                                 activate
    132                                 do script python & " " & appwithpath & " " & filename & "; exit"
    133                         end tell
    134                 end if
    135         end repeat
    136 end open
    137 '''
    138177    shell = os.path.join("/tmp/","appscrpt.script")
    139178    f = open(shell, "w")
    140     f.write(AppleScript.format(newpython,script,newpython,script))
     179    f.write(AppleScript.format(newpython,script,'',newpython,script,''))
    141180    f.close()
    142181
     
    150189
    151190    # create a link to the python app, but named to match the project
    152     os.symlink(pythonapp,newpython)
     191    if pythonapp != newpython: os.symlink(pythonapp,newpython)
    153192
    154193    # change the icon
     
    163202        'CFBundleTypeName': 'GSAS-II project',
    164203        'CFBundleTypeRole': 'Editor'}]
     204   
    165205    plistlib.writePlist(d,os.path.join(apppath,"Contents",'Info.plist'))
    166206
Note: See TracChangeset for help on using the changeset viewer.