summaryrefslogtreecommitdiffstats
path: root/Mac/mkapplet.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-02-17 14:28:39 (GMT)
committerGuido van Rossum <guido@python.org>1995-02-17 14:28:39 (GMT)
commit2d54692fcc291917f58d2112e9ed1b0f307b67d8 (patch)
treee623a05f7faac4e2afb2d1b4bcf637f15daaca39 /Mac/mkapplet.py
parent930b36b349851620dccef490c1c1e5aa6578f7bd (diff)
downloadcpython-2d54692fcc291917f58d2112e9ed1b0f307b67d8.zip
cpython-2d54692fcc291917f58d2112e9ed1b0f307b67d8.tar.gz
cpython-2d54692fcc291917f58d2112e9ed1b0f307b67d8.tar.bz2
completely redone
Diffstat (limited to 'Mac/mkapplet.py')
-rw-r--r--Mac/mkapplet.py89
1 files changed, 89 insertions, 0 deletions
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()