From 2d54692fcc291917f58d2112e9ed1b0f307b67d8 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 17 Feb 1995 14:28:39 +0000 Subject: completely redone --- Mac/MPW/Build | 3 ++ Mac/mkapplet.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 Mac/MPW/Build create mode 100644 Mac/mkapplet.py diff --git a/Mac/MPW/Build b/Mac/MPW/Build new file mode 100644 index 0000000..f85c0da --- /dev/null +++ b/Mac/MPW/Build @@ -0,0 +1,3 @@ +Make {1} -f MakeFile >Make.out +Set Echo 1 +Make.out diff --git a/Mac/mkapplet.py b/Mac/mkapplet.py new file mode 100644 index 0000000..d5cfba3 --- /dev/null +++ b/Mac/mkapplet.py @@ -0,0 +1,89 @@ +"""Add a 'PYC ' resource named "__main__" to a resource file. + +This first puts up a dialog asking for a Python source file ('TEXT'), +then one asking for an existing destination file ('APPL' or 'rsrc'). + +It compiles the Python source into a code object, marshals it into a string, +prefixes the string with a .pyc header, and turns it into a resource, +which is then written to the destination. + +If the destination already contains a resource with the same name and type, +it is overwritten. +""" + +import marshal +import imp +import macfs +from Res import * + +# .pyc file (and 'PYC ' resource magic number) +MAGIC = imp.get_magic() + +# Complete specification of our resource +RESTYPE = 'PYC ' +RESID = 128 +RESNAME = '__main__' + +# OpenResFile mode parameters +READ = 1 +WRITE = 2 + +def main(): + + # Ask for source text + + srcfss, ok = macfs.StandardGetFile('TEXT') + if not ok: return + + # Read the source and compile it + # (there's no point asking for a destination if it has a syntax error) + + filename = srcfss.as_pathname() + fp = open(filename) + text = fp.read() + fp.close() + code = compile(text, filename, "exec") + + # Ask for destination file + + dstfss, ok = macfs.StandardGetFile('APPL', 'rsrc') + if not ok: return + + # Create the raw data for the resource from the code object + + data = marshal.dumps(code) + del code + data = (MAGIC + '\0\0\0\0') + data + + # Open the resource file + + fnum = FSpOpenResFile(dstfss.as_pathname(), WRITE) + + # Delete any existing resource with name __main__ or number 128 + + try: + res = Get1NamedResource(RESTYPE, RESNAME) + res.RmveResource() + except Error: + pass + + try: + res = Get1Resource(RESTYPE, RESID) + res.RmveResource() + except Error: + pass + + # Create the resource and write it + + res = Resource(data) + res.AddResource(RESTYPE, RESID, RESNAME) + # XXX Are the following two calls needed? + res.WriteResource() + res.ReleaseResource() + + # Close the resource file + + CloseResFile(fnum) + +if __name__ == '__main__': + main() -- cgit v0.12