| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | """Create an applet from a Python script.
 | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | This puts up a dialog asking for a Python source file ('TEXT'). | 
					
						
							|  |  |  | The output is a file with the same name but its ".py" suffix dropped. | 
					
						
							|  |  |  | It is created by copying an applet template and then adding a 'PYC ' | 
					
						
							|  |  |  | resource named __main__ containing the compiled, marshalled script. | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | import sys | 
					
						
							|  |  |  | import string | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | import marshal | 
					
						
							|  |  |  | import imp | 
					
						
							|  |  |  | import macfs | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | import MacOS | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | from Res import * | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # .pyc file (and 'PYC ' resource magic number) | 
					
						
							|  |  |  | MAGIC = imp.get_magic() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | # Template file (searched on sys.path) | 
					
						
							|  |  |  | TEMPLATE = "PythonApplet" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Specification of our resource | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | RESTYPE = 'PYC ' | 
					
						
							|  |  |  | RESNAME = '__main__' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # OpenResFile mode parameters | 
					
						
							|  |  |  | READ = 1 | 
					
						
							|  |  |  | WRITE = 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	# Find the template | 
					
						
							|  |  |  | 	# (there's no point in proceeding if we can't find it) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	for p in sys.path: | 
					
						
							|  |  |  | 		template = os.path.join(p, TEMPLATE) | 
					
						
							|  |  |  | 		try: | 
					
						
							|  |  |  | 			tmpl = open(template, "rb") | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		except IOError: | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		# XXX Ought to put up a dialog | 
					
						
							|  |  |  | 		print "Template not found.  Exit." | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	# Ask for source text | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	srcfss, ok = macfs.StandardGetFile('TEXT') | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	if not ok: | 
					
						
							|  |  |  | 		tmpl.close() | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	filename = srcfss.as_pathname() | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	# Read the source and compile it | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	# (there's no point overwriting the destination if it has a syntax error) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	fp = open(filename) | 
					
						
							|  |  |  | 	text = fp.read() | 
					
						
							|  |  |  | 	fp.close() | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	try: | 
					
						
							|  |  |  | 		code = compile(text, filename, "exec") | 
					
						
							|  |  |  | 	except (SyntaxError, EOFError): | 
					
						
							|  |  |  | 		print "Syntax error in script" | 
					
						
							|  |  |  | 		tmpl.close() | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	# Set the destination file name | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	if string.lower(filename[-3:]) == ".py": | 
					
						
							|  |  |  | 		destname = filename[:-3] | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		destname = filename + ".applet" | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	# Copy the data from the template (creating the file as well) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	dest = open(destname, "wb") | 
					
						
							|  |  |  | 	data = tmpl.read() | 
					
						
							|  |  |  | 	if data: | 
					
						
							|  |  |  | 		dest.write(data) | 
					
						
							|  |  |  | 	dest.close() | 
					
						
							|  |  |  | 	tmpl.close() | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	# Copy the creator and type of the template to the destination | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	ctor, type = MacOS.GetCreatorAndType(template) | 
					
						
							|  |  |  | 	MacOS.SetCreatorAndType(destname, ctor, type) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	# Open the input and output resource forks | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	input = FSpOpenResFile(template, READ) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	try: | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 		output = FSpOpenResFile(destname, WRITE) | 
					
						
							|  |  |  | 	except MacOS.Error: | 
					
						
							|  |  |  | 		print "Creating resource fork..." | 
					
						
							|  |  |  | 		CreateResFile(destname) | 
					
						
							|  |  |  | 		output = FSpOpenResFile(destname, WRITE) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# Copy the resources from the template, | 
					
						
							|  |  |  | 	# except a 'PYC ' resource named __main__ | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	UseResFile(input) | 
					
						
							|  |  |  | 	ntypes = Count1Types() | 
					
						
							|  |  |  | 	for itype in range(1, 1+ntypes): | 
					
						
							|  |  |  | 		type = Get1IndType(itype) | 
					
						
							|  |  |  | 		nresources = Count1Resources(type) | 
					
						
							|  |  |  | 		for ires in range(1, 1+nresources): | 
					
						
							|  |  |  | 			res = Get1IndResource(type, ires) | 
					
						
							|  |  |  | 			id, type, name = res.GetResInfo() | 
					
						
							|  |  |  | 			if (type, name) == (RESTYPE, RESNAME): | 
					
						
							|  |  |  | 				continue # Don't copy __main__ from template | 
					
						
							|  |  |  | 			size = res.SizeResource() | 
					
						
							|  |  |  | 			attrs = res.GetResAttrs() | 
					
						
							|  |  |  | 			print id, type, name, size, hex(attrs) | 
					
						
							|  |  |  | 			res.LoadResource() | 
					
						
							|  |  |  | 			res.DetachResource() | 
					
						
							|  |  |  | 			UseResFile(output) | 
					
						
							|  |  |  | 			try: | 
					
						
							|  |  |  | 				res2 = Get1Resource(type, id) | 
					
						
							|  |  |  | 			except MacOS.Error: | 
					
						
							|  |  |  | 				res2 = None | 
					
						
							|  |  |  | 			if res2: | 
					
						
							|  |  |  | 				print "Overwriting..." | 
					
						
							|  |  |  | 				res2.RmveResource() | 
					
						
							|  |  |  | 			res.AddResource(type, id, name) | 
					
						
							|  |  |  | 			#res.SetResAttrs(attrs) | 
					
						
							|  |  |  | 			res.WriteResource() | 
					
						
							|  |  |  | 			UseResFile(input) | 
					
						
							|  |  |  | 	CloseResFile(input) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# Make sure we're manipulating the output resource file now | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	UseResFile(output) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# Delete any existing 'PYC 'resource named __main__ | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	try: | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 		res = Get1NamedResource(RESTYPE, RESNAME) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 		res.RmveResource() | 
					
						
							|  |  |  | 	except Error: | 
					
						
							|  |  |  | 		pass | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	# Create the raw data for the resource from the code object | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	data = marshal.dumps(code) | 
					
						
							|  |  |  | 	del code | 
					
						
							|  |  |  | 	data = (MAGIC + '\0\0\0\0') + data | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	# Create the resource and write it | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	id = 0 | 
					
						
							|  |  |  | 	while id < 128: | 
					
						
							|  |  |  | 		id = Unique1ID(RESTYPE) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	res = Resource(data) | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	res.AddResource(RESTYPE, id, RESNAME) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 	res.WriteResource() | 
					
						
							|  |  |  | 	res.ReleaseResource() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	# Close the resource file | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-02-18 15:05:39 +00:00
										 |  |  | 	CloseResFile(output) | 
					
						
							| 
									
										
										
										
											1995-02-17 14:28:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  | 	main() |