| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | import sys | 
					
						
							|  |  |  | sys.stdout = sys.stderr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import MacOS | 
					
						
							| 
									
										
										
										
											1997-05-13 15:42:26 +00:00
										 |  |  | import EasyDialogs | 
					
						
							| 
									
										
										
										
											1998-07-31 09:44:23 +00:00
										 |  |  | import buildtools | 
					
						
							| 
									
										
										
										
											2002-06-09 22:08:52 +00:00
										 |  |  | import getopt | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-31 09:44:23 +00:00
										 |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         buildapplet() | 
					
						
							|  |  |  |     except buildtools.BuildError, detail: | 
					
						
							|  |  |  |         EasyDialogs.Message(detail) | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-09-01 11:54:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-31 09:44:23 +00:00
										 |  |  | def buildapplet(): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     buildtools.DEBUG=1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Find the template | 
					
						
							|  |  |  |     # (there's no point in proceeding if we can't find it) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template = buildtools.findtemplate() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Ask for source text if not specified in sys.argv[1:] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not sys.argv[1:]: | 
					
						
							|  |  |  |         filename = EasyDialogs.AskFileForOpen(message='Select Python source or applet:', | 
					
						
							|  |  |  |                 typeList=('TEXT', 'APPL')) | 
					
						
							|  |  |  |         if not filename: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         tp, tf = os.path.split(filename) | 
					
						
							|  |  |  |         if tf[-3:] == '.py': | 
					
						
							|  |  |  |             tf = tf[:-3] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             tf = tf + '.applet' | 
					
						
							|  |  |  |         dstfilename = EasyDialogs.AskFileForSave(message='Save application as:', | 
					
						
							|  |  |  |                 savedFileName=tf) | 
					
						
							|  |  |  |         if not dstfilename: return | 
					
						
							|  |  |  |         cr, tp = MacOS.GetCreatorAndType(filename) | 
					
						
							|  |  |  |         if tp == 'APPL': | 
					
						
							|  |  |  |             buildtools.update(template, filename, dstfilename) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             buildtools.process(template, filename, dstfilename, 1) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-27 15:51:03 +00:00
										 |  |  |         SHORTOPTS = "o:r:ne:v?PR" | 
					
						
							|  |  |  |         LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help", "python=", "destroot=") | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00: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 | 
					
						
							| 
									
										
										
										
											2004-12-27 15:51:03 +00:00
										 |  |  |         destroot = '' | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         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'): | 
					
						
							|  |  |  |                 if ':' in arg: | 
					
						
							|  |  |  |                     arg = arg.split(':') | 
					
						
							|  |  |  |                 extras.append(arg) | 
					
						
							|  |  |  |             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 | 
					
						
							|  |  |  |             elif opt in ('-v', '--verbose'): | 
					
						
							|  |  |  |                 verbose = Verbose() | 
					
						
							|  |  |  |             elif opt in ('-?', '--help'): | 
					
						
							|  |  |  |                 usage() | 
					
						
							| 
									
										
										
										
											2004-12-27 15:51:03 +00:00
										 |  |  |             elif opt in ('-d', '--destroot'): | 
					
						
							|  |  |  |                 destroot = arg | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         # On OS9 always be verbose | 
					
						
							|  |  |  |         if sys.platform == 'mac' and not verbose: | 
					
						
							|  |  |  |             verbose = 'default' | 
					
						
							|  |  |  |         # Loop over all files to be processed | 
					
						
							|  |  |  |         for filename in args: | 
					
						
							|  |  |  |             cr, tp = MacOS.GetCreatorAndType(filename) | 
					
						
							|  |  |  |             if tp == 'APPL': | 
					
						
							|  |  |  |                 buildtools.update(template, filename, dstfilename) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 buildtools.process(template, filename, dstfilename, 1, | 
					
						
							| 
									
										
										
										
											2004-12-27 15:51:03 +00:00
										 |  |  |                         rsrcname=rsrcfilename, others=extras, raw=raw, | 
					
						
							|  |  |  |                         progress=verbose, destroot=destroot) | 
					
						
							| 
									
										
										
										
											2002-06-09 22:08:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def usage(): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     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 src[:dst] Extra file to put in .app bundle, short -e, OSX only" | 
					
						
							|  |  |  |     print "  --verbose         Verbose, short -v" | 
					
						
							|  |  |  |     print "  --help            This message, short -?" | 
					
						
							|  |  |  |     sys.exit(1) | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-09 22:08:52 +00:00
										 |  |  | class Verbose: | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     """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 13:48:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     main() |