Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 1 | """Create an applet from a Python script. |
| 2 | |
| 3 | This puts up a dialog asking for a Python source file ('TEXT'). |
| 4 | The output is a file with the same name but its ".py" suffix dropped. |
| 5 | It is created by copying an applet template and then adding a 'PYC ' |
| 6 | resource named __main__ containing the compiled, marshalled script. |
| 7 | """ |
| 8 | |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame] | 9 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 10 | import sys |
| 11 | sys.stdout = sys.stderr |
| 12 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 13 | import os |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 14 | import MacOS |
Jack Jansen | af647dd | 1997-05-13 15:42:26 +0000 | [diff] [blame] | 15 | import EasyDialogs |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 16 | import buildtools |
Jack Jansen | 388fbf3 | 2002-06-09 22:08:52 +0000 | [diff] [blame] | 17 | import getopt |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame] | 18 | |
| 19 | def main(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 20 | try: |
| 21 | buildapplet() |
| 22 | except buildtools.BuildError, detail: |
| 23 | EasyDialogs.Message(detail) |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def buildapplet(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 27 | buildtools.DEBUG=1 |
| 28 | |
| 29 | # Find the template |
| 30 | # (there's no point in proceeding if we can't find it) |
| 31 | |
| 32 | template = buildtools.findtemplate() |
| 33 | |
| 34 | # Ask for source text if not specified in sys.argv[1:] |
| 35 | |
| 36 | if not sys.argv[1:]: |
| 37 | filename = EasyDialogs.AskFileForOpen(message='Select Python source or applet:', |
| 38 | typeList=('TEXT', 'APPL')) |
| 39 | if not filename: |
| 40 | return |
| 41 | tp, tf = os.path.split(filename) |
| 42 | if tf[-3:] == '.py': |
| 43 | tf = tf[:-3] |
| 44 | else: |
| 45 | tf = tf + '.applet' |
| 46 | dstfilename = EasyDialogs.AskFileForSave(message='Save application as:', |
| 47 | savedFileName=tf) |
| 48 | if not dstfilename: return |
| 49 | cr, tp = MacOS.GetCreatorAndType(filename) |
| 50 | if tp == 'APPL': |
| 51 | buildtools.update(template, filename, dstfilename) |
| 52 | else: |
| 53 | buildtools.process(template, filename, dstfilename, 1) |
| 54 | else: |
| 55 | |
| 56 | SHORTOPTS = "o:r:ne:v?P" |
| 57 | LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help", "python=") |
| 58 | try: |
| 59 | options, args = getopt.getopt(sys.argv[1:], SHORTOPTS, LONGOPTS) |
| 60 | except getopt.error: |
| 61 | usage() |
| 62 | if options and len(args) > 1: |
| 63 | sys.stderr.write("Cannot use options when specifying multiple input files") |
| 64 | sys.exit(1) |
| 65 | dstfilename = None |
| 66 | rsrcfilename = None |
| 67 | raw = 0 |
| 68 | extras = [] |
| 69 | verbose = None |
| 70 | for opt, arg in options: |
| 71 | if opt in ('-o', '--output'): |
| 72 | dstfilename = arg |
| 73 | elif opt in ('-r', '--resource'): |
| 74 | rsrcfilename = arg |
| 75 | elif opt in ('-n', '--noargv'): |
| 76 | raw = 1 |
| 77 | elif opt in ('-e', '--extra'): |
| 78 | if ':' in arg: |
| 79 | arg = arg.split(':') |
| 80 | extras.append(arg) |
| 81 | elif opt in ('-P', '--python'): |
| 82 | # This is a very dirty trick. We set sys.executable |
| 83 | # so that bundlebuilder will use this in the #! line |
| 84 | # for the applet bootstrap. |
| 85 | sys.executable = arg |
| 86 | elif opt in ('-v', '--verbose'): |
| 87 | verbose = Verbose() |
| 88 | elif opt in ('-?', '--help'): |
| 89 | usage() |
| 90 | # On OS9 always be verbose |
| 91 | if sys.platform == 'mac' and not verbose: |
| 92 | verbose = 'default' |
| 93 | # Loop over all files to be processed |
| 94 | for filename in args: |
| 95 | cr, tp = MacOS.GetCreatorAndType(filename) |
| 96 | if tp == 'APPL': |
| 97 | buildtools.update(template, filename, dstfilename) |
| 98 | else: |
| 99 | buildtools.process(template, filename, dstfilename, 1, |
| 100 | rsrcname=rsrcfilename, others=extras, raw=raw, progress=verbose) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 101 | |
Jack Jansen | 388fbf3 | 2002-06-09 22:08:52 +0000 | [diff] [blame] | 102 | def usage(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 103 | print "BuildApplet creates an application from a Python source file" |
| 104 | print "Usage:" |
| 105 | print " BuildApplet interactive, single file, no options" |
| 106 | print " BuildApplet src1.py src2.py ... non-interactive multiple file" |
| 107 | print " BuildApplet [options] src.py non-interactive single file" |
| 108 | print "Options:" |
| 109 | print " --output o Output file; default based on source filename, short -o" |
| 110 | print " --resource r Resource file; default based on source filename, short -r" |
| 111 | print " --noargv Build applet without drag-and-drop sys.argv emulation, short -n, OSX only" |
| 112 | print " --extra src[:dst] Extra file to put in .app bundle, short -e, OSX only" |
| 113 | print " --verbose Verbose, short -v" |
| 114 | print " --help This message, short -?" |
| 115 | sys.exit(1) |
Jack Jansen | 388fbf3 | 2002-06-09 22:08:52 +0000 | [diff] [blame] | 116 | |
| 117 | class Verbose: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 118 | """This class mimics EasyDialogs.ProgressBar but prints to stderr""" |
| 119 | def __init__(self, *args): |
| 120 | if args and args[0]: |
| 121 | self.label(args[0]) |
| 122 | |
| 123 | def set(self, *args): |
| 124 | pass |
| 125 | |
| 126 | def inc(self, *args): |
| 127 | pass |
| 128 | |
| 129 | def label(self, str): |
| 130 | sys.stderr.write(str+'\n') |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 131 | |
| 132 | if __name__ == '__main__': |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 133 | main() |