blob: 99486d4878fa957c8b3f18e489c9a3a3a1940e24 [file] [log] [blame]
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +00001"""Create an applet from a Python script.
Guido van Rossum2d546921995-02-17 14:28:39 +00002
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +00003This puts up a dialog asking for a Python source file ('TEXT').
4The output is a file with the same name but its ".py" suffix dropped.
5It is created by copying an applet template and then adding a 'PYC '
6resource named __main__ containing the compiled, marshalled script.
Guido van Rossum2d546921995-02-17 14:28:39 +00007"""
8
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +00009import sys
Guido van Rossum57128fd1995-02-19 15:49:17 +000010sys.stdout = sys.stderr
11
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000012import string
13import os
Guido van Rossum2d546921995-02-17 14:28:39 +000014import marshal
15import imp
16import macfs
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000017import MacOS
Guido van Rossum2d546921995-02-17 14:28:39 +000018from Res import *
19
20# .pyc file (and 'PYC ' resource magic number)
21MAGIC = imp.get_magic()
22
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000023# Template file (searched on sys.path)
24TEMPLATE = "PythonApplet"
25
26# Specification of our resource
Guido van Rossum2d546921995-02-17 14:28:39 +000027RESTYPE = 'PYC '
Guido van Rossum2d546921995-02-17 14:28:39 +000028RESNAME = '__main__'
29
Guido van Rossum83c434b1995-02-26 10:19:42 +000030# A resource with this name sets the "owner" (creator) of the destination
31OWNERNAME = "owner resource"
32
Guido van Rossum2d546921995-02-17 14:28:39 +000033# OpenResFile mode parameters
34READ = 1
35WRITE = 2
36
37def main():
38
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000039 # Find the template
40 # (there's no point in proceeding if we can't find it)
41
42 for p in sys.path:
43 template = os.path.join(p, TEMPLATE)
44 try:
45 tmpl = open(template, "rb")
Guido van Rossum57128fd1995-02-19 15:49:17 +000046 tmpl.close()
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000047 break
48 except IOError:
49 continue
50 else:
Guido van Rossum42a69c81995-02-20 23:44:14 +000051 die("Template %s not found" % `template`)
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000052 return
53
Guido van Rossum57128fd1995-02-19 15:49:17 +000054 # Ask for source text if not specified in sys.argv[1:]
Guido van Rossum2d546921995-02-17 14:28:39 +000055
Guido van Rossum57128fd1995-02-19 15:49:17 +000056 if not sys.argv[1:]:
57 srcfss, ok = macfs.StandardGetFile('TEXT')
58 if not ok:
59 return
60 filename = srcfss.as_pathname()
Jack Jansen2120d121995-02-27 16:19:07 +000061 tp, tf = os.path.split(filename)
62 if tf[-3:] == '.py':
63 tf = tf[:-3]
64 else:
65 tf = tf + '.applet'
66 dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
67 if not ok: return
68 process(template, filename, dstfss.as_pathname())
69 else:
70
71 # Loop over all files to be processed
72 for filename in sys.argv[1:]:
73 process(template, filename, '')
Guido van Rossum57128fd1995-02-19 15:49:17 +000074
Jack Jansen397c3fb1995-02-24 22:46:51 +000075undefs = ('????', ' ', '\0\0\0\0', 'BINA')
Guido van Rossum57128fd1995-02-19 15:49:17 +000076
Jack Jansen2120d121995-02-27 16:19:07 +000077def process(template, filename, output):
Guido van Rossum57128fd1995-02-19 15:49:17 +000078
79 print "Processing", `filename`, "..."
Guido van Rossum2d546921995-02-17 14:28:39 +000080
81 # Read the source and compile it
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000082 # (there's no point overwriting the destination if it has a syntax error)
Guido van Rossum2d546921995-02-17 14:28:39 +000083
Guido van Rossum2d546921995-02-17 14:28:39 +000084 fp = open(filename)
85 text = fp.read()
86 fp.close()
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000087 try:
88 code = compile(text, filename, "exec")
89 except (SyntaxError, EOFError):
Guido van Rossum42a69c81995-02-20 23:44:14 +000090 die("Syntax error in script %s" % `filename`)
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000091 return
Guido van Rossum2d546921995-02-17 14:28:39 +000092
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000093 # Set the destination file name
Guido van Rossum2d546921995-02-17 14:28:39 +000094
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000095 if string.lower(filename[-3:]) == ".py":
96 destname = filename[:-3]
Guido van Rossum42a69c81995-02-20 23:44:14 +000097 rsrcname = destname + '.rsrc'
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +000098 else:
99 destname = filename + ".applet"
Guido van Rossum42a69c81995-02-20 23:44:14 +0000100 rsrcname = filename + '.rsrc'
Guido van Rossum2d546921995-02-17 14:28:39 +0000101
Jack Jansen2120d121995-02-27 16:19:07 +0000102 if output:
103 destname = output
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000104 # Copy the data from the template (creating the file as well)
Guido van Rossum2d546921995-02-17 14:28:39 +0000105
Guido van Rossum57128fd1995-02-19 15:49:17 +0000106 tmpl = open(template, "rb")
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000107 dest = open(destname, "wb")
108 data = tmpl.read()
109 if data:
110 dest.write(data)
111 dest.close()
112 tmpl.close()
Guido van Rossum2d546921995-02-17 14:28:39 +0000113
Jack Jansen77b58281995-02-20 15:49:27 +0000114 # Copy the creator of the template to the destination
Guido van Rossum42a69c81995-02-20 23:44:14 +0000115 # unless it already got one. Set type to APPL
116
Guido van Rossum57128fd1995-02-19 15:49:17 +0000117 tctor, ttype = MacOS.GetCreatorAndType(template)
118 ctor, type = MacOS.GetCreatorAndType(destname)
Jack Jansen77b58281995-02-20 15:49:27 +0000119 if type in undefs: type = 'APPL'
Guido van Rossum57128fd1995-02-19 15:49:17 +0000120 if ctor in undefs: ctor = tctor
Guido van Rossum2d546921995-02-17 14:28:39 +0000121
Guido van Rossum42a69c81995-02-20 23:44:14 +0000122 # Open the output resource fork
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000123
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000124 try:
125 output = FSpOpenResFile(destname, WRITE)
126 except MacOS.Error:
127 print "Creating resource fork..."
128 CreateResFile(destname)
129 output = FSpOpenResFile(destname, WRITE)
130
Guido van Rossum42a69c81995-02-20 23:44:14 +0000131 # Copy the resources from the template
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000132
Guido van Rossum42a69c81995-02-20 23:44:14 +0000133 input = FSpOpenResFile(template, READ)
Guido van Rossum83c434b1995-02-26 10:19:42 +0000134 newctor = copyres(input, output)
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000135 CloseResFile(input)
Guido van Rossum83c434b1995-02-26 10:19:42 +0000136 if newctor: ctor = newctor
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000137
Guido van Rossum42a69c81995-02-20 23:44:14 +0000138 # Copy the resources from the target specific resource template, if any
139
140 try:
141 input = FSpOpenResFile(rsrcname, READ)
142 except MacOS.Error:
143 pass
144 else:
Guido van Rossum83c434b1995-02-26 10:19:42 +0000145 newctor = copyres(input, output)
Guido van Rossum42a69c81995-02-20 23:44:14 +0000146 CloseResFile(input)
Guido van Rossum83c434b1995-02-26 10:19:42 +0000147 if newctor: ctor = newctor
148
149 # Now set the creator and type of the destination
150
151 MacOS.SetCreatorAndType(destname, ctor, type)
Guido van Rossum42a69c81995-02-20 23:44:14 +0000152
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000153 # Make sure we're manipulating the output resource file now
154
155 UseResFile(output)
156
157 # Delete any existing 'PYC 'resource named __main__
Guido van Rossum2d546921995-02-17 14:28:39 +0000158
159 try:
160 res = Get1NamedResource(RESTYPE, RESNAME)
161 res.RmveResource()
162 except Error:
163 pass
164
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000165 # Create the raw data for the resource from the code object
166
167 data = marshal.dumps(code)
168 del code
169 data = (MAGIC + '\0\0\0\0') + data
Guido van Rossum2d546921995-02-17 14:28:39 +0000170
171 # Create the resource and write it
172
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000173 id = 0
174 while id < 128:
175 id = Unique1ID(RESTYPE)
Guido van Rossum2d546921995-02-17 14:28:39 +0000176 res = Resource(data)
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000177 res.AddResource(RESTYPE, id, RESNAME)
Guido van Rossum2d546921995-02-17 14:28:39 +0000178 res.WriteResource()
179 res.ReleaseResource()
180
Guido van Rossum42a69c81995-02-20 23:44:14 +0000181 # Close the output file
Guido van Rossum2d546921995-02-17 14:28:39 +0000182
Guido van Rossum6a0fb6e1995-02-18 15:05:39 +0000183 CloseResFile(output)
Guido van Rossum57128fd1995-02-19 15:49:17 +0000184
Guido van Rossum42a69c81995-02-20 23:44:14 +0000185 # Give positive feedback
186
187 message("Applet %s created." % `destname`)
188
189
Guido van Rossum83c434b1995-02-26 10:19:42 +0000190# Copy resources between two resource file descriptors.
191# Exception: don't copy a __main__ resource.
192# If a resource's name is "owner resource", its type is returned
193# (so the caller can use it to set the destination's creator)
Guido van Rossum42a69c81995-02-20 23:44:14 +0000194
195def copyres(input, output):
Guido van Rossum83c434b1995-02-26 10:19:42 +0000196 ctor = None
Guido van Rossum42a69c81995-02-20 23:44:14 +0000197 UseResFile(input)
198 ntypes = Count1Types()
199 for itype in range(1, 1+ntypes):
200 type = Get1IndType(itype)
201 nresources = Count1Resources(type)
202 for ires in range(1, 1+nresources):
203 res = Get1IndResource(type, ires)
204 id, type, name = res.GetResInfo()
Guido van Rossum83c434b1995-02-26 10:19:42 +0000205 lcname = string.lower(name)
206 if (type, lcname) == (RESTYPE, RESNAME):
Guido van Rossum42a69c81995-02-20 23:44:14 +0000207 continue # Don't copy __main__ from template
Guido van Rossum83c434b1995-02-26 10:19:42 +0000208 if lcname == OWNERNAME: ctor = type
Guido van Rossum42a69c81995-02-20 23:44:14 +0000209 size = res.SizeResource()
210 attrs = res.GetResAttrs()
211 print id, type, name, size, hex(attrs)
212 res.LoadResource()
213 res.DetachResource()
214 UseResFile(output)
215 try:
216 res2 = Get1Resource(type, id)
217 except MacOS.Error:
218 res2 = None
219 if res2:
220 print "Overwriting..."
221 res2.RmveResource()
222 res.AddResource(type, id, name)
Guido van Rossum42a69c81995-02-20 23:44:14 +0000223 res.WriteResource()
Guido van Rossum83c434b1995-02-26 10:19:42 +0000224 attrs = attrs | res.GetResAttrs()
225 print "New attrs =", hex(attrs)
226 res.SetResAttrs(attrs)
Guido van Rossum42a69c81995-02-20 23:44:14 +0000227 UseResFile(input)
Guido van Rossum83c434b1995-02-26 10:19:42 +0000228 return ctor
Guido van Rossum42a69c81995-02-20 23:44:14 +0000229
230
231# Show a message and exit
232
233def die(str):
234 message(str)
235 sys.exit(1)
236
237
238# Show a message
239
240def message(str, id = 256):
241 from Dlg import *
242 d = GetNewDialog(id, -1)
243 if not d:
244 print "Error:", `str`
245 print "DLOG id =", id, "not found."
246 return
247 tp, h, rect = d.GetDItem(2)
248 SetIText(h, str)
249 while 1:
250 n = ModalDialog(None)
251 if n == 1: break
252 del d
Guido van Rossum57128fd1995-02-19 15:49:17 +0000253
Guido van Rossum2d546921995-02-17 14:28:39 +0000254
255if __name__ == '__main__':
256 main()
Guido van Rossum83c434b1995-02-26 10:19:42 +0000257