BuildApplet.py 3.55 KB
Newer Older
1 2 3 4 5 6 7 8
"""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.
"""

9

10 11 12 13 14 15
import sys
sys.stdout = sys.stderr

import os
import macfs
import MacOS
16
import EasyDialogs
17
import buildtools
18
import getopt
19

20 21 22 23 24
def main():
	try:
		buildapplet()
	except buildtools.BuildError, detail:
		EasyDialogs.Message(detail)
25

26

27 28
def buildapplet():
	buildtools.DEBUG=1
29 30 31 32
	
	# Find the template
	# (there's no point in proceeding if we can't find it)
	
33 34
	template = buildtools.findtemplate()
	
35 36 37
	# Ask for source text if not specified in sys.argv[1:]
	
	if not sys.argv[1:]:
38
		srcfss, ok = macfs.PromptGetFile('Select Python source or applet:', 'TEXT', 'APPL')
39 40 41 42 43 44 45
		if not ok:
			return
		filename = srcfss.as_pathname()
		tp, tf = os.path.split(filename)
		if tf[-3:] == '.py':
			tf = tf[:-3]
		else:
46
			tf = tf + '.applet'
47 48
		dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
		if not ok: return
49 50 51
		dstfilename = dstfss.as_pathname()
		cr, tp = MacOS.GetCreatorAndType(filename)
		if tp == 'APPL':
52
			buildtools.update(template, filename, dstfilename)
53
		else:
54
			buildtools.process(template, filename, dstfilename, 1)
55 56
	else:
		
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		SHORTOPTS = "o:r:ne:v?"
		LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help")
		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'):
				extras.append(arg)
			elif opt in ('-v', '--verbose'):
				verbose = Verbose()
			elif opt in ('-?', '--help'):
				usage()
84 85 86
		# On OS9 always be verbose
		if sys.platform == 'mac' and not verbose:
			verbose = 'default'
87
		# Loop over all files to be processed
88
		for filename in args:
89 90
			cr, tp = MacOS.GetCreatorAndType(filename)
			if tp == 'APPL':
91
				buildtools.update(template, filename, dstfilename)
92
			else:
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
				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:"
	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 f     Extra file to put in .app bundle, short -e, OSX only"
	print "  --verbose     Verbose, short -v"
	print "  --help        This message, short -?"
	sys.exit(1)
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124
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')
125 126 127

if __name__ == '__main__':
	main()