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 |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 17 | from Carbon import Res |
| 18 | from Carbon import Dlg |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 19 | import EasyDialogs |
| 20 | import buildtools |
Jack Jansen | 3c06b9a | 2001-08-27 21:41:23 +0000 | [diff] [blame^] | 21 | import macresource |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 22 | |
| 23 | # Hmmm... |
| 24 | MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze") |
| 25 | if MACFREEZEPATH not in sys.path: |
| 26 | sys.path.append(MACFREEZEPATH) |
| 27 | |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 28 | import macgen_bin |
| 29 | |
| 30 | # dialog, items |
| 31 | DLG_ID = 400 |
| 32 | OK_BUTTON = 1 |
| 33 | CANCEL_BUTTON = 2 |
| 34 | GENFAT_BUTTON = 4 |
| 35 | GENPPC_BUTTON = 5 |
| 36 | GEN68K_BUTTON = 6 |
| 37 | |
Jack Jansen | 919d326 | 2000-07-24 19:44:17 +0000 | [diff] [blame] | 38 | # Define this if we cannot generate 68/fat binaries (Python 1.6) |
| 39 | PPC_ONLY=1 |
| 40 | |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 41 | |
Jack Jansen | 3c06b9a | 2001-08-27 21:41:23 +0000 | [diff] [blame^] | 42 | macresource.need('DITL', DLG_ID, "BuildApplication.rsrc") |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 43 | |
| 44 | def main(): |
| 45 | try: |
| 46 | buildapplication() |
| 47 | except buildtools.BuildError, detail: |
| 48 | EasyDialogs.Message(detail) |
| 49 | |
| 50 | |
| 51 | def buildapplication(debug = 0): |
| 52 | buildtools.DEBUG = debug |
| 53 | |
| 54 | # Ask for source text if not specified in sys.argv[1:] |
| 55 | |
| 56 | if not sys.argv[1:]: |
| 57 | srcfss, ok = macfs.PromptGetFile('Select Python source:', 'TEXT') |
| 58 | if not ok: |
| 59 | return |
| 60 | filename = srcfss.as_pathname() |
| 61 | else: |
| 62 | if sys.argv[2:]: |
| 63 | raise buildtools.BuildError, "please select one file at a time" |
| 64 | filename = sys.argv[1] |
| 65 | tp, tf = os.path.split(filename) |
| 66 | |
| 67 | # interact with user |
| 68 | architecture, ok = interact(tf) |
| 69 | if not ok: |
| 70 | return |
| 71 | if tf[-3:] == '.py': |
| 72 | tf = tf[:-3] |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 73 | else: |
| 74 | tf = tf + '.app' |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 75 | |
| 76 | dstfss, ok = macfs.StandardPutFile('Save application as:', tf) |
| 77 | if not ok: |
| 78 | return |
| 79 | dstfilename = dstfss.as_pathname() |
| 80 | |
| 81 | macgen_bin.generate(filename, dstfilename, None, architecture, 1) |
| 82 | |
| 83 | |
| 84 | class radio: |
| 85 | |
| 86 | def __init__(self, dlg, *items): |
| 87 | self.items = {} |
| 88 | for item in items: |
Jack Jansen | d2bf68f | 1999-12-23 14:33:20 +0000 | [diff] [blame] | 89 | ctl = dlg.GetDialogItemAsControl(item) |
| 90 | self.items[item] = ctl |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 91 | |
| 92 | def set(self, setitem): |
| 93 | for item, ctl in self.items.items(): |
| 94 | if item == setitem: |
| 95 | ctl.SetControlValue(1) |
| 96 | else: |
| 97 | ctl.SetControlValue(0) |
| 98 | |
| 99 | def get(self): |
| 100 | for item, ctl in self.items.items(): |
| 101 | if ctl.GetControlValue(): |
| 102 | return item |
| 103 | |
| 104 | def hasitem(self, item): |
| 105 | return self.items.has_key(item) |
| 106 | |
| 107 | |
| 108 | def interact(scriptname): |
Jack Jansen | 919d326 | 2000-07-24 19:44:17 +0000 | [diff] [blame] | 109 | if PPC_ONLY: |
Jack Jansen | f0d7508 | 2000-12-14 23:34:15 +0000 | [diff] [blame] | 110 | return 'pwpc', 1 |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 111 | d = Dlg.GetNewDialog(DLG_ID, -1) |
| 112 | if not d: |
Jack Jansen | f0d7508 | 2000-12-14 23:34:15 +0000 | [diff] [blame] | 113 | raise "Can't get DLOG resource with id =", DLG_ID |
Jack Jansen | b44f1cc | 1998-07-31 09:44:58 +0000 | [diff] [blame] | 114 | d.SetDialogDefaultItem(OK_BUTTON) |
| 115 | d.SetDialogCancelItem(CANCEL_BUTTON) |
| 116 | Dlg.ParamText(scriptname, "", "", "") |
| 117 | |
| 118 | radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON) |
| 119 | radiogroup.set(GENFAT_BUTTON) |
| 120 | |
| 121 | gentype = 'fat' |
| 122 | while 1: |
| 123 | n = Dlg.ModalDialog(None) |
| 124 | if n == OK_BUTTON or n == CANCEL_BUTTON: |
| 125 | break |
| 126 | elif radiogroup.hasitem(n): |
| 127 | radiogroup.set(n) |
| 128 | genitem = radiogroup.get() |
| 129 | del radiogroup |
| 130 | del d |
| 131 | if genitem == GENFAT_BUTTON: |
| 132 | gentype = 'fat' |
| 133 | elif genitem == GENPPC_BUTTON: |
| 134 | gentype = 'pwpc' |
| 135 | elif genitem == GEN68K_BUTTON: |
| 136 | gentype = 'm68k' |
| 137 | return gentype, n == OK_BUTTON |
| 138 | |
| 139 | |
| 140 | if __name__ == '__main__': |
| 141 | main() |