Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 1 | """Create an applet from a Python script. |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 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. |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 9 | import sys |
| 10 | import string |
| 11 | import os |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 12 | import marshal |
| 13 | import imp |
| 14 | import macfs |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 15 | import MacOS |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 16 | from Res import * |
| 17 | |
| 18 | # .pyc file (and 'PYC ' resource magic number) |
| 19 | MAGIC = imp.get_magic() |
| 20 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 21 | # Template file (searched on sys.path) |
| 22 | TEMPLATE = "PythonApplet" |
| 23 | |
| 24 | # Specification of our resource |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 25 | RESTYPE = 'PYC ' |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 26 | RESNAME = '__main__' |
| 27 | |
| 28 | # OpenResFile mode parameters |
| 29 | READ = 1 |
| 30 | WRITE = 2 |
| 31 | |
| 32 | def main(): |
| 33 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 34 | # Find the template |
| 35 | # (there's no point in proceeding if we can't find it) |
| 36 | |
| 37 | for p in sys.path: |
| 38 | template = os.path.join(p, TEMPLATE) |
| 39 | try: |
| 40 | tmpl = open(template, "rb") |
| 41 | break |
| 42 | except IOError: |
| 43 | continue |
| 44 | else: |
| 45 | # XXX Ought to put up a dialog |
| 46 | print "Template not found. Exit." |
| 47 | return |
| 48 | |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 49 | # Ask for source text |
| 50 | |
| 51 | srcfss, ok = macfs.StandardGetFile('TEXT') |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 52 | if not ok: |
| 53 | tmpl.close() |
| 54 | return |
| 55 | filename = srcfss.as_pathname() |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 56 | |
| 57 | # Read the source and compile it |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 58 | # (there's no point overwriting the destination if it has a syntax error) |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 60 | fp = open(filename) |
| 61 | text = fp.read() |
| 62 | fp.close() |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 63 | try: |
| 64 | code = compile(text, filename, "exec") |
| 65 | except (SyntaxError, EOFError): |
| 66 | print "Syntax error in script" |
| 67 | tmpl.close() |
| 68 | return |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 70 | # Set the destination file name |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 72 | if string.lower(filename[-3:]) == ".py": |
| 73 | destname = filename[:-3] |
| 74 | else: |
| 75 | destname = filename + ".applet" |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 77 | # Copy the data from the template (creating the file as well) |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 79 | dest = open(destname, "wb") |
| 80 | data = tmpl.read() |
| 81 | if data: |
| 82 | dest.write(data) |
| 83 | dest.close() |
| 84 | tmpl.close() |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 86 | # Copy the creator and type of the template to the destination |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 88 | ctor, type = MacOS.GetCreatorAndType(template) |
| 89 | MacOS.SetCreatorAndType(destname, ctor, type) |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 91 | # Open the input and output resource forks |
| 92 | |
| 93 | input = FSpOpenResFile(template, READ) |
| 94 | try: |
| 95 | output = FSpOpenResFile(destname, WRITE) |
| 96 | except MacOS.Error: |
| 97 | print "Creating resource fork..." |
| 98 | CreateResFile(destname) |
| 99 | output = FSpOpenResFile(destname, WRITE) |
| 100 | |
| 101 | # Copy the resources from the template, |
| 102 | # except a 'PYC ' resource named __main__ |
| 103 | |
| 104 | UseResFile(input) |
| 105 | ntypes = Count1Types() |
| 106 | for itype in range(1, 1+ntypes): |
| 107 | type = Get1IndType(itype) |
| 108 | nresources = Count1Resources(type) |
| 109 | for ires in range(1, 1+nresources): |
| 110 | res = Get1IndResource(type, ires) |
| 111 | id, type, name = res.GetResInfo() |
| 112 | if (type, name) == (RESTYPE, RESNAME): |
| 113 | continue # Don't copy __main__ from template |
| 114 | size = res.SizeResource() |
| 115 | attrs = res.GetResAttrs() |
| 116 | print id, type, name, size, hex(attrs) |
| 117 | res.LoadResource() |
| 118 | res.DetachResource() |
| 119 | UseResFile(output) |
| 120 | try: |
| 121 | res2 = Get1Resource(type, id) |
| 122 | except MacOS.Error: |
| 123 | res2 = None |
| 124 | if res2: |
| 125 | print "Overwriting..." |
| 126 | res2.RmveResource() |
| 127 | res.AddResource(type, id, name) |
| 128 | #res.SetResAttrs(attrs) |
| 129 | res.WriteResource() |
| 130 | UseResFile(input) |
| 131 | CloseResFile(input) |
| 132 | |
| 133 | # Make sure we're manipulating the output resource file now |
| 134 | |
| 135 | UseResFile(output) |
| 136 | |
| 137 | # Delete any existing 'PYC 'resource named __main__ |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 138 | |
| 139 | try: |
| 140 | res = Get1NamedResource(RESTYPE, RESNAME) |
| 141 | res.RmveResource() |
| 142 | except Error: |
| 143 | pass |
| 144 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 145 | # Create the raw data for the resource from the code object |
| 146 | |
| 147 | data = marshal.dumps(code) |
| 148 | del code |
| 149 | data = (MAGIC + '\0\0\0\0') + data |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 150 | |
| 151 | # Create the resource and write it |
| 152 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 153 | id = 0 |
| 154 | while id < 128: |
| 155 | id = Unique1ID(RESTYPE) |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 156 | res = Resource(data) |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 157 | res.AddResource(RESTYPE, id, RESNAME) |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 158 | res.WriteResource() |
| 159 | res.ReleaseResource() |
| 160 | |
| 161 | # Close the resource file |
| 162 | |
Guido van Rossum | 6a0fb6e | 1995-02-18 15:05:39 +0000 | [diff] [blame^] | 163 | CloseResFile(output) |
Guido van Rossum | 2d54692 | 1995-02-17 14:28:39 +0000 | [diff] [blame] | 164 | |
| 165 | if __name__ == '__main__': |
| 166 | main() |