1998-07-31 06:42:35 -03:00
|
|
|
"""tools for BuildApplet and BuildApplication"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import string
|
|
|
|
import imp
|
|
|
|
import marshal
|
|
|
|
import macfs
|
2001-08-25 09:15:04 -03:00
|
|
|
from Carbon import Res
|
1998-07-31 06:42:35 -03:00
|
|
|
import MACFS
|
|
|
|
import MacOS
|
|
|
|
import macostools
|
2002-03-29 17:21:28 -04:00
|
|
|
import macresource
|
1998-07-31 06:42:35 -03:00
|
|
|
import EasyDialogs
|
2002-03-29 17:21:28 -04:00
|
|
|
import shutil
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
|
|
|
|
BuildError = "BuildError"
|
|
|
|
|
|
|
|
# .pyc file (and 'PYC ' resource magic number)
|
|
|
|
MAGIC = imp.get_magic()
|
|
|
|
|
|
|
|
# Template file (searched on sys.path)
|
1999-02-14 19:12:06 -04:00
|
|
|
TEMPLATE = "PythonInterpreter"
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
# Specification of our resource
|
|
|
|
RESTYPE = 'PYC '
|
|
|
|
RESNAME = '__main__'
|
|
|
|
|
|
|
|
# A resource with this name sets the "owner" (creator) of the destination
|
1999-03-17 18:57:55 -04:00
|
|
|
# It should also have ID=0. Either of these alone is not enough.
|
1998-07-31 06:42:35 -03:00
|
|
|
OWNERNAME = "owner resource"
|
|
|
|
|
1999-03-17 18:57:55 -04:00
|
|
|
# Default applet creator code
|
|
|
|
DEFAULT_APPLET_CREATOR="Pyta"
|
|
|
|
|
1998-07-31 06:42:35 -03:00
|
|
|
# OpenResFile mode parameters
|
|
|
|
READ = 1
|
|
|
|
WRITE = 2
|
|
|
|
|
|
|
|
|
2001-02-17 19:30:19 -04:00
|
|
|
def findtemplate(template=None):
|
1998-07-31 06:42:35 -03:00
|
|
|
"""Locate the applet template along sys.path"""
|
2002-03-29 17:21:28 -04:00
|
|
|
if MacOS.runtimemodel == 'macho':
|
|
|
|
if template:
|
|
|
|
return template
|
|
|
|
return findtemplate_macho()
|
2001-02-17 19:30:19 -04:00
|
|
|
if not template:
|
|
|
|
template=TEMPLATE
|
1998-07-31 06:42:35 -03:00
|
|
|
for p in sys.path:
|
2001-02-17 19:30:19 -04:00
|
|
|
file = os.path.join(p, template)
|
1998-07-31 06:42:35 -03:00
|
|
|
try:
|
2001-02-17 19:30:19 -04:00
|
|
|
file, d1, d2 = macfs.ResolveAliasFile(file)
|
1998-07-31 06:42:35 -03:00
|
|
|
break
|
|
|
|
except (macfs.error, ValueError):
|
|
|
|
continue
|
|
|
|
else:
|
2001-02-17 19:30:19 -04:00
|
|
|
raise BuildError, "Template %s not found on sys.path" % `template`
|
|
|
|
file = file.as_pathname()
|
|
|
|
return file
|
2002-03-29 17:21:28 -04:00
|
|
|
|
|
|
|
def findtemplate_macho():
|
|
|
|
execpath = sys.executable.split('/')
|
|
|
|
if not 'Contents' in execpath:
|
|
|
|
raise BuildError, "Not running from a .app bundle: %s" % sys.executable
|
|
|
|
i = execpath.index('Contents')
|
|
|
|
return '/'.join(execpath[:i])
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
def process(template, filename, destname, copy_codefragment,
|
|
|
|
rsrcname=None, others=[], raw=0, progress="default"):
|
1998-07-31 06:42:35 -03:00
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress == "default":
|
1998-07-31 06:42:35 -03:00
|
|
|
progress = EasyDialogs.ProgressBar("Processing %s..."%os.path.split(filename)[1], 120)
|
|
|
|
progress.label("Compiling...")
|
2002-06-09 19:08:52 -03:00
|
|
|
progress.inc(0)
|
2002-08-18 18:57:09 -03:00
|
|
|
# check for the script name being longer than 32 chars. This may trigger a bug
|
|
|
|
# on OSX that can destroy your sourcefile.
|
|
|
|
if '#' in os.path.split(filename)[1]:
|
|
|
|
raise BuildError, "BuildApplet could destroy your sourcefile on OSX, please rename: %s" % filename
|
1998-07-31 06:42:35 -03:00
|
|
|
# Read the source and compile it
|
|
|
|
# (there's no point overwriting the destination if it has a syntax error)
|
|
|
|
|
2002-06-20 17:42:07 -03:00
|
|
|
fp = open(filename, 'rU')
|
1998-07-31 06:42:35 -03:00
|
|
|
text = fp.read()
|
|
|
|
fp.close()
|
|
|
|
try:
|
|
|
|
code = compile(text, filename, "exec")
|
2002-08-02 11:04:15 -03:00
|
|
|
except SyntaxError, arg:
|
|
|
|
raise BuildError, "Syntax error in script %s: %s" % (filename, arg)
|
|
|
|
except EOFError:
|
|
|
|
raise BuildError, "End-of-file in script %s" % (filename,)
|
1998-07-31 06:42:35 -03:00
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
# Set the destination file name. Note that basename
|
|
|
|
# does contain the whole filepath, only a .py is stripped.
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
if string.lower(filename[-3:]) == ".py":
|
2002-06-09 19:08:52 -03:00
|
|
|
basename = filename[:-3]
|
|
|
|
if MacOS.runtimemodel != 'macho' and not destname:
|
|
|
|
destname = basename
|
1998-07-31 06:42:35 -03:00
|
|
|
else:
|
2002-06-09 19:08:52 -03:00
|
|
|
basename = filename
|
|
|
|
|
|
|
|
if not destname:
|
2002-03-29 17:21:28 -04:00
|
|
|
if MacOS.runtimemodel == 'macho':
|
2002-06-09 19:08:52 -03:00
|
|
|
destname = basename + '.app'
|
2002-03-29 17:21:28 -04:00
|
|
|
else:
|
2002-06-09 19:08:52 -03:00
|
|
|
destname = basename + '.applet'
|
|
|
|
if not rsrcname:
|
|
|
|
rsrcname = basename + '.rsrc'
|
|
|
|
|
2002-03-29 17:21:28 -04:00
|
|
|
# Try removing the output file. This fails in MachO, but it should
|
|
|
|
# do any harm.
|
1998-07-31 06:42:35 -03:00
|
|
|
try:
|
|
|
|
os.remove(destname)
|
|
|
|
except os.error:
|
|
|
|
pass
|
2002-06-09 19:08:52 -03:00
|
|
|
process_common(template, progress, code, rsrcname, destname, 0,
|
|
|
|
copy_codefragment, raw, others)
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
|
|
|
|
def update(template, filename, output):
|
2002-03-29 17:21:28 -04:00
|
|
|
if MacOS.runtimemodel == 'macho':
|
|
|
|
raise BuildError, "No updating yet for MachO applets"
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress = EasyDialogs.ProgressBar("Updating %s..."%os.path.split(filename)[1], 120)
|
|
|
|
else:
|
|
|
|
progress = None
|
|
|
|
if not output:
|
|
|
|
output = filename + ' (updated)'
|
|
|
|
|
|
|
|
# Try removing the output file
|
|
|
|
try:
|
|
|
|
os.remove(output)
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
process_common(template, progress, None, filename, output, 1, 1)
|
|
|
|
|
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
def process_common(template, progress, code, rsrcname, destname, is_update,
|
|
|
|
copy_codefragment, raw=0, others=[]):
|
2002-03-29 17:21:28 -04:00
|
|
|
if MacOS.runtimemodel == 'macho':
|
2002-06-09 19:08:52 -03:00
|
|
|
return process_common_macho(template, progress, code, rsrcname, destname,
|
|
|
|
is_update, raw, others)
|
|
|
|
if others:
|
|
|
|
raise BuildError, "Extra files only allowed for MachoPython applets"
|
1998-07-31 06:42:35 -03:00
|
|
|
# Create FSSpecs for the various files
|
|
|
|
template_fss = macfs.FSSpec(template)
|
|
|
|
template_fss, d1, d2 = macfs.ResolveAliasFile(template_fss)
|
|
|
|
dest_fss = macfs.FSSpec(destname)
|
|
|
|
|
|
|
|
# Copy data (not resources, yet) from the template
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress.label("Copy data fork...")
|
|
|
|
progress.set(10)
|
|
|
|
|
|
|
|
if copy_codefragment:
|
|
|
|
tmpl = open(template, "rb")
|
|
|
|
dest = open(destname, "wb")
|
|
|
|
data = tmpl.read()
|
|
|
|
if data:
|
|
|
|
dest.write(data)
|
|
|
|
dest.close()
|
|
|
|
tmpl.close()
|
|
|
|
del dest
|
|
|
|
del tmpl
|
|
|
|
|
|
|
|
# Open the output resource fork
|
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress.label("Copy resources...")
|
|
|
|
progress.set(20)
|
|
|
|
try:
|
|
|
|
output = Res.FSpOpenResFile(dest_fss, WRITE)
|
|
|
|
except MacOS.Error:
|
2001-01-29 11:32:00 -04:00
|
|
|
Res.FSpCreateResFile(destname, '????', 'APPL', MACFS.smAllScripts)
|
1998-07-31 06:42:35 -03:00
|
|
|
output = Res.FSpOpenResFile(dest_fss, WRITE)
|
|
|
|
|
|
|
|
# Copy the resources from the target specific resource template, if any
|
|
|
|
typesfound, ownertype = [], None
|
|
|
|
try:
|
|
|
|
input = Res.FSpOpenResFile(rsrcname, READ)
|
|
|
|
except (MacOS.Error, ValueError):
|
|
|
|
pass
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress.inc(50)
|
|
|
|
else:
|
|
|
|
if is_update:
|
|
|
|
skip_oldfile = ['cfrg']
|
|
|
|
else:
|
|
|
|
skip_oldfile = []
|
|
|
|
typesfound, ownertype = copyres(input, output, skip_oldfile, 0, progress)
|
|
|
|
Res.CloseResFile(input)
|
|
|
|
|
|
|
|
# Check which resource-types we should not copy from the template
|
1999-03-17 18:57:55 -04:00
|
|
|
skiptypes = []
|
|
|
|
if 'vers' in typesfound: skiptypes.append('vers')
|
1998-07-31 06:42:35 -03:00
|
|
|
if 'SIZE' in typesfound: skiptypes.append('SIZE')
|
|
|
|
if 'BNDL' in typesfound: skiptypes = skiptypes + ['BNDL', 'FREF', 'icl4',
|
|
|
|
'icl8', 'ics4', 'ics8', 'ICN#', 'ics#']
|
|
|
|
if not copy_codefragment:
|
|
|
|
skiptypes.append('cfrg')
|
1999-03-17 18:57:55 -04:00
|
|
|
## skipowner = (ownertype <> None)
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
# Copy the resources from the template
|
|
|
|
|
|
|
|
input = Res.FSpOpenResFile(template_fss, READ)
|
1999-03-17 18:57:55 -04:00
|
|
|
dummy, tmplowner = copyres(input, output, skiptypes, 1, progress)
|
|
|
|
|
1998-07-31 06:42:35 -03:00
|
|
|
Res.CloseResFile(input)
|
1999-03-17 18:57:55 -04:00
|
|
|
## if ownertype == None:
|
|
|
|
## raise BuildError, "No owner resource found in either resource file or template"
|
1998-07-31 06:42:35 -03:00
|
|
|
# Make sure we're manipulating the output resource file now
|
|
|
|
|
|
|
|
Res.UseResFile(output)
|
1999-03-17 18:57:55 -04:00
|
|
|
|
|
|
|
if ownertype == None:
|
|
|
|
# No owner resource in the template. We have skipped the
|
|
|
|
# Python owner resource, so we have to add our own. The relevant
|
|
|
|
# bundle stuff is already included in the interpret/applet template.
|
|
|
|
newres = Res.Resource('\0')
|
|
|
|
newres.AddResource(DEFAULT_APPLET_CREATOR, 0, "Owner resource")
|
|
|
|
ownertype = DEFAULT_APPLET_CREATOR
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
if code:
|
|
|
|
# Delete any existing 'PYC ' resource named __main__
|
|
|
|
|
|
|
|
try:
|
|
|
|
res = Res.Get1NamedResource(RESTYPE, RESNAME)
|
|
|
|
res.RemoveResource()
|
|
|
|
except Res.Error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Create the raw data for the resource from the code object
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress.label("Write PYC resource...")
|
|
|
|
progress.set(120)
|
|
|
|
|
|
|
|
data = marshal.dumps(code)
|
|
|
|
del code
|
|
|
|
data = (MAGIC + '\0\0\0\0') + data
|
|
|
|
|
|
|
|
# Create the resource and write it
|
|
|
|
|
|
|
|
id = 0
|
|
|
|
while id < 128:
|
|
|
|
id = Res.Unique1ID(RESTYPE)
|
|
|
|
res = Res.Resource(data)
|
|
|
|
res.AddResource(RESTYPE, id, RESNAME)
|
1999-01-30 18:31:26 -04:00
|
|
|
attrs = res.GetResAttrs()
|
|
|
|
attrs = attrs | 0x04 # set preload
|
|
|
|
res.SetResAttrs(attrs)
|
1998-07-31 06:42:35 -03:00
|
|
|
res.WriteResource()
|
|
|
|
res.ReleaseResource()
|
|
|
|
|
|
|
|
# Close the output file
|
|
|
|
|
|
|
|
Res.CloseResFile(output)
|
|
|
|
|
|
|
|
# Now set the creator, type and bundle bit of the destination
|
|
|
|
dest_finfo = dest_fss.GetFInfo()
|
|
|
|
dest_finfo.Creator = ownertype
|
|
|
|
dest_finfo.Type = 'APPL'
|
1999-12-03 19:38:05 -04:00
|
|
|
dest_finfo.Flags = dest_finfo.Flags | MACFS.kHasBundle | MACFS.kIsShared
|
1998-07-31 06:42:35 -03:00
|
|
|
dest_finfo.Flags = dest_finfo.Flags & ~MACFS.kHasBeenInited
|
|
|
|
dest_fss.SetFInfo(dest_finfo)
|
|
|
|
|
|
|
|
macostools.touched(dest_fss)
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress.label("Done.")
|
2002-06-09 19:08:52 -03:00
|
|
|
progress.inc(0)
|
1998-07-31 06:42:35 -03:00
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
def process_common_macho(template, progress, code, rsrcname, destname, is_update, raw=0, others=[]):
|
2002-03-29 17:21:28 -04:00
|
|
|
# First make sure the name ends in ".app"
|
|
|
|
if destname[-4:] != '.app':
|
|
|
|
destname = destname + '.app'
|
|
|
|
# Now deduce the short name
|
|
|
|
shortname = os.path.split(destname)[1]
|
|
|
|
if shortname[-4:] == '.app':
|
|
|
|
# Strip the .app suffix
|
|
|
|
shortname = shortname[:-4]
|
2002-03-29 19:44:37 -04:00
|
|
|
# And deduce the .plist and .icns names
|
|
|
|
plistname = None
|
|
|
|
icnsname = None
|
|
|
|
if rsrcname and rsrcname[-5:] == '.rsrc':
|
|
|
|
tmp = rsrcname[:-5]
|
|
|
|
plistname = tmp + '.plist'
|
|
|
|
if os.path.exists(plistname):
|
|
|
|
icnsname = tmp + '.icns'
|
|
|
|
if not os.path.exists(icnsname):
|
|
|
|
icnsname = None
|
|
|
|
else:
|
|
|
|
plistname = None
|
2002-03-29 17:21:28 -04:00
|
|
|
# Start with copying the .app framework
|
|
|
|
if not is_update:
|
|
|
|
exceptlist = ["Contents/Info.plist",
|
|
|
|
"Contents/Resources/English.lproj/InfoPlist.strings",
|
2002-08-29 17:20:24 -03:00
|
|
|
"Contents/Resources/English.lproj/Documentation",
|
2002-03-29 17:21:28 -04:00
|
|
|
"Contents/Resources/python.rsrc",
|
|
|
|
]
|
2002-06-09 19:08:52 -03:00
|
|
|
copyapptree(template, destname, exceptlist, progress)
|
2002-03-29 17:21:28 -04:00
|
|
|
# Now either use the .plist file or the default
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
|
|
|
progress.label('Create info.plist')
|
|
|
|
progress.inc(0)
|
2002-03-29 19:44:37 -04:00
|
|
|
if plistname:
|
2002-06-09 19:08:52 -03:00
|
|
|
shutil.copy2(plistname, os.path.join(destname, 'Contents', 'Info.plist'))
|
2002-03-29 19:44:37 -04:00
|
|
|
if icnsname:
|
|
|
|
icnsdest = os.path.split(icnsname)[1]
|
|
|
|
icnsdest = os.path.join(destname,
|
2002-06-09 19:08:52 -03:00
|
|
|
os.path.join('Contents', 'Resources', icnsdest))
|
2002-03-29 19:44:37 -04:00
|
|
|
shutil.copy2(icnsname, icnsdest)
|
|
|
|
# XXXX Wrong. This should be parsed from plist file. Also a big hack:-)
|
|
|
|
if shortname == 'PythonIDE':
|
|
|
|
ownertype = 'Pide'
|
|
|
|
else:
|
|
|
|
ownertype = 'PytA'
|
2002-03-29 17:21:28 -04:00
|
|
|
# XXXX Should copy .icns file
|
|
|
|
else:
|
2002-06-09 19:08:52 -03:00
|
|
|
cocoainfo = ''
|
|
|
|
for o in others:
|
|
|
|
if o[-4:] == '.nib':
|
|
|
|
nibname = os.path.split(o)[1][:-4]
|
|
|
|
cocoainfo = """
|
|
|
|
<key>NSMainNibFile</key>
|
|
|
|
<string>%s</string>
|
|
|
|
<key>NSPrincipalClass</key>
|
|
|
|
<string>NSApplication</string>""" % nibname
|
2002-08-05 19:06:29 -03:00
|
|
|
elif o[-6:] == '.lproj':
|
|
|
|
files = os.listdir(o)
|
|
|
|
for f in files:
|
|
|
|
if f[-4:] == '.nib':
|
|
|
|
nibname = os.path.split(f)[1][:-4]
|
|
|
|
cocoainfo = """
|
|
|
|
<key>NSMainNibFile</key>
|
|
|
|
<string>%s</string>
|
|
|
|
<key>NSPrincipalClass</key>
|
|
|
|
<string>NSApplication</string>""" % nibname
|
2002-06-09 19:08:52 -03:00
|
|
|
|
|
|
|
plistname = os.path.join(template, 'Contents', 'Resources', 'Applet-Info.plist')
|
2002-03-29 17:21:28 -04:00
|
|
|
plistdata = open(plistname).read()
|
2002-06-09 19:08:52 -03:00
|
|
|
plistdata = plistdata % {'appletname':shortname, 'cocoainfo':cocoainfo}
|
|
|
|
ofp = open(os.path.join(destname, 'Contents', 'Info.plist'), 'w')
|
2002-03-29 17:21:28 -04:00
|
|
|
ofp.write(plistdata)
|
|
|
|
ofp.close()
|
|
|
|
ownertype = 'PytA'
|
|
|
|
# Create the PkgInfo file
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
|
|
|
progress.label('Create PkgInfo')
|
|
|
|
progress.inc(0)
|
|
|
|
ofp = open(os.path.join(destname, 'Contents', 'PkgInfo'), 'wb')
|
2002-03-29 17:21:28 -04:00
|
|
|
ofp.write('APPL' + ownertype)
|
|
|
|
ofp.close()
|
|
|
|
|
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
2002-03-29 17:21:28 -04:00
|
|
|
progress.label("Copy resources...")
|
|
|
|
progress.set(20)
|
2002-08-02 11:04:15 -03:00
|
|
|
resfilename = 'python.rsrc' # XXXX later: '%s.rsrc' % shortname
|
2002-03-29 17:21:28 -04:00
|
|
|
try:
|
|
|
|
output = Res.FSOpenResourceFile(
|
2002-06-09 19:08:52 -03:00
|
|
|
os.path.join(destname, 'Contents', 'Resources', resfilename),
|
2002-03-29 17:21:28 -04:00
|
|
|
u'', WRITE)
|
|
|
|
except MacOS.Error:
|
|
|
|
fsr, dummy = Res.FSCreateResourceFile(
|
2002-06-09 19:08:52 -03:00
|
|
|
os.path.join(destname, 'Contents', 'Resources'),
|
2002-03-29 17:21:28 -04:00
|
|
|
unicode(resfilename), '')
|
|
|
|
output = Res.FSOpenResourceFile(fsr, u'', WRITE)
|
|
|
|
|
|
|
|
# Copy the resources from the target specific resource template, if any
|
|
|
|
typesfound, ownertype = [], None
|
|
|
|
try:
|
|
|
|
input = macresource.open_pathname(rsrcname)
|
|
|
|
except (MacOS.Error, ValueError):
|
|
|
|
pass
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
2002-03-29 17:21:28 -04:00
|
|
|
progress.inc(50)
|
|
|
|
else:
|
|
|
|
typesfound, ownertype = copyres(input, output, [], 0, progress)
|
|
|
|
Res.CloseResFile(input)
|
|
|
|
|
|
|
|
# Check which resource-types we should not copy from the template
|
|
|
|
skiptypes = []
|
|
|
|
## if 'vers' in typesfound: skiptypes.append('vers')
|
|
|
|
## if 'SIZE' in typesfound: skiptypes.append('SIZE')
|
|
|
|
## if 'BNDL' in typesfound: skiptypes = skiptypes + ['BNDL', 'FREF', 'icl4',
|
|
|
|
## 'icl8', 'ics4', 'ics8', 'ICN#', 'ics#']
|
|
|
|
## if not copy_codefragment:
|
|
|
|
## skiptypes.append('cfrg')
|
|
|
|
## skipowner = (ownertype <> None)
|
|
|
|
|
|
|
|
# Copy the resources from the template
|
|
|
|
|
|
|
|
input = Res.FSOpenResourceFile(
|
2002-06-09 19:08:52 -03:00
|
|
|
os.path.join(template, 'Contents', 'Resources', 'python.rsrc'), u'', READ)
|
|
|
|
if progress:
|
|
|
|
progress.label("Copy standard resources...")
|
|
|
|
progress.inc(0)
|
|
|
|
## dummy, tmplowner = copyres(input, output, skiptypes, 1, progress)
|
|
|
|
dummy, tmplowner = copyres(input, output, skiptypes, 1, None)
|
2002-03-29 17:21:28 -04:00
|
|
|
|
|
|
|
Res.CloseResFile(input)
|
|
|
|
## if ownertype == None:
|
|
|
|
## raise BuildError, "No owner resource found in either resource file or template"
|
|
|
|
# Make sure we're manipulating the output resource file now
|
|
|
|
|
|
|
|
Res.CloseResFile(output)
|
|
|
|
|
|
|
|
if code:
|
2002-06-09 19:08:52 -03:00
|
|
|
if raw:
|
|
|
|
pycname = '__rawmain__.pyc'
|
|
|
|
else:
|
|
|
|
pycname = '__main__.pyc'
|
2002-08-02 11:57:43 -03:00
|
|
|
# And we also create __rawmain__.pyc
|
|
|
|
outputfilename = os.path.join(destname, 'Contents', 'Resources', '__rawmain__.pyc')
|
|
|
|
if progress:
|
|
|
|
progress.label('Creating __rawmain__.pyc')
|
|
|
|
progress.inc(0)
|
|
|
|
rawsourcefile = os.path.join(sys.prefix, 'Mac', 'Lib', 'appletrawmain.py')
|
|
|
|
rawsource = open(rawsourcefile, 'rU').read()
|
|
|
|
rawcode = compile(rawsource, rawsourcefile, 'exec')
|
|
|
|
writepycfile(rawcode, outputfilename)
|
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
outputfilename = os.path.join(destname, 'Contents', 'Resources', pycname)
|
|
|
|
if progress:
|
|
|
|
progress.label('Creating '+pycname)
|
|
|
|
progress.inc(0)
|
2002-03-29 17:21:28 -04:00
|
|
|
writepycfile(code, outputfilename)
|
2002-06-09 19:08:52 -03:00
|
|
|
# Copy other files the user asked for
|
|
|
|
for osrc in others:
|
|
|
|
oname = os.path.split(osrc)[1]
|
|
|
|
odst = os.path.join(destname, 'Contents', 'Resources', oname)
|
|
|
|
if progress:
|
|
|
|
progress.label('Copy ' + oname)
|
|
|
|
progress.inc(0)
|
|
|
|
if os.path.isdir(osrc):
|
|
|
|
copyapptree(osrc, odst)
|
|
|
|
else:
|
|
|
|
shutil.copy2(osrc, odst)
|
|
|
|
if progress:
|
|
|
|
progress.label('Done.')
|
|
|
|
progress.inc(0)
|
2002-03-29 17:21:28 -04:00
|
|
|
|
|
|
|
## macostools.touched(dest_fss)
|
1998-07-31 06:42:35 -03:00
|
|
|
|
|
|
|
# Copy resources between two resource file descriptors.
|
1999-03-17 18:57:55 -04:00
|
|
|
# skip a resource named '__main__' or (if skipowner is set) with ID zero.
|
1998-07-31 06:42:35 -03:00
|
|
|
# Also skip resources with a type listed in skiptypes.
|
|
|
|
#
|
|
|
|
def copyres(input, output, skiptypes, skipowner, progress=None):
|
|
|
|
ctor = None
|
|
|
|
alltypes = []
|
|
|
|
Res.UseResFile(input)
|
|
|
|
ntypes = Res.Count1Types()
|
|
|
|
progress_type_inc = 50/ntypes
|
|
|
|
for itype in range(1, 1+ntypes):
|
|
|
|
type = Res.Get1IndType(itype)
|
|
|
|
if type in skiptypes:
|
|
|
|
continue
|
|
|
|
alltypes.append(type)
|
|
|
|
nresources = Res.Count1Resources(type)
|
|
|
|
progress_cur_inc = progress_type_inc/nresources
|
|
|
|
for ires in range(1, 1+nresources):
|
|
|
|
res = Res.Get1IndResource(type, ires)
|
|
|
|
id, type, name = res.GetResInfo()
|
|
|
|
lcname = string.lower(name)
|
1999-03-17 18:57:55 -04:00
|
|
|
|
|
|
|
if lcname == OWNERNAME and id == 0:
|
1998-07-31 06:42:35 -03:00
|
|
|
if skipowner:
|
|
|
|
continue # Skip this one
|
|
|
|
else:
|
|
|
|
ctor = type
|
|
|
|
size = res.size
|
|
|
|
attrs = res.GetResAttrs()
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress.label("Copy %s %d %s"%(type, id, name))
|
|
|
|
progress.inc(progress_cur_inc)
|
|
|
|
res.LoadResource()
|
|
|
|
res.DetachResource()
|
|
|
|
Res.UseResFile(output)
|
|
|
|
try:
|
|
|
|
res2 = Res.Get1Resource(type, id)
|
|
|
|
except MacOS.Error:
|
|
|
|
res2 = None
|
|
|
|
if res2:
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
1998-07-31 06:42:35 -03:00
|
|
|
progress.label("Overwrite %s %d %s"%(type, id, name))
|
2002-06-09 19:08:52 -03:00
|
|
|
progress.inc(0)
|
1998-07-31 06:42:35 -03:00
|
|
|
res2.RemoveResource()
|
|
|
|
res.AddResource(type, id, name)
|
|
|
|
res.WriteResource()
|
|
|
|
attrs = attrs | res.GetResAttrs()
|
|
|
|
res.SetResAttrs(attrs)
|
|
|
|
Res.UseResFile(input)
|
|
|
|
return alltypes, ctor
|
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
def copyapptree(srctree, dsttree, exceptlist=[], progress=None):
|
2002-03-29 17:21:28 -04:00
|
|
|
names = []
|
|
|
|
if os.path.exists(dsttree):
|
|
|
|
shutil.rmtree(dsttree)
|
|
|
|
os.mkdir(dsttree)
|
|
|
|
todo = os.listdir(srctree)
|
|
|
|
while todo:
|
|
|
|
this, todo = todo[0], todo[1:]
|
|
|
|
if this in exceptlist:
|
|
|
|
continue
|
|
|
|
thispath = os.path.join(srctree, this)
|
|
|
|
if os.path.isdir(thispath):
|
|
|
|
thiscontent = os.listdir(thispath)
|
|
|
|
for t in thiscontent:
|
|
|
|
todo.append(os.path.join(this, t))
|
|
|
|
names.append(this)
|
|
|
|
for this in names:
|
|
|
|
srcpath = os.path.join(srctree, this)
|
|
|
|
dstpath = os.path.join(dsttree, this)
|
|
|
|
if os.path.isdir(srcpath):
|
|
|
|
os.mkdir(dstpath)
|
|
|
|
else:
|
2002-06-09 19:08:52 -03:00
|
|
|
if progress:
|
|
|
|
progress.label('Copy '+this)
|
|
|
|
progress.inc(0)
|
2002-03-29 17:21:28 -04:00
|
|
|
shutil.copy2(srcpath, dstpath)
|
|
|
|
|
|
|
|
def writepycfile(codeobject, cfile):
|
|
|
|
import marshal
|
|
|
|
fc = open(cfile, 'wb')
|
|
|
|
fc.write('\0\0\0\0') # MAGIC placeholder, written later
|
|
|
|
fc.write('\0\0\0\0') # Timestap placeholder, not needed
|
|
|
|
marshal.dump(codeobject, fc)
|
|
|
|
fc.flush()
|
|
|
|
fc.seek(0, 0)
|
|
|
|
fc.write(MAGIC)
|
|
|
|
fc.close()
|
1998-07-31 06:42:35 -03:00
|
|
|
|