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