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