Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 1 | """Import a module while pretending its name is __main__. This |
| 2 | can be used to run scripts from the PackedLib resource file while pretending |
| 3 | they have been double-clicked.""" |
| 4 | |
| 5 | import imp |
| 6 | import sys |
| 7 | import os |
| 8 | import string |
| 9 | import Dlg |
| 10 | import macfs |
| 11 | |
Jack Jansen | 9062fa2 | 1995-08-14 12:21:12 +0000 | [diff] [blame] | 12 | DIALOG_ID = 512 |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 13 | OK = 1 |
| 14 | CANCEL = 2 |
| 15 | SCRIPTNAME=3 |
| 16 | ARGV=4 |
| 17 | STDIN_CONS=5 |
| 18 | STDIN_FILE=6 |
| 19 | STDOUT_CONS=7 |
| 20 | STDOUT_FILE=8 |
| 21 | WORKING_DIR=9 |
| 22 | PAUSE=10 |
| 23 | |
| 24 | def import_as_main(name): |
| 25 | fp, path, (suffix, mode, type) = imp.find_module(name) |
| 26 | if type == imp.PY_SOURCE: |
Jack Jansen | fbac4bb | 1995-08-31 13:46:13 +0000 | [diff] [blame] | 27 | imp.load_source('__main__', path, fp) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 28 | elif type == imp.PY_COMPILED: |
Jack Jansen | fbac4bb | 1995-08-31 13:46:13 +0000 | [diff] [blame] | 29 | imp.load_compiled('__main__', path, fp) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 30 | elif type == imp.PY_RESOURCE: |
| 31 | imp.load_resource('__main__', path) |
| 32 | |
| 33 | def interact(): |
| 34 | d = Dlg.GetNewDialog(DIALOG_ID, -1) |
| 35 | wdir = stdin = stdout = None |
| 36 | pause = 0 |
| 37 | |
| 38 | tp, in_c_h, rect = d.GetDialogItem(STDIN_CONS) |
| 39 | tp, in_f_h, rect = d.GetDialogItem(STDIN_FILE) |
| 40 | tp, out_c_h, rect = d.GetDialogItem(STDOUT_CONS) |
| 41 | tp, out_f_h, rect = d.GetDialogItem(STDOUT_FILE) |
| 42 | tp, pause_h, rect = d.GetDialogItem(PAUSE) |
| 43 | in_c_h = in_c_h.as_Control() |
| 44 | in_f_h = in_f_h.as_Control() |
| 45 | out_c_h = out_c_h.as_Control() |
| 46 | out_f_h = out_f_h.as_Control() |
| 47 | pause_h = pause_h.as_Control() |
| 48 | |
| 49 | while 1: |
| 50 | in_c_h.SetControlValue(not stdin) |
| 51 | in_f_h.SetControlValue(not not stdin) |
| 52 | out_c_h.SetControlValue(not stdout) |
| 53 | out_f_h.SetControlValue(not not stdout) |
| 54 | pause_h.SetControlValue(pause) |
| 55 | |
| 56 | n = Dlg.ModalDialog(None) |
| 57 | if n == OK: |
| 58 | break |
| 59 | elif n == CANCEL: |
| 60 | sys.exit(0) |
| 61 | elif n == STDIN_CONS: |
| 62 | stdin = None |
| 63 | elif n == STDIN_FILE: |
| 64 | fss, ok = macfs.StandardGetFile('TEXT') |
| 65 | if ok: |
| 66 | stdin = fss |
| 67 | elif n == STDOUT_FILE: |
| 68 | fss, ok = macfs.StandardPutFile('stdout:') |
| 69 | if ok: |
| 70 | stdout = fss |
| 71 | elif n == WORKING_DIR: |
| 72 | fss, ok = macfs.GetDirectory() |
| 73 | if ok: |
| 74 | wdir = fss |
| 75 | elif n == PAUSE: |
| 76 | pause = (not pause) |
| 77 | |
| 78 | tp, h, rect = d.GetDialogItem(SCRIPTNAME) |
| 79 | name = Dlg.GetDialogItemText(h) |
| 80 | tp, h, rect = d.GetDialogItem(ARGV) |
| 81 | argv = Dlg.GetDialogItemText(h) |
| 82 | return name, argv, stdin, stdout, wdir, pause |
| 83 | |
| 84 | def main(): |
| 85 | curdir = os.getcwd() |
| 86 | import Res |
Jack Jansen | fbac4bb | 1995-08-31 13:46:13 +0000 | [diff] [blame] | 87 | try: |
Jack Jansen | d13c385 | 2000-06-20 21:59:25 +0000 | [diff] [blame] | 88 | Res.FSpOpenResFile('RunLibScript.rsrc', 1) |
Jack Jansen | fbac4bb | 1995-08-31 13:46:13 +0000 | [diff] [blame] | 89 | except: |
| 90 | pass # Assume we're an applet already |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 91 | name, argv, stdin, stdout, wdir, pause = interact() |
| 92 | if not name: |
| 93 | sys.exit(0) |
| 94 | sys.argv = [name] + string.split(argv) |
| 95 | if stdin: |
| 96 | sys.stdin = open(stdin.as_pathname()) |
| 97 | if stdout: |
| 98 | sys.stdout = open(stdout.as_pathname(), 'w') |
| 99 | if wdir: |
| 100 | os.chdir(wdir.as_pathname()) |
| 101 | else: |
| 102 | os.chdir(curdir) |
| 103 | |
| 104 | import_as_main(name) |
| 105 | |
| 106 | if pause: |
| 107 | sys.exit(1) |
| 108 | |
| 109 | if __name__ == '__main__': |
| 110 | main() |
| 111 | |