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 macfs |
| 15 | import MacOS |
Jack Jansen | af647dd | 1997-05-13 15:42:26 +0000 | [diff] [blame] | 16 | import EasyDialogs |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 17 | import buildtools |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 18 | |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame] | 19 | |
| 20 | def main(): |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 21 | try: |
| 22 | buildapplet() |
| 23 | except buildtools.BuildError, detail: |
| 24 | EasyDialogs.Message(detail) |
| 25 | |
| 26 | |
| 27 | def buildapplet(): |
| 28 | buildtools.DEBUG=1 |
Jack Jansen | 0f452fa | 1995-09-01 11:54:11 +0000 | [diff] [blame] | 29 | |
| 30 | # Find the template |
| 31 | # (there's no point in proceeding if we can't find it) |
| 32 | |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 33 | template = buildtools.findtemplate() |
| 34 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 35 | # Ask for source text if not specified in sys.argv[1:] |
| 36 | |
| 37 | if not sys.argv[1:]: |
Jack Jansen | 8554e30 | 1998-02-20 16:06:56 +0000 | [diff] [blame] | 38 | srcfss, ok = macfs.PromptGetFile('Select Python source or applet:', 'TEXT', 'APPL') |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 39 | if not ok: |
| 40 | return |
| 41 | filename = srcfss.as_pathname() |
| 42 | tp, tf = os.path.split(filename) |
| 43 | if tf[-3:] == '.py': |
| 44 | tf = tf[:-3] |
| 45 | else: |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 46 | tf = tf + '.applet' |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 47 | dstfss, ok = macfs.StandardPutFile('Save application as:', tf) |
| 48 | if not ok: return |
Jack Jansen | 8554e30 | 1998-02-20 16:06:56 +0000 | [diff] [blame] | 49 | dstfilename = dstfss.as_pathname() |
| 50 | cr, tp = MacOS.GetCreatorAndType(filename) |
| 51 | if tp == 'APPL': |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 52 | buildtools.update(template, filename, dstfilename) |
Jack Jansen | 8554e30 | 1998-02-20 16:06:56 +0000 | [diff] [blame] | 53 | else: |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 54 | buildtools.process(template, filename, dstfilename, 1) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 55 | else: |
| 56 | |
| 57 | # Loop over all files to be processed |
| 58 | for filename in sys.argv[1:]: |
Jack Jansen | 8554e30 | 1998-02-20 16:06:56 +0000 | [diff] [blame] | 59 | cr, tp = MacOS.GetCreatorAndType(filename) |
| 60 | if tp == 'APPL': |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 61 | buildtools.update(template, filename, '') |
Jack Jansen | 8554e30 | 1998-02-20 16:06:56 +0000 | [diff] [blame] | 62 | else: |
Jack Jansen | 015b70e | 1998-07-31 09:44:23 +0000 | [diff] [blame] | 63 | buildtools.process(template, filename, '', 1) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | main() |