Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 1 | """Create an applet 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 and then adding a 'PYC ' |
| 6 | resource named __main__ containing the compiled, marshalled script. |
| 7 | """ |
| 8 | |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 9 | DEBUG=0 |
| 10 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 11 | import sys |
| 12 | sys.stdout = sys.stderr |
| 13 | |
| 14 | import string |
| 15 | import os |
| 16 | import marshal |
| 17 | import imp |
| 18 | import macfs |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 19 | import MACFS |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 20 | import MacOS |
| 21 | from Res import * |
| 22 | |
| 23 | # .pyc file (and 'PYC ' resource magic number) |
| 24 | MAGIC = imp.get_magic() |
| 25 | |
| 26 | # Template file (searched on sys.path) |
| 27 | TEMPLATE = "PythonApplet" |
| 28 | |
| 29 | # Specification of our resource |
| 30 | RESTYPE = 'PYC ' |
| 31 | RESNAME = '__main__' |
| 32 | |
| 33 | # A resource with this name sets the "owner" (creator) of the destination |
| 34 | OWNERNAME = "owner resource" |
| 35 | |
| 36 | # OpenResFile mode parameters |
| 37 | READ = 1 |
| 38 | WRITE = 2 |
| 39 | |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 40 | def findtemplate(): |
| 41 | """Locate the applet template along sys.path""" |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 42 | for p in sys.path: |
| 43 | template = os.path.join(p, TEMPLATE) |
| 44 | try: |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 45 | template, d1, d2 = macfs.ResolveAliasFile(template) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 46 | break |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 47 | except (macfs.error, ValueError): |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 48 | continue |
| 49 | else: |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 50 | die("Template %s not found on sys.path" % `TEMPLATE`) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 51 | return |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 52 | template = template.as_pathname() |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 53 | return template |
| 54 | |
| 55 | def main(): |
| 56 | global DEBUG |
| 57 | DEBUG=1 |
| 58 | |
| 59 | # Find the template |
| 60 | # (there's no point in proceeding if we can't find it) |
| 61 | |
| 62 | template = findtemplate() |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 63 | print 'Using template', template |
| 64 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 65 | # Ask for source text if not specified in sys.argv[1:] |
| 66 | |
| 67 | if not sys.argv[1:]: |
Jack Jansen | 9062fa2 | 1995-08-14 12:21:12 +0000 | [diff] [blame] | 68 | srcfss, ok = macfs.PromptGetFile('Select Python source file:', 'TEXT') |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 69 | if not ok: |
| 70 | return |
| 71 | filename = srcfss.as_pathname() |
| 72 | tp, tf = os.path.split(filename) |
| 73 | if tf[-3:] == '.py': |
| 74 | tf = tf[:-3] |
| 75 | else: |
| 76 | tf = tf + '.applet' |
| 77 | dstfss, ok = macfs.StandardPutFile('Save application as:', tf) |
| 78 | if not ok: return |
| 79 | process(template, filename, dstfss.as_pathname()) |
| 80 | else: |
| 81 | |
| 82 | # Loop over all files to be processed |
| 83 | for filename in sys.argv[1:]: |
| 84 | process(template, filename, '') |
| 85 | |
| 86 | undefs = ('Atmp', '????', ' ', '\0\0\0\0', 'BINA') |
| 87 | |
| 88 | def process(template, filename, output): |
| 89 | |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 90 | if DEBUG: |
| 91 | print "Processing", `filename`, "..." |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 92 | |
| 93 | # Read the source and compile it |
| 94 | # (there's no point overwriting the destination if it has a syntax error) |
| 95 | |
| 96 | fp = open(filename) |
| 97 | text = fp.read() |
| 98 | fp.close() |
| 99 | try: |
| 100 | code = compile(text, filename, "exec") |
| 101 | except (SyntaxError, EOFError): |
| 102 | die("Syntax error in script %s" % `filename`) |
| 103 | return |
| 104 | |
| 105 | # Set the destination file name |
| 106 | |
| 107 | if string.lower(filename[-3:]) == ".py": |
| 108 | destname = filename[:-3] |
| 109 | rsrcname = destname + '.rsrc' |
| 110 | else: |
| 111 | destname = filename + ".applet" |
| 112 | rsrcname = filename + '.rsrc' |
| 113 | |
| 114 | if output: |
| 115 | destname = output |
| 116 | # Copy the data from the template (creating the file as well) |
| 117 | |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 118 | template_fss = macfs.FSSpec(template) |
| 119 | template_fss, d1, d2 = macfs.ResolveAliasFile(template_fss) |
| 120 | dest_fss = macfs.FSSpec(destname) |
| 121 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 122 | tmpl = open(template, "rb") |
| 123 | dest = open(destname, "wb") |
| 124 | data = tmpl.read() |
| 125 | if data: |
| 126 | dest.write(data) |
| 127 | dest.close() |
| 128 | tmpl.close() |
| 129 | |
| 130 | # Copy the creator of the template to the destination |
| 131 | # unless it already got one. Set type to APPL |
| 132 | |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 133 | tctor, ttype = template_fss.GetCreatorType() |
| 134 | ctor, type = dest_fss.GetCreatorType() |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 135 | if type in undefs: type = 'APPL' |
| 136 | if ctor in undefs: ctor = tctor |
| 137 | |
| 138 | # Open the output resource fork |
| 139 | |
| 140 | try: |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 141 | output = FSpOpenResFile(dest_fss, WRITE) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 142 | except MacOS.Error: |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 143 | if DEBUG: |
| 144 | print "Creating resource fork..." |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 145 | CreateResFile(destname) |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 146 | output = FSpOpenResFile(dest_fss, WRITE) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 147 | |
| 148 | # Copy the resources from the template |
| 149 | |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 150 | input = FSpOpenResFile(template_fss, READ) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 151 | newctor = copyres(input, output) |
| 152 | CloseResFile(input) |
| 153 | if newctor: ctor = newctor |
| 154 | |
| 155 | # Copy the resources from the target specific resource template, if any |
| 156 | |
| 157 | try: |
| 158 | input = FSpOpenResFile(rsrcname, READ) |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 159 | except (MacOS.Error, ValueError): |
| 160 | print 'No resource file', rsrcname |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 161 | pass |
| 162 | else: |
| 163 | newctor = copyres(input, output) |
| 164 | CloseResFile(input) |
| 165 | if newctor: ctor = newctor |
| 166 | |
Jack Jansen | 7c86b21 | 1995-08-31 13:48:43 +0000 | [diff] [blame] | 167 | # Now set the creator, type and bundle bit of the destination |
| 168 | dest_finfo = dest_fss.GetFInfo() |
| 169 | dest_finfo.Creator = ctor |
| 170 | dest_finfo.Type = type |
| 171 | dest_finfo.Flags = dest_finfo.Flags | MACFS.kHasBundle |
| 172 | dest_finfo.Flags = dest_finfo.Flags & ~MACFS.kHasBeenInited |
| 173 | dest_fss.SetFInfo(dest_finfo) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 174 | |
| 175 | # Make sure we're manipulating the output resource file now |
| 176 | |
| 177 | UseResFile(output) |
| 178 | |
| 179 | # Delete any existing 'PYC 'resource named __main__ |
| 180 | |
| 181 | try: |
| 182 | res = Get1NamedResource(RESTYPE, RESNAME) |
| 183 | res.RemoveResource() |
| 184 | except Error: |
| 185 | pass |
| 186 | |
| 187 | # Create the raw data for the resource from the code object |
| 188 | |
| 189 | data = marshal.dumps(code) |
| 190 | del code |
| 191 | data = (MAGIC + '\0\0\0\0') + data |
| 192 | |
| 193 | # Create the resource and write it |
| 194 | |
| 195 | id = 0 |
| 196 | while id < 128: |
| 197 | id = Unique1ID(RESTYPE) |
| 198 | res = Resource(data) |
| 199 | res.AddResource(RESTYPE, id, RESNAME) |
| 200 | res.WriteResource() |
| 201 | res.ReleaseResource() |
| 202 | |
| 203 | # Close the output file |
| 204 | |
| 205 | CloseResFile(output) |
| 206 | |
| 207 | # Give positive feedback |
| 208 | |
| 209 | message("Applet %s created." % `destname`) |
| 210 | |
| 211 | |
| 212 | # Copy resources between two resource file descriptors. |
| 213 | # Exception: don't copy a __main__ resource. |
| 214 | # If a resource's name is "owner resource", its type is returned |
| 215 | # (so the caller can use it to set the destination's creator) |
| 216 | |
| 217 | def copyres(input, output): |
| 218 | ctor = None |
| 219 | UseResFile(input) |
| 220 | ntypes = Count1Types() |
| 221 | for itype in range(1, 1+ntypes): |
| 222 | type = Get1IndType(itype) |
| 223 | nresources = Count1Resources(type) |
| 224 | for ires in range(1, 1+nresources): |
| 225 | res = Get1IndResource(type, ires) |
| 226 | id, type, name = res.GetResInfo() |
| 227 | lcname = string.lower(name) |
| 228 | if (type, lcname) == (RESTYPE, RESNAME): |
| 229 | continue # Don't copy __main__ from template |
| 230 | if lcname == OWNERNAME: ctor = type |
| 231 | size = res.size |
| 232 | attrs = res.GetResAttrs() |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 233 | if DEBUG: |
| 234 | print id, type, name, size, hex(attrs) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 235 | res.LoadResource() |
| 236 | res.DetachResource() |
| 237 | UseResFile(output) |
| 238 | try: |
| 239 | res2 = Get1Resource(type, id) |
| 240 | except MacOS.Error: |
| 241 | res2 = None |
| 242 | if res2: |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 243 | if DEBUG: |
| 244 | print "Overwriting..." |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 245 | res2.RemoveResource() |
| 246 | res.AddResource(type, id, name) |
| 247 | res.WriteResource() |
| 248 | attrs = attrs | res.GetResAttrs() |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame^] | 249 | if DEBUG: |
| 250 | print "New attrs =", hex(attrs) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 251 | res.SetResAttrs(attrs) |
| 252 | UseResFile(input) |
| 253 | return ctor |
| 254 | |
| 255 | |
| 256 | # Show a message and exit |
| 257 | |
| 258 | def die(str): |
| 259 | message(str) |
| 260 | sys.exit(1) |
| 261 | |
| 262 | |
| 263 | # Show a message |
| 264 | |
| 265 | def message(str, id = 256): |
| 266 | from Dlg import * |
| 267 | d = GetNewDialog(id, -1) |
| 268 | if not d: |
| 269 | print "Error:", `str` |
| 270 | print "DLOG id =", id, "not found." |
| 271 | return |
| 272 | tp, h, rect = d.GetDialogItem(2) |
| 273 | SetDialogItemText(h, str) |
| 274 | while 1: |
| 275 | n = ModalDialog(None) |
| 276 | if n == 1: break |
| 277 | del d |
| 278 | |
| 279 | |
| 280 | if __name__ == '__main__': |
| 281 | main() |
| 282 | |