1995-02-18 11:05:39 -04:00
|
|
|
"""Create an applet from a Python script.
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
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-02-17 10:28:39 -04:00
|
|
|
"""
|
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
import sys
|
1995-02-19 11:49:17 -04:00
|
|
|
sys.stdout = sys.stderr
|
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
import string
|
|
|
|
import os
|
1995-02-17 10:28:39 -04:00
|
|
|
import marshal
|
|
|
|
import imp
|
|
|
|
import macfs
|
1995-02-18 11:05:39 -04:00
|
|
|
import MacOS
|
1995-02-17 10:28:39 -04:00
|
|
|
from Res import *
|
|
|
|
|
|
|
|
# .pyc file (and 'PYC ' resource magic number)
|
|
|
|
MAGIC = imp.get_magic()
|
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
# Template file (searched on sys.path)
|
|
|
|
TEMPLATE = "PythonApplet"
|
|
|
|
|
|
|
|
# Specification of our resource
|
1995-02-17 10:28:39 -04:00
|
|
|
RESTYPE = 'PYC '
|
|
|
|
RESNAME = '__main__'
|
|
|
|
|
1995-02-26 06:19:42 -04:00
|
|
|
# A resource with this name sets the "owner" (creator) of the destination
|
|
|
|
OWNERNAME = "owner resource"
|
|
|
|
|
1995-02-17 10:28:39 -04:00
|
|
|
# OpenResFile mode parameters
|
|
|
|
READ = 1
|
|
|
|
WRITE = 2
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
# Find the template
|
|
|
|
# (there's no point in proceeding if we can't find it)
|
|
|
|
|
|
|
|
for p in sys.path:
|
|
|
|
template = os.path.join(p, TEMPLATE)
|
|
|
|
try:
|
|
|
|
tmpl = open(template, "rb")
|
1995-02-19 11:49:17 -04:00
|
|
|
tmpl.close()
|
1995-02-18 11:05:39 -04:00
|
|
|
break
|
|
|
|
except IOError:
|
|
|
|
continue
|
|
|
|
else:
|
1995-02-20 19:44:14 -04:00
|
|
|
die("Template %s not found" % `template`)
|
1995-02-18 11:05:39 -04:00
|
|
|
return
|
|
|
|
|
1995-02-19 11:49:17 -04:00
|
|
|
# Ask for source text if not specified in sys.argv[1:]
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-19 11:49:17 -04:00
|
|
|
if not sys.argv[1:]:
|
|
|
|
srcfss, ok = macfs.StandardGetFile('TEXT')
|
|
|
|
if not ok:
|
|
|
|
return
|
|
|
|
filename = srcfss.as_pathname()
|
1995-02-27 12:19:07 -04:00
|
|
|
tp, tf = os.path.split(filename)
|
|
|
|
if tf[-3:] == '.py':
|
|
|
|
tf = tf[:-3]
|
|
|
|
else:
|
|
|
|
tf = tf + '.applet'
|
|
|
|
dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
|
|
|
|
if not ok: return
|
|
|
|
process(template, filename, dstfss.as_pathname())
|
|
|
|
else:
|
|
|
|
|
|
|
|
# Loop over all files to be processed
|
|
|
|
for filename in sys.argv[1:]:
|
|
|
|
process(template, filename, '')
|
1995-02-19 11:49:17 -04:00
|
|
|
|
1995-02-24 18:46:51 -04:00
|
|
|
undefs = ('????', ' ', '\0\0\0\0', 'BINA')
|
1995-02-19 11:49:17 -04:00
|
|
|
|
1995-02-27 12:19:07 -04:00
|
|
|
def process(template, filename, output):
|
1995-02-19 11:49:17 -04:00
|
|
|
|
|
|
|
print "Processing", `filename`, "..."
|
1995-02-17 10:28:39 -04:00
|
|
|
|
|
|
|
# Read the source and compile it
|
1995-02-18 11:05:39 -04:00
|
|
|
# (there's no point overwriting the destination if it has a syntax error)
|
1995-02-17 10:28:39 -04:00
|
|
|
|
|
|
|
fp = open(filename)
|
|
|
|
text = fp.read()
|
|
|
|
fp.close()
|
1995-02-18 11:05:39 -04:00
|
|
|
try:
|
|
|
|
code = compile(text, filename, "exec")
|
|
|
|
except (SyntaxError, EOFError):
|
1995-02-20 19:44:14 -04:00
|
|
|
die("Syntax error in script %s" % `filename`)
|
1995-02-18 11:05:39 -04:00
|
|
|
return
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
# Set the destination file name
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
if string.lower(filename[-3:]) == ".py":
|
|
|
|
destname = filename[:-3]
|
1995-02-20 19:44:14 -04:00
|
|
|
rsrcname = destname + '.rsrc'
|
1995-02-18 11:05:39 -04:00
|
|
|
else:
|
|
|
|
destname = filename + ".applet"
|
1995-02-20 19:44:14 -04:00
|
|
|
rsrcname = filename + '.rsrc'
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-27 12:19:07 -04:00
|
|
|
if output:
|
|
|
|
destname = output
|
1995-02-18 11:05:39 -04:00
|
|
|
# Copy the data from the template (creating the file as well)
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-19 11:49:17 -04:00
|
|
|
tmpl = open(template, "rb")
|
1995-02-18 11:05:39 -04:00
|
|
|
dest = open(destname, "wb")
|
|
|
|
data = tmpl.read()
|
|
|
|
if data:
|
|
|
|
dest.write(data)
|
|
|
|
dest.close()
|
|
|
|
tmpl.close()
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-20 11:49:27 -04:00
|
|
|
# Copy the creator of the template to the destination
|
1995-02-20 19:44:14 -04:00
|
|
|
# unless it already got one. Set type to APPL
|
|
|
|
|
1995-02-19 11:49:17 -04:00
|
|
|
tctor, ttype = MacOS.GetCreatorAndType(template)
|
|
|
|
ctor, type = MacOS.GetCreatorAndType(destname)
|
1995-02-20 11:49:27 -04:00
|
|
|
if type in undefs: type = 'APPL'
|
1995-02-19 11:49:17 -04:00
|
|
|
if ctor in undefs: ctor = tctor
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-20 19:44:14 -04:00
|
|
|
# Open the output resource fork
|
1995-02-17 10:28:39 -04:00
|
|
|
|
|
|
|
try:
|
1995-02-18 11:05:39 -04:00
|
|
|
output = FSpOpenResFile(destname, WRITE)
|
|
|
|
except MacOS.Error:
|
|
|
|
print "Creating resource fork..."
|
|
|
|
CreateResFile(destname)
|
|
|
|
output = FSpOpenResFile(destname, WRITE)
|
|
|
|
|
1995-02-20 19:44:14 -04:00
|
|
|
# Copy the resources from the template
|
1995-02-18 11:05:39 -04:00
|
|
|
|
1995-02-20 19:44:14 -04:00
|
|
|
input = FSpOpenResFile(template, READ)
|
1995-02-26 06:19:42 -04:00
|
|
|
newctor = copyres(input, output)
|
1995-02-18 11:05:39 -04:00
|
|
|
CloseResFile(input)
|
1995-02-26 06:19:42 -04:00
|
|
|
if newctor: ctor = newctor
|
1995-02-18 11:05:39 -04:00
|
|
|
|
1995-02-20 19:44:14 -04:00
|
|
|
# Copy the resources from the target specific resource template, if any
|
|
|
|
|
|
|
|
try:
|
|
|
|
input = FSpOpenResFile(rsrcname, READ)
|
|
|
|
except MacOS.Error:
|
|
|
|
pass
|
|
|
|
else:
|
1995-02-26 06:19:42 -04:00
|
|
|
newctor = copyres(input, output)
|
1995-02-20 19:44:14 -04:00
|
|
|
CloseResFile(input)
|
1995-02-26 06:19:42 -04:00
|
|
|
if newctor: ctor = newctor
|
|
|
|
|
|
|
|
# Now set the creator and type of the destination
|
|
|
|
|
|
|
|
MacOS.SetCreatorAndType(destname, ctor, type)
|
1995-02-20 19:44:14 -04:00
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
# Make sure we're manipulating the output resource file now
|
|
|
|
|
|
|
|
UseResFile(output)
|
|
|
|
|
|
|
|
# Delete any existing 'PYC 'resource named __main__
|
1995-02-17 10:28:39 -04:00
|
|
|
|
|
|
|
try:
|
1995-02-18 11:05:39 -04:00
|
|
|
res = Get1NamedResource(RESTYPE, RESNAME)
|
1995-02-17 10:28:39 -04:00
|
|
|
res.RmveResource()
|
|
|
|
except Error:
|
|
|
|
pass
|
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
# Create the raw data for the resource from the code object
|
|
|
|
|
|
|
|
data = marshal.dumps(code)
|
|
|
|
del code
|
|
|
|
data = (MAGIC + '\0\0\0\0') + data
|
|
|
|
|
1995-02-17 10:28:39 -04:00
|
|
|
# Create the resource and write it
|
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
id = 0
|
|
|
|
while id < 128:
|
|
|
|
id = Unique1ID(RESTYPE)
|
1995-02-17 10:28:39 -04:00
|
|
|
res = Resource(data)
|
1995-02-18 11:05:39 -04:00
|
|
|
res.AddResource(RESTYPE, id, RESNAME)
|
1995-02-17 10:28:39 -04:00
|
|
|
res.WriteResource()
|
|
|
|
res.ReleaseResource()
|
|
|
|
|
1995-02-20 19:44:14 -04:00
|
|
|
# Close the output file
|
1995-02-17 10:28:39 -04:00
|
|
|
|
1995-02-18 11:05:39 -04:00
|
|
|
CloseResFile(output)
|
1995-02-19 11:49:17 -04:00
|
|
|
|
1995-02-20 19:44:14 -04:00
|
|
|
# Give positive feedback
|
|
|
|
|
|
|
|
message("Applet %s created." % `destname`)
|
|
|
|
|
|
|
|
|
1995-02-26 06:19:42 -04:00
|
|
|
# Copy resources between two resource file descriptors.
|
|
|
|
# Exception: don't copy a __main__ resource.
|
|
|
|
# If a resource's name is "owner resource", its type is returned
|
|
|
|
# (so the caller can use it to set the destination's creator)
|
1995-02-20 19:44:14 -04:00
|
|
|
|
|
|
|
def copyres(input, output):
|
1995-02-26 06:19:42 -04:00
|
|
|
ctor = None
|
1995-02-20 19:44:14 -04:00
|
|
|
UseResFile(input)
|
|
|
|
ntypes = Count1Types()
|
|
|
|
for itype in range(1, 1+ntypes):
|
|
|
|
type = Get1IndType(itype)
|
|
|
|
nresources = Count1Resources(type)
|
|
|
|
for ires in range(1, 1+nresources):
|
|
|
|
res = Get1IndResource(type, ires)
|
|
|
|
id, type, name = res.GetResInfo()
|
1995-02-26 06:19:42 -04:00
|
|
|
lcname = string.lower(name)
|
|
|
|
if (type, lcname) == (RESTYPE, RESNAME):
|
1995-02-20 19:44:14 -04:00
|
|
|
continue # Don't copy __main__ from template
|
1995-02-26 06:19:42 -04:00
|
|
|
if lcname == OWNERNAME: ctor = type
|
1995-02-20 19:44:14 -04:00
|
|
|
size = res.SizeResource()
|
|
|
|
attrs = res.GetResAttrs()
|
|
|
|
print id, type, name, size, hex(attrs)
|
|
|
|
res.LoadResource()
|
|
|
|
res.DetachResource()
|
|
|
|
UseResFile(output)
|
|
|
|
try:
|
|
|
|
res2 = Get1Resource(type, id)
|
|
|
|
except MacOS.Error:
|
|
|
|
res2 = None
|
|
|
|
if res2:
|
|
|
|
print "Overwriting..."
|
|
|
|
res2.RmveResource()
|
|
|
|
res.AddResource(type, id, name)
|
|
|
|
res.WriteResource()
|
1995-02-26 06:19:42 -04:00
|
|
|
attrs = attrs | res.GetResAttrs()
|
|
|
|
print "New attrs =", hex(attrs)
|
|
|
|
res.SetResAttrs(attrs)
|
1995-02-20 19:44:14 -04:00
|
|
|
UseResFile(input)
|
1995-02-26 06:19:42 -04:00
|
|
|
return ctor
|
1995-02-20 19:44:14 -04: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.GetDItem(2)
|
|
|
|
SetIText(h, str)
|
|
|
|
while 1:
|
|
|
|
n = ModalDialog(None)
|
|
|
|
if n == 1: break
|
|
|
|
del d
|
1995-02-19 11:49:17 -04:00
|
|
|
|
1995-02-17 10:28:39 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
1995-02-26 06:19:42 -04:00
|
|
|
|