mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
Changed the way .rsrc and template are copied: hopefully this will
finally get the bundle stuff right.
This commit is contained in:
parent
9722ad8883
commit
3f14f4a3fc
1 changed files with 55 additions and 38 deletions
|
@ -31,6 +31,7 @@ RESTYPE = 'PYC '
|
||||||
RESNAME = '__main__'
|
RESNAME = '__main__'
|
||||||
|
|
||||||
# A resource with this name sets the "owner" (creator) of the destination
|
# A resource with this name sets the "owner" (creator) of the destination
|
||||||
|
# XXXX Should look for id=0
|
||||||
OWNERNAME = "owner resource"
|
OWNERNAME = "owner resource"
|
||||||
|
|
||||||
# OpenResFile mode parameters
|
# OpenResFile mode parameters
|
||||||
|
@ -60,7 +61,8 @@ def main():
|
||||||
# (there's no point in proceeding if we can't find it)
|
# (there's no point in proceeding if we can't find it)
|
||||||
|
|
||||||
template = findtemplate()
|
template = findtemplate()
|
||||||
print 'Using template', template
|
if DEBUG:
|
||||||
|
print 'Using template', template
|
||||||
|
|
||||||
# Ask for source text if not specified in sys.argv[1:]
|
# Ask for source text if not specified in sys.argv[1:]
|
||||||
|
|
||||||
|
@ -83,8 +85,6 @@ def main():
|
||||||
for filename in sys.argv[1:]:
|
for filename in sys.argv[1:]:
|
||||||
process(template, filename, '')
|
process(template, filename, '')
|
||||||
|
|
||||||
undefs = ('Atmp', '????', ' ', '\0\0\0\0', 'BINA')
|
|
||||||
|
|
||||||
def process(template, filename, output):
|
def process(template, filename, output):
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
|
@ -113,12 +113,22 @@ def process(template, filename, output):
|
||||||
|
|
||||||
if output:
|
if output:
|
||||||
destname = output
|
destname = output
|
||||||
# Copy the data from the template (creating the file as well)
|
|
||||||
|
# Try removing the output file
|
||||||
|
try:
|
||||||
|
os.unlink(output)
|
||||||
|
except os.error:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Create FSSpecs for the various files
|
||||||
|
|
||||||
template_fss = macfs.FSSpec(template)
|
template_fss = macfs.FSSpec(template)
|
||||||
template_fss, d1, d2 = macfs.ResolveAliasFile(template_fss)
|
template_fss, d1, d2 = macfs.ResolveAliasFile(template_fss)
|
||||||
dest_fss = macfs.FSSpec(destname)
|
dest_fss = macfs.FSSpec(destname)
|
||||||
|
|
||||||
|
# Copy data (not resources, yet) from the template
|
||||||
|
|
||||||
tmpl = open(template, "rb")
|
tmpl = open(template, "rb")
|
||||||
dest = open(destname, "wb")
|
dest = open(destname, "wb")
|
||||||
data = tmpl.read()
|
data = tmpl.read()
|
||||||
|
@ -127,14 +137,6 @@ def process(template, filename, output):
|
||||||
dest.close()
|
dest.close()
|
||||||
tmpl.close()
|
tmpl.close()
|
||||||
|
|
||||||
# Copy the creator of the template to the destination
|
|
||||||
# unless it already got one. Set type to APPL
|
|
||||||
|
|
||||||
tctor, ttype = template_fss.GetCreatorType()
|
|
||||||
ctor, type = dest_fss.GetCreatorType()
|
|
||||||
if type in undefs: type = 'APPL'
|
|
||||||
if ctor in undefs: ctor = tctor
|
|
||||||
|
|
||||||
# Open the output resource fork
|
# Open the output resource fork
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -144,30 +146,38 @@ def process(template, filename, output):
|
||||||
print "Creating resource fork..."
|
print "Creating resource fork..."
|
||||||
CreateResFile(destname)
|
CreateResFile(destname)
|
||||||
output = FSpOpenResFile(dest_fss, WRITE)
|
output = FSpOpenResFile(dest_fss, WRITE)
|
||||||
|
|
||||||
|
# Copy the resources from the target specific resource template, if any
|
||||||
|
typesfound, ownertype = [], None
|
||||||
|
try:
|
||||||
|
input = FSpOpenResFile(rsrcname, READ)
|
||||||
|
except (MacOS.Error, ValueError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
typesfound, ownertype = copyres(input, output, [], 0)
|
||||||
|
CloseResFile(input)
|
||||||
|
|
||||||
|
# Check which resource-types we should not copy from the template
|
||||||
|
skiptypes = []
|
||||||
|
if 'SIZE' in typesfound: skiptypes.append('SIZE')
|
||||||
|
if 'BNDL' in typesfound: skiptypes = skiptypes + ['BNDL', 'FREF', 'icl4',
|
||||||
|
'icl8', 'ics4', 'ics8', 'ICN#', 'ics#']
|
||||||
|
skipowner = (ownertype <> None)
|
||||||
|
|
||||||
# Copy the resources from the template
|
# Copy the resources from the template
|
||||||
|
|
||||||
input = FSpOpenResFile(template_fss, READ)
|
input = FSpOpenResFile(template_fss, READ)
|
||||||
newctor = copyres(input, output)
|
dummy, tmplowner = copyres(input, output, skiptypes, skipowner)
|
||||||
|
if ownertype == None:
|
||||||
|
ownertype = tmplowner
|
||||||
CloseResFile(input)
|
CloseResFile(input)
|
||||||
if newctor: ctor = newctor
|
if ownertype == None:
|
||||||
|
die("No owner resource found in either resource file or template")
|
||||||
# Copy the resources from the target specific resource template, if any
|
|
||||||
|
|
||||||
try:
|
|
||||||
input = FSpOpenResFile(rsrcname, READ)
|
|
||||||
except (MacOS.Error, ValueError):
|
|
||||||
print 'No resource file', rsrcname
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
newctor = copyres(input, output)
|
|
||||||
CloseResFile(input)
|
|
||||||
if newctor: ctor = newctor
|
|
||||||
|
|
||||||
# Now set the creator, type and bundle bit of the destination
|
# Now set the creator, type and bundle bit of the destination
|
||||||
dest_finfo = dest_fss.GetFInfo()
|
dest_finfo = dest_fss.GetFInfo()
|
||||||
dest_finfo.Creator = ctor
|
dest_finfo.Creator = ownertype
|
||||||
dest_finfo.Type = type
|
dest_finfo.Type = 'APPL'
|
||||||
dest_finfo.Flags = dest_finfo.Flags | MACFS.kHasBundle
|
dest_finfo.Flags = dest_finfo.Flags | MACFS.kHasBundle
|
||||||
dest_finfo.Flags = dest_finfo.Flags & ~MACFS.kHasBeenInited
|
dest_finfo.Flags = dest_finfo.Flags & ~MACFS.kHasBeenInited
|
||||||
dest_fss.SetFInfo(dest_finfo)
|
dest_fss.SetFInfo(dest_finfo)
|
||||||
|
@ -176,7 +186,7 @@ def process(template, filename, output):
|
||||||
|
|
||||||
UseResFile(output)
|
UseResFile(output)
|
||||||
|
|
||||||
# Delete any existing 'PYC 'resource named __main__
|
# Delete any existing 'PYC ' resource named __main__
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = Get1NamedResource(RESTYPE, RESNAME)
|
res = Get1NamedResource(RESTYPE, RESNAME)
|
||||||
|
@ -210,16 +220,19 @@ def process(template, filename, output):
|
||||||
|
|
||||||
|
|
||||||
# Copy resources between two resource file descriptors.
|
# Copy resources between two resource file descriptors.
|
||||||
# Exception: don't copy a __main__ resource.
|
# skip a resource named '__main__' or (if skipowner is set) 'Owner resource'.
|
||||||
# If a resource's name is "owner resource", its type is returned
|
# Also skip resources with a type listed in skiptypes.
|
||||||
# (so the caller can use it to set the destination's creator)
|
#
|
||||||
|
def copyres(input, output, skiptypes, skipowner):
|
||||||
def copyres(input, output):
|
|
||||||
ctor = None
|
ctor = None
|
||||||
|
alltypes = []
|
||||||
UseResFile(input)
|
UseResFile(input)
|
||||||
ntypes = Count1Types()
|
ntypes = Count1Types()
|
||||||
for itype in range(1, 1+ntypes):
|
for itype in range(1, 1+ntypes):
|
||||||
type = Get1IndType(itype)
|
type = Get1IndType(itype)
|
||||||
|
if type in skiptypes:
|
||||||
|
continue
|
||||||
|
alltypes.append(type)
|
||||||
nresources = Count1Resources(type)
|
nresources = Count1Resources(type)
|
||||||
for ires in range(1, 1+nresources):
|
for ires in range(1, 1+nresources):
|
||||||
res = Get1IndResource(type, ires)
|
res = Get1IndResource(type, ires)
|
||||||
|
@ -227,7 +240,12 @@ def copyres(input, output):
|
||||||
lcname = string.lower(name)
|
lcname = string.lower(name)
|
||||||
if (type, lcname) == (RESTYPE, RESNAME):
|
if (type, lcname) == (RESTYPE, RESNAME):
|
||||||
continue # Don't copy __main__ from template
|
continue # Don't copy __main__ from template
|
||||||
if lcname == OWNERNAME: ctor = type
|
# XXXX should look for id=0
|
||||||
|
if lcname == OWNERNAME:
|
||||||
|
if skipowner:
|
||||||
|
continue # Skip this one
|
||||||
|
else:
|
||||||
|
ctor = type
|
||||||
size = res.size
|
size = res.size
|
||||||
attrs = res.GetResAttrs()
|
attrs = res.GetResAttrs()
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
|
@ -250,7 +268,7 @@ def copyres(input, output):
|
||||||
print "New attrs =", hex(attrs)
|
print "New attrs =", hex(attrs)
|
||||||
res.SetResAttrs(attrs)
|
res.SetResAttrs(attrs)
|
||||||
UseResFile(input)
|
UseResFile(input)
|
||||||
return ctor
|
return alltypes, ctor
|
||||||
|
|
||||||
|
|
||||||
# Show a message and exit
|
# Show a message and exit
|
||||||
|
@ -279,4 +297,3 @@ def message(str, id = 256):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue