1995-07-29 10:48:41 -03: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 08:54:11 -03:00
|
|
|
|
1995-07-29 10:48:41 -03:00
|
|
|
import sys
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
|
|
|
|
import os
|
|
|
|
import MacOS
|
1997-05-13 12:42:26 -03:00
|
|
|
import EasyDialogs
|
1998-07-31 06:44:23 -03:00
|
|
|
import buildtools
|
2002-06-09 19:08:52 -03:00
|
|
|
import getopt
|
1995-07-29 10:48:41 -03:00
|
|
|
|
1998-07-31 06:44:23 -03:00
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
buildapplet()
|
|
|
|
except buildtools.BuildError, detail:
|
|
|
|
EasyDialogs.Message(detail)
|
1995-07-29 10:48:41 -03:00
|
|
|
|
1995-09-01 08:54:11 -03:00
|
|
|
|
1998-07-31 06:44:23 -03:00
|
|
|
def buildapplet():
|
|
|
|
buildtools.DEBUG=1
|
1995-09-01 08:54:11 -03:00
|
|
|
|
|
|
|
# Find the template
|
|
|
|
# (there's no point in proceeding if we can't find it)
|
|
|
|
|
1998-07-31 06:44:23 -03:00
|
|
|
template = buildtools.findtemplate()
|
|
|
|
|
1995-07-29 10:48:41 -03:00
|
|
|
# Ask for source text if not specified in sys.argv[1:]
|
|
|
|
|
|
|
|
if not sys.argv[1:]:
|
2003-01-22 10:03:12 -04:00
|
|
|
filename = EasyDialogs.AskFileForOpen(message='Select Python source or applet:',
|
2003-01-26 16:35:47 -04:00
|
|
|
typeList=('TEXT', 'APPL'))
|
2003-01-22 10:03:12 -04:00
|
|
|
if not filename:
|
1995-07-29 10:48:41 -03:00
|
|
|
return
|
|
|
|
tp, tf = os.path.split(filename)
|
|
|
|
if tf[-3:] == '.py':
|
|
|
|
tf = tf[:-3]
|
|
|
|
else:
|
1998-07-31 06:44:23 -03:00
|
|
|
tf = tf + '.applet'
|
2003-01-22 10:03:12 -04:00
|
|
|
dstfilename = EasyDialogs.AskFileForSave(message='Save application as:',
|
|
|
|
savedFileName=tf)
|
|
|
|
if not dstfilename: return
|
1998-02-20 12:06:56 -04:00
|
|
|
cr, tp = MacOS.GetCreatorAndType(filename)
|
|
|
|
if tp == 'APPL':
|
1998-07-31 06:44:23 -03:00
|
|
|
buildtools.update(template, filename, dstfilename)
|
1998-02-20 12:06:56 -04:00
|
|
|
else:
|
1998-07-31 06:44:23 -03:00
|
|
|
buildtools.process(template, filename, dstfilename, 1)
|
1995-07-29 10:48:41 -03:00
|
|
|
else:
|
|
|
|
|
2003-05-25 19:00:17 -03:00
|
|
|
SHORTOPTS = "o:r:ne:v?P"
|
|
|
|
LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help", "python=")
|
2002-06-09 19:08:52 -03:00
|
|
|
try:
|
|
|
|
options, args = getopt.getopt(sys.argv[1:], SHORTOPTS, LONGOPTS)
|
|
|
|
except getopt.error:
|
|
|
|
usage()
|
|
|
|
if options and len(args) > 1:
|
|
|
|
sys.stderr.write("Cannot use options when specifying multiple input files")
|
|
|
|
sys.exit(1)
|
|
|
|
dstfilename = None
|
|
|
|
rsrcfilename = None
|
|
|
|
raw = 0
|
|
|
|
extras = []
|
|
|
|
verbose = None
|
|
|
|
for opt, arg in options:
|
|
|
|
if opt in ('-o', '--output'):
|
|
|
|
dstfilename = arg
|
|
|
|
elif opt in ('-r', '--resource'):
|
|
|
|
rsrcfilename = arg
|
|
|
|
elif opt in ('-n', '--noargv'):
|
|
|
|
raw = 1
|
|
|
|
elif opt in ('-e', '--extra'):
|
2003-02-24 12:28:37 -04:00
|
|
|
if ':' in arg:
|
|
|
|
arg = arg.split(':')
|
2002-06-09 19:08:52 -03:00
|
|
|
extras.append(arg)
|
2003-05-25 19:00:17 -03:00
|
|
|
elif opt in ('-P', '--python'):
|
|
|
|
# This is a very dirty trick. We set sys.executable
|
|
|
|
# so that bundlebuilder will use this in the #! line
|
|
|
|
# for the applet bootstrap.
|
|
|
|
sys.executable = arg
|
2002-06-09 19:08:52 -03:00
|
|
|
elif opt in ('-v', '--verbose'):
|
|
|
|
verbose = Verbose()
|
|
|
|
elif opt in ('-?', '--help'):
|
|
|
|
usage()
|
2002-08-22 20:36:11 -03:00
|
|
|
# On OS9 always be verbose
|
|
|
|
if sys.platform == 'mac' and not verbose:
|
|
|
|
verbose = 'default'
|
1995-07-29 10:48:41 -03:00
|
|
|
# Loop over all files to be processed
|
2002-06-09 19:08:52 -03:00
|
|
|
for filename in args:
|
2003-01-26 16:35:47 -04:00
|
|
|
cr, tp = MacOS.GetCreatorAndType(filename)
|
1998-02-20 12:06:56 -04:00
|
|
|
if tp == 'APPL':
|
2002-06-09 19:08:52 -03:00
|
|
|
buildtools.update(template, filename, dstfilename)
|
1998-02-20 12:06:56 -04:00
|
|
|
else:
|
2002-06-09 19:08:52 -03:00
|
|
|
buildtools.process(template, filename, dstfilename, 1,
|
|
|
|
rsrcname=rsrcfilename, others=extras, raw=raw, progress=verbose)
|
|
|
|
|
|
|
|
def usage():
|
|
|
|
print "BuildApplet creates an application from a Python source file"
|
|
|
|
print "Usage:"
|
|
|
|
print " BuildApplet interactive, single file, no options"
|
|
|
|
print " BuildApplet src1.py src2.py ... non-interactive multiple file"
|
|
|
|
print " BuildApplet [options] src.py non-interactive single file"
|
|
|
|
print "Options:"
|
2003-02-24 12:28:37 -04:00
|
|
|
print " --output o Output file; default based on source filename, short -o"
|
|
|
|
print " --resource r Resource file; default based on source filename, short -r"
|
|
|
|
print " --noargv Build applet without drag-and-drop sys.argv emulation, short -n, OSX only"
|
|
|
|
print " --extra src[:dst] Extra file to put in .app bundle, short -e, OSX only"
|
|
|
|
print " --verbose Verbose, short -v"
|
|
|
|
print " --help This message, short -?"
|
2002-06-09 19:08:52 -03:00
|
|
|
sys.exit(1)
|
1995-07-29 10:48:41 -03:00
|
|
|
|
2002-06-09 19:08:52 -03:00
|
|
|
class Verbose:
|
|
|
|
"""This class mimics EasyDialogs.ProgressBar but prints to stderr"""
|
|
|
|
def __init__(self, *args):
|
|
|
|
if args and args[0]:
|
|
|
|
self.label(args[0])
|
|
|
|
|
|
|
|
def set(self, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def inc(self, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def label(self, str):
|
|
|
|
sys.stderr.write(str+'\n')
|
1995-07-29 10:48:41 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|