summaryrefslogtreecommitdiffstats
path: root/Mac/Lib
diff options
context:
space:
mode:
authorJack Jansen <jack.jansen@cwi.nl>1996-05-31 13:02:21 (GMT)
committerJack Jansen <jack.jansen@cwi.nl>1996-05-31 13:02:21 (GMT)
commit0acb7f7a57b6bf74cf46bffecf3a54e6c3216444 (patch)
treedfb5b59539d42fada1cef1b95a8f545c652770f1 /Mac/Lib
parent31dd5c083bfca98df81cfc853de3b544e312c8a7 (diff)
downloadcpython-0acb7f7a57b6bf74cf46bffecf3a54e6c3216444.zip
cpython-0acb7f7a57b6bf74cf46bffecf3a54e6c3216444.tar.gz
cpython-0acb7f7a57b6bf74cf46bffecf3a54e6c3216444.tar.bz2
Module to handle PYC resources
Diffstat (limited to 'Mac/Lib')
-rw-r--r--Mac/Lib/py_resource.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/Mac/Lib/py_resource.py b/Mac/Lib/py_resource.py
new file mode 100644
index 0000000..8530b59
--- /dev/null
+++ b/Mac/Lib/py_resource.py
@@ -0,0 +1,76 @@
+"""Creation of PYC resources"""
+import os
+import Res
+import __builtin__
+
+READ = 1
+WRITE = 2
+smAllScripts = -3
+
+def Pstring(str):
+ """Return a pascal-style string from a python-style string"""
+ if len(str) > 255:
+ raise ValueError, 'String too large'
+ return chr(len(str))+str
+
+def create(dst, creator='Pyth'):
+ """Create output file. Return handle and first id to use."""
+
+ try:
+ os.unlink(dst)
+ except os.error:
+ pass
+ Res.FSpCreateResFile(dst, creator, 'rsrc', smAllScripts)
+ return open(dst)
+
+def open(dst):
+ output = Res.FSpOpenResFile(dst, WRITE)
+ Res.UseResFile(output)
+ return output
+
+def writemodule(name, id, data, type='PYC '):
+ """Write pyc code to a PYC resource with given name and id."""
+ # XXXX Check that it doesn't exist
+ res = Res.Resource(data)
+ res.AddResource(type, id, name)
+ res.WriteResource()
+ res.ReleaseResource()
+
+def frompycfile(file, name=None):
+ """Copy one pyc file to the open resource file"""
+ if name == None:
+ d, name = os.path.split(file)
+ name = name[:-4]
+ id = findfreeid()
+ writemodule(name, id, __builtin__.open(file, 'rb').read())
+ return id, name
+
+def frompyfile(file, name=None):
+ """Compile python source file to pyc file and add to resource file"""
+ import py_compile
+
+ py_compile.compile(file)
+ file = file +'c'
+ return frompycfile(file, name)
+
+# XXXX Note this is incorrect, it only handles one type and one file....
+
+_firstfreeid = None
+
+def findfreeid(type='PYC '):
+ """Find next free id-number for given resource type"""
+ global _firstfreeid
+
+ if _firstfreeid == None:
+ Res.SetResLoad(0)
+ highest = 511
+ num = Res.Count1Resources(type)
+ for i in range(1, num+1):
+ r = Res.Get1IndResource(type, i)
+ id, d1, d2 = r.GetResInfo()
+ highest = max(highest, id)
+ Res.SetResLoad(1)
+ _firstfreeid = highest+1
+ id = _firstfreeid
+ _firstfreeid = _firstfreeid+1
+ return id