Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 1 | """Create a standalone application from a Python script. |
| 2 | |
| 3 | This puts up a dialog asking for a Python source file ('TEXT'). |
| 4 | The output is a file with the same name but its ".py" suffix dropped. |
| 5 | It is created by copying an applet template, all used shared libs and |
| 6 | then adding 'PYC ' resources containing compiled versions of all used |
Just van Rossum | 29e5184 | 1999-11-04 10:30:13 +0000 | [diff] [blame] | 7 | modules written in Python and the main script itself, as __main__. |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 8 | """ |
| 9 | |
| 10 | |
| 11 | import sys |
| 12 | |
| 13 | import string |
| 14 | import os |
| 15 | import macfs |
| 16 | import MacOS |
| 17 | import Res |
| 18 | import Dlg |
| 19 | import EasyDialogs |
| 20 | import buildtools |
| 21 | |
| 22 | # Hmmm... |
| 23 | MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze") |
| 24 | if MACFREEZEPATH not in sys.path: |
| 25 | sys.path.append(MACFREEZEPATH) |
| 26 | |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 27 | import macgen_bin |
| 28 | |
| 29 | # dialog, items |
| 30 | DLG_ID = 400 |
| 31 | OK_BUTTON = 1 |
| 32 | CANCEL_BUTTON = 2 |
| 33 | GENFAT_BUTTON = 4 |
| 34 | GENPPC_BUTTON = 5 |
| 35 | GEN68K_BUTTON = 6 |
| 36 | |
Jack Jansen | 919d326 | 2000-07-24 19:44:17 +0000 | [diff] [blame] | 37 | # Define this if we cannot generate 68/fat binaries (Python 1.6) |
| 38 | PPC_ONLY=1 |
| 39 | |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 40 | |
| 41 | try: |
| 42 | Res.GetResource('DITL', DLG_ID) |
| 43 | except Res.Error: |
Jack Jansen | d13c385 | 2000-06-20 21:59:25 +0000 | [diff] [blame] | 44 | Res.FSpOpenResFile("BuildApplication.rsrc", 1) |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 45 | else: |
| 46 | pass # we're an applet |
| 47 | |
| 48 | |
| 49 | def main(): |
| 50 | try: |
| 51 | buildapplication() |
| 52 | except buildtools.BuildError, detail: |
| 53 | EasyDialogs.Message(detail) |
| 54 | |
| 55 | |
| 56 | def buildapplication(debug = 0): |
| 57 | buildtools.DEBUG = debug |
| 58 | |
| 59 | # Ask for source text if not specified in sys.argv[1:] |
| 60 | |
| 61 | if not sys.argv[1:]: |
| 62 | srcfss, ok = macfs.PromptGetFile('Select Python source:', 'TEXT') |
| 63 | if not ok: |
| 64 | return |
| 65 | filename = srcfss.as_pathname() |
| 66 | else: |
| 67 | if sys.argv[2:]: |
| 68 | raise buildtools.BuildError, "please select one file at a time" |
| 69 | filename = sys.argv[1] |
| 70 | tp, tf = os.path.split(filename) |
| 71 | |
| 72 | # interact with user |
| 73 | architecture, ok = interact(tf) |
| 74 | if not ok: |
| 75 | return |
| 76 | if tf[-3:] == '.py': |
| 77 | tf = tf[:-3] |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 78 | else: |
| 79 | tf = tf + '.app' |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 80 | |
| 81 | dstfss, ok = macfs.StandardPutFile('Save application as:', tf) |
| 82 | if not ok: |
| 83 | return |
| 84 | dstfilename = dstfss.as_pathname() |
| 85 | |
| 86 | macgen_bin.generate(filename, dstfilename, None, architecture, 1) |
| 87 | |
| 88 | |
| 89 | class radio: |
| 90 | |
| 91 | def __init__(self, dlg, *items): |
| 92 | self.items = {} |
| 93 | for item in items: |
Jack Jansen | d2bf68f | 1999-12-23 14:33:20 +0000 | [diff] [blame] | 94 | ctl = dlg.GetDialogItemAsControl(item) |
| 95 | self.items[item] = ctl |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 96 | |
| 97 | def set(self, setitem): |
| 98 | for item, ctl in self.items.items(): |
| 99 | if item == setitem: |
| 100 | ctl.SetControlValue(1) |
| 101 | else: |
| 102 | ctl.SetControlValue(0) |
| 103 | |
| 104 | def get(self): |
| 105 | for item, ctl in self.items.items(): |
| 106 | if ctl.GetControlValue(): |
| 107 | return item |
| 108 | |
| 109 | def hasitem(self, item): |
| 110 | return self.items.has_key(item) |
| 111 | |
| 112 | |
| 113 | def interact(scriptname): |
Jack Jansen | 919d326 | 2000-07-24 19:44:17 +0000 | [diff] [blame] | 114 | if PPC_ONLY: |
Jack Jansen | f0d7508 | 2000-12-14 23:34:15 +0000 | [diff] [blame] | 115 | return 'pwpc', 1 |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 116 | d = Dlg.GetNewDialog(DLG_ID, -1) |
| 117 | if not d: |
Jack Jansen | f0d7508 | 2000-12-14 23:34:15 +0000 | [diff] [blame] | 118 | raise "Can't get DLOG resource with id =", DLG_ID |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 119 | d.SetDialogDefaultItem(OK_BUTTON) |
| 120 | d.SetDialogCancelItem(CANCEL_BUTTON) |
| 121 | Dlg.ParamText(scriptname, "", "", "") |
| 122 | |
| 123 | radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON) |
| 124 | radiogroup.set(GENFAT_BUTTON) |
| 125 | |
| 126 | gentype = 'fat' |
| 127 | while 1: |
| 128 | n = Dlg.ModalDialog(None) |
| 129 | if n == OK_BUTTON or n == CANCEL_BUTTON: |
| 130 | break |
| 131 | elif radiogroup.hasitem(n): |
| 132 | radiogroup.set(n) |
| 133 | genitem = radiogroup.get() |
| 134 | del radiogroup |
| 135 | del d |
| 136 | if genitem == GENFAT_BUTTON: |
| 137 | gentype = 'fat' |
| 138 | elif genitem == GENPPC_BUTTON: |
| 139 | gentype = 'pwpc' |
| 140 | elif genitem == GEN68K_BUTTON: |
| 141 | gentype = 'm68k' |
| 142 | return gentype, n == OK_BUTTON |
| 143 | |
| 144 | |
| 145 | if __name__ == '__main__': |
| 146 | main() |