1995-07-29 13:48:41 +00:00
|
|
|
"""Create an applet from a Python script.
|
|
|
|
|
|
|
|
This puts up a dialog asking for a Python source file ('TEXT').
|
|
|
|
The output is a file with the same name but its ".py" suffix dropped.
|
|
|
|
It is created by copying an applet template and then adding a 'PYC '
|
|
|
|
resource named __main__ containing the compiled, marshalled script.
|
|
|
|
"""
|
|
|
|
|
1995-09-01 11:54:11 +00:00
|
|
|
DEBUG=0
|
|
|
|
|
1995-07-29 13:48:41 +00:00
|
|
|
import sys
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
|
|
|
|
import string
|
|
|
|
import os
|
|
|
|
import marshal
|
|
|
|
import imp
|
|
|
|
import macfs
|
1995-08-31 13:48:43 +00:00
|
|
|
import MACFS
|
1995-07-29 13:48:41 +00:00
|
|
|
import MacOS
|
|
|
|
from Res import *
|
1996-09-15 22:13:59 +00:00
|
|
|
import macostools
|
1997-05-13 15:42:26 +00:00
|
|
|
import EasyDialogs
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
# .pyc file (and 'PYC ' resource magic number)
|
|
|
|
MAGIC = imp.get_magic()
|
|
|
|
|
|
|
|
# Template file (searched on sys.path)
|
|
|
|
TEMPLATE = "PythonApplet"
|
|
|
|
|
|
|
|
# Specification of our resource
|
|
|
|
RESTYPE = 'PYC '
|
|
|
|
RESNAME = '__main__'
|
|
|
|
|
|
|
|
# A resource with this name sets the "owner" (creator) of the destination
|
1995-09-24 21:05:24 +00:00
|
|
|
# XXXX Should look for id=0
|
1995-07-29 13:48:41 +00:00
|
|
|
OWNERNAME = "owner resource"
|
|
|
|
|
|
|
|
# OpenResFile mode parameters
|
|
|
|
READ = 1
|
|
|
|
WRITE = 2
|
|
|
|
|
1995-09-01 11:54:11 +00:00
|
|
|
def findtemplate():
|
|
|
|
"""Locate the applet template along sys.path"""
|
1995-07-29 13:48:41 +00:00
|
|
|
for p in sys.path:
|
|
|
|
template = os.path.join(p, TEMPLATE)
|
|
|
|
try:
|
1995-08-31 13:48:43 +00:00
|
|
|
template, d1, d2 = macfs.ResolveAliasFile(template)
|
1995-07-29 13:48:41 +00:00
|
|
|
break
|
1995-08-31 13:48:43 +00:00
|
|
|
except (macfs.error, ValueError):
|
1995-07-29 13:48:41 +00:00
|
|
|
continue
|
|
|
|
else:
|
1995-08-31 13:48:43 +00:00
|
|
|
die("Template %s not found on sys.path" % `TEMPLATE`)
|
1995-07-29 13:48:41 +00:00
|
|
|
return
|
1995-08-31 13:48:43 +00:00
|
|
|
template = template.as_pathname()
|
1995-09-01 11:54:11 +00:00
|
|
|
return template
|
|
|
|
|
|
|
|
def main():
|
1997-05-13 15:42:26 +00:00
|
|
|
global DEBUG
|
|
|
|
DEBUG=1
|
1995-09-01 11:54:11 +00:00
|
|
|
|
|
|
|
# Find the template
|
|
|
|
# (there's no point in proceeding if we can't find it)
|
|
|
|
|
|
|
|
template = findtemplate()
|
1995-08-31 13:48:43 +00:00
|
|
|
|
1995-07-29 13:48:41 +00:00
|
|
|
# Ask for source text if not specified in sys.argv[1:]
|
|
|
|
|
|
|
|
if not sys.argv[1:]:
|
1998-02-20 16:06:56 +00:00
|
|
|
srcfss, ok = macfs.PromptGetFile('Select Python source or applet:', 'TEXT', 'APPL')
|
1995-07-29 13:48:41 +00:00
|
|
|
if not ok:
|
|
|
|
return
|
|
|
|
filename = srcfss.as_pathname()
|
|
|
|
tp, tf = os.path.split(filename)
|
|
|
|
if tf[-3:] == '.py':
|
|
|
|
tf = tf[:-3]
|
|
|
|
else:
|
1998-02-20 16:06:56 +00:00
|
|
|
tf = tf + '.out'
|
1995-07-29 13:48:41 +00:00
|
|
|
dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
|
|
|
|
if not ok: return
|
1998-02-20 16:06:56 +00:00
|
|
|
dstfilename = dstfss.as_pathname()
|
|
|
|
cr, tp = MacOS.GetCreatorAndType(filename)
|
|
|
|
if tp == 'APPL':
|
|
|
|
update(template, filename, dstfilename)
|
|
|
|
else:
|
|
|
|
process(template, filename, dstfilename)
|
1995-07-29 13:48:41 +00:00
|
|
|
else:
|
|
|
|
|
|
|
|
# Loop over all files to be processed
|
|
|
|
for filename in sys.argv[1:]:
|
1998-02-20 16:06:56 +00:00
|
|
|
cr, tp = MacOS.GetCreatorAndType(filename)
|
|
|
|
if tp == 'APPL':
|
|
|
|
update(template, filename, '')
|
|
|
|
else:
|
|
|
|
process(template, filename, '')
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
def process(template, filename, output):
|
|
|
|
|
1995-09-01 11:54:11 +00:00
|
|
|
if DEBUG:
|
1997-05-13 15:42:26 +00:00
|
|
|
progress = EasyDialogs.ProgressBar("Processing %s..."%os.path.split(filename)[1], 120)
|
|
|
|
progress.label("Compiling...")
|
|
|
|
else:
|
|
|
|
progress = None
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
# Read the source and compile it
|
|
|
|
# (there's no point overwriting the destination if it has a syntax error)
|
|
|
|
|
|
|
|
fp = open(filename)
|
|
|
|
text = fp.read()
|
|
|
|
fp.close()
|
|
|
|
try:
|
|
|
|
code = compile(text, filename, "exec")
|
|
|
|
except (SyntaxError, EOFError):
|
|
|
|
die("Syntax error in script %s" % `filename`)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Set the destination file name
|
|
|
|
|
|
|
|
if string.lower(filename[-3:]) == ".py":
|
|
|
|
destname = filename[:-3]
|
|
|
|
rsrcname = destname + '.rsrc'
|
|
|
|
else:
|
|
|
|
destname = filename + ".applet"
|
|
|
|
rsrcname = filename + '.rsrc'
|
|
|
|
|
|
|
|
if output:
|
|
|
|
destname = output
|
1998-02-20 16:06:56 +00:00
|
|
|
|
|
|
|
process_common(template, progress, code, rsrcname, destname, 0)
|
|
|
|
|
|
|
|
def update(template, filename, output):
|
|
|
|
if DEBUG:
|
|
|
|
progress = EasyDialogs.ProgressBar("Updating %s..."%os.path.split(filename)[1], 120)
|
|
|
|
else:
|
|
|
|
progress = None
|
|
|
|
if not output:
|
|
|
|
output = filename + ' (updated)'
|
|
|
|
process_common(template, progress, None, filename, output, 1)
|
|
|
|
|
1995-09-24 21:05:24 +00:00
|
|
|
|
1998-02-20 16:06:56 +00:00
|
|
|
def process_common(template, progress, code, rsrcname, destname, is_update):
|
1995-09-24 21:05:24 +00:00
|
|
|
# Try removing the output file
|
|
|
|
try:
|
1998-02-20 16:06:56 +00:00
|
|
|
os.unlink(destname)
|
1995-09-24 21:05:24 +00:00
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Create FSSpecs for the various files
|
|
|
|
|
1995-08-31 13:48:43 +00:00
|
|
|
template_fss = macfs.FSSpec(template)
|
|
|
|
template_fss, d1, d2 = macfs.ResolveAliasFile(template_fss)
|
|
|
|
dest_fss = macfs.FSSpec(destname)
|
|
|
|
|
1995-09-24 21:05:24 +00:00
|
|
|
# Copy data (not resources, yet) from the template
|
1997-05-13 15:42:26 +00:00
|
|
|
if DEBUG:
|
|
|
|
progress.label("Copy data fork...")
|
|
|
|
progress.set(10)
|
1995-09-24 21:05:24 +00:00
|
|
|
|
1995-07-29 13:48:41 +00:00
|
|
|
tmpl = open(template, "rb")
|
|
|
|
dest = open(destname, "wb")
|
|
|
|
data = tmpl.read()
|
|
|
|
if data:
|
|
|
|
dest.write(data)
|
|
|
|
dest.close()
|
|
|
|
tmpl.close()
|
1998-04-21 15:25:57 +00:00
|
|
|
del dest
|
|
|
|
del tmpl
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
# Open the output resource fork
|
|
|
|
|
1997-05-13 15:42:26 +00:00
|
|
|
if DEBUG:
|
|
|
|
progress.label("Copy resources...")
|
|
|
|
progress.set(20)
|
1995-07-29 13:48:41 +00:00
|
|
|
try:
|
1995-08-31 13:48:43 +00:00
|
|
|
output = FSpOpenResFile(dest_fss, WRITE)
|
1995-07-29 13:48:41 +00:00
|
|
|
except MacOS.Error:
|
|
|
|
CreateResFile(destname)
|
1995-08-31 13:48:43 +00:00
|
|
|
output = FSpOpenResFile(dest_fss, WRITE)
|
1995-09-24 21:05:24 +00:00
|
|
|
|
1995-07-29 13:48:41 +00:00
|
|
|
# Copy the resources from the target specific resource template, if any
|
1995-09-24 21:05:24 +00:00
|
|
|
typesfound, ownertype = [], None
|
1995-07-29 13:48:41 +00:00
|
|
|
try:
|
|
|
|
input = FSpOpenResFile(rsrcname, READ)
|
1995-09-01 11:54:11 +00:00
|
|
|
except (MacOS.Error, ValueError):
|
1995-07-29 13:48:41 +00:00
|
|
|
pass
|
1997-05-13 15:42:26 +00:00
|
|
|
if DEBUG:
|
|
|
|
progress.inc(50)
|
1995-07-29 13:48:41 +00:00
|
|
|
else:
|
1998-02-20 16:06:56 +00:00
|
|
|
if is_update:
|
|
|
|
skip_oldfile = ['cfrg']
|
|
|
|
else:
|
|
|
|
skip_oldfile = []
|
|
|
|
typesfound, ownertype = copyres(input, output, skip_oldfile, 0, progress)
|
1995-07-29 13:48:41 +00:00
|
|
|
CloseResFile(input)
|
1995-09-24 21:05:24 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
input = FSpOpenResFile(template_fss, READ)
|
1997-05-13 15:42:26 +00:00
|
|
|
dummy, tmplowner = copyres(input, output, skiptypes, skipowner, progress)
|
1995-09-24 21:05:24 +00:00
|
|
|
if ownertype == None:
|
|
|
|
ownertype = tmplowner
|
|
|
|
CloseResFile(input)
|
|
|
|
if ownertype == None:
|
|
|
|
die("No owner resource found in either resource file or template")
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
# Make sure we're manipulating the output resource file now
|
|
|
|
|
|
|
|
UseResFile(output)
|
|
|
|
|
1998-02-20 16:06:56 +00:00
|
|
|
if code:
|
|
|
|
# Delete any existing 'PYC ' resource named __main__
|
|
|
|
|
|
|
|
try:
|
|
|
|
res = Get1NamedResource(RESTYPE, RESNAME)
|
|
|
|
res.RemoveResource()
|
|
|
|
except Error:
|
|
|
|
pass
|
1995-07-29 13:48:41 +00:00
|
|
|
|
1998-02-20 16:06:56 +00:00
|
|
|
# Create the raw data for the resource from the code object
|
|
|
|
if DEBUG:
|
|
|
|
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 = Unique1ID(RESTYPE)
|
|
|
|
res = Resource(data)
|
|
|
|
res.AddResource(RESTYPE, id, RESNAME)
|
|
|
|
res.WriteResource()
|
|
|
|
res.ReleaseResource()
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
# Close the output file
|
|
|
|
|
|
|
|
CloseResFile(output)
|
|
|
|
|
1998-04-21 15:25:57 +00:00
|
|
|
# Now set the creator, type and bundle bit of the destination
|
|
|
|
dest_finfo = dest_fss.GetFInfo()
|
|
|
|
dest_finfo.Creator = ownertype
|
|
|
|
dest_finfo.Type = 'APPL'
|
|
|
|
dest_finfo.Flags = dest_finfo.Flags | MACFS.kHasBundle
|
|
|
|
dest_finfo.Flags = dest_finfo.Flags & ~MACFS.kHasBeenInited
|
|
|
|
dest_fss.SetFInfo(dest_finfo)
|
|
|
|
|
1996-09-15 22:13:59 +00:00
|
|
|
macostools.touched(dest_fss)
|
1996-08-28 14:16:39 +00:00
|
|
|
if DEBUG:
|
1997-05-13 15:42:26 +00:00
|
|
|
progress.label("Done.")
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Copy resources between two resource file descriptors.
|
1995-09-24 21:05:24 +00:00
|
|
|
# skip a resource named '__main__' or (if skipowner is set) 'Owner resource'.
|
|
|
|
# Also skip resources with a type listed in skiptypes.
|
|
|
|
#
|
1997-05-13 15:42:26 +00:00
|
|
|
def copyres(input, output, skiptypes, skipowner, progress=None):
|
1995-07-29 13:48:41 +00:00
|
|
|
ctor = None
|
1995-09-24 21:05:24 +00:00
|
|
|
alltypes = []
|
1995-07-29 13:48:41 +00:00
|
|
|
UseResFile(input)
|
|
|
|
ntypes = Count1Types()
|
1997-05-13 15:42:26 +00:00
|
|
|
progress_type_inc = 50/ntypes
|
1995-07-29 13:48:41 +00:00
|
|
|
for itype in range(1, 1+ntypes):
|
|
|
|
type = Get1IndType(itype)
|
1995-09-24 21:05:24 +00:00
|
|
|
if type in skiptypes:
|
|
|
|
continue
|
|
|
|
alltypes.append(type)
|
1995-07-29 13:48:41 +00:00
|
|
|
nresources = Count1Resources(type)
|
1997-05-13 15:42:26 +00:00
|
|
|
progress_cur_inc = progress_type_inc/nresources
|
1995-07-29 13:48:41 +00:00
|
|
|
for ires in range(1, 1+nresources):
|
|
|
|
res = Get1IndResource(type, ires)
|
|
|
|
id, type, name = res.GetResInfo()
|
|
|
|
lcname = string.lower(name)
|
1998-02-20 16:06:56 +00:00
|
|
|
## if (type, lcname) == (RESTYPE, RESNAME):
|
|
|
|
## continue # Don't copy __main__ from template
|
1995-09-24 21:05:24 +00:00
|
|
|
# XXXX should look for id=0
|
|
|
|
if lcname == OWNERNAME:
|
|
|
|
if skipowner:
|
|
|
|
continue # Skip this one
|
|
|
|
else:
|
|
|
|
ctor = type
|
1995-07-29 13:48:41 +00:00
|
|
|
size = res.size
|
|
|
|
attrs = res.GetResAttrs()
|
1997-05-13 15:42:26 +00:00
|
|
|
if DEBUG and progress:
|
|
|
|
progress.label("Copy %s %d %s"%(type, id, name))
|
|
|
|
progress.inc(progress_cur_inc)
|
1995-07-29 13:48:41 +00:00
|
|
|
res.LoadResource()
|
|
|
|
res.DetachResource()
|
|
|
|
UseResFile(output)
|
|
|
|
try:
|
|
|
|
res2 = Get1Resource(type, id)
|
|
|
|
except MacOS.Error:
|
|
|
|
res2 = None
|
|
|
|
if res2:
|
1997-05-13 15:42:26 +00:00
|
|
|
if DEBUG and progress:
|
|
|
|
progress.label("Overwrite %s %d %s"%(type, id, name))
|
1995-07-29 13:48:41 +00:00
|
|
|
res2.RemoveResource()
|
|
|
|
res.AddResource(type, id, name)
|
|
|
|
res.WriteResource()
|
|
|
|
attrs = attrs | res.GetResAttrs()
|
|
|
|
res.SetResAttrs(attrs)
|
|
|
|
UseResFile(input)
|
1995-09-24 21:05:24 +00:00
|
|
|
return alltypes, ctor
|
1995-07-29 13:48:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Show a message and exit
|
|
|
|
|
|
|
|
def die(str):
|
|
|
|
message(str)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
# Show a message
|
|
|
|
|
|
|
|
def message(str, id = 256):
|
|
|
|
from Dlg import *
|
|
|
|
d = GetNewDialog(id, -1)
|
|
|
|
if not d:
|
|
|
|
print "Error:", `str`
|
|
|
|
print "DLOG id =", id, "not found."
|
|
|
|
return
|
|
|
|
tp, h, rect = d.GetDialogItem(2)
|
|
|
|
SetDialogItemText(h, str)
|
1996-01-22 14:55:52 +00:00
|
|
|
d.SetDialogDefaultItem(1)
|
1995-07-29 13:48:41 +00:00
|
|
|
while 1:
|
|
|
|
n = ModalDialog(None)
|
|
|
|
if n == 1: break
|
|
|
|
del d
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|