1996-05-31 13:02:21 +00:00
|
|
|
"""Creation of PYC resources"""
|
|
|
|
import os
|
2001-08-25 12:15:04 +00:00
|
|
|
from Carbon import Res
|
1996-05-31 13:02:21 +00:00
|
|
|
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
|
|
|
|
|
1998-08-18 12:23:11 +00:00
|
|
|
def writemodule(name, id, data, type='PYC ', preload=0, ispackage=0):
|
1996-05-31 13:02:21 +00:00
|
|
|
"""Write pyc code to a PYC resource with given name and id."""
|
|
|
|
# XXXX Check that it doesn't exist
|
1998-08-18 12:23:11 +00:00
|
|
|
|
|
|
|
# Normally, byte 4-7 are the time stamp, but that is not used
|
|
|
|
# for 'PYC ' resources. We abuse byte 4 as a flag to indicate
|
|
|
|
# that it is a package rather than an ordinary module.
|
|
|
|
# See also macimport.c. (jvr)
|
|
|
|
if ispackage:
|
|
|
|
data = data[:4] + '\377\0\0\0' + data[8:] # flag resource as package
|
|
|
|
else:
|
|
|
|
data = data[:4] + '\0\0\0\0' + data[8:] # clear mod date field, used as package flag
|
1996-05-31 13:02:21 +00:00
|
|
|
res = Res.Resource(data)
|
|
|
|
res.AddResource(type, id, name)
|
1997-06-12 10:50:47 +00:00
|
|
|
if preload:
|
|
|
|
attrs = res.GetResAttrs()
|
|
|
|
attrs = attrs | 0x04
|
|
|
|
res.SetResAttrs(attrs)
|
1996-05-31 13:02:21 +00:00
|
|
|
res.WriteResource()
|
|
|
|
res.ReleaseResource()
|
|
|
|
|
1998-08-18 12:23:11 +00:00
|
|
|
def frompycfile(file, name=None, preload=0, ispackage=0):
|
1996-05-31 13:02:21 +00:00
|
|
|
"""Copy one pyc file to the open resource file"""
|
|
|
|
if name == None:
|
|
|
|
d, name = os.path.split(file)
|
|
|
|
name = name[:-4]
|
|
|
|
id = findfreeid()
|
1998-08-18 12:23:11 +00:00
|
|
|
data = __builtin__.open(file, 'rb').read()
|
|
|
|
writemodule(name, id, data, preload=preload, ispackage=ispackage)
|
1996-05-31 13:02:21 +00:00
|
|
|
return id, name
|
|
|
|
|
1998-08-18 12:23:11 +00:00
|
|
|
def frompyfile(file, name=None, preload=0, ispackage=0):
|
1996-05-31 13:02:21 +00:00
|
|
|
"""Compile python source file to pyc file and add to resource file"""
|
|
|
|
import py_compile
|
|
|
|
|
|
|
|
py_compile.compile(file)
|
|
|
|
file = file +'c'
|
1998-08-18 12:23:11 +00:00
|
|
|
return frompycfile(file, name, preload=preload, ispackage=ispackage)
|
1996-05-31 13:02:21 +00:00
|
|
|
|
|
|
|
# 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
|