Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 1 | # Emulate sys.argv and run __main__.py or __main__.pyc in an environment that |
| 2 | # is as close to "normal" as possible. |
| 3 | # |
| 4 | # This script is put into __rawmain__.pyc for applets that need argv |
| 5 | # emulation, by BuildApplet and friends. |
| 6 | # |
| 7 | import argvemulator |
| 8 | import os |
| 9 | import sys |
| 10 | import marshal |
| 11 | |
| 12 | # |
Jack Jansen | 3e6c80b | 2002-08-02 15:31:25 +0000 | [diff] [blame] | 13 | # Make sure we have an argv[0], and make _dir point to the Resources |
| 14 | # directory. |
| 15 | # |
| 16 | if not sys.argv or sys.argv[0][:1] == '-': |
| 17 | # Insert our (guessed) name. |
| 18 | _dir = os.path.split(sys.executable)[0] # removes "python" |
| 19 | _dir = os.path.split(_dir)[0] # Removes "MacOS" |
| 20 | _dir = os.path.join(_dir, 'Resources') |
| 21 | sys.argv.insert(0, '__rawmain__') |
| 22 | else: |
| 23 | _dir = os.path.split(sys.argv[0])[0] |
| 24 | # |
Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 25 | # Create sys.argv |
| 26 | # |
| 27 | argvemulator.ArgvCollector().mainloop() |
| 28 | # |
Jack Jansen | 3e6c80b | 2002-08-02 15:31:25 +0000 | [diff] [blame] | 29 | # Find the real main program to run |
Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 30 | # |
Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 31 | __file__ = os.path.join(_dir, '__main__.py') |
| 32 | if os.path.exists(__file__): |
| 33 | # |
| 34 | # Setup something resembling a normal environment and go. |
| 35 | # |
| 36 | sys.argv[0] = __file__ |
| 37 | del argvemulator, os, sys, _dir |
| 38 | execfile(__file__) |
| 39 | else: |
| 40 | __file__ = os.path.join(_dir, '__main__.pyc') |
| 41 | if os.path.exists(__file__): |
| 42 | # |
| 43 | # If we have only a .pyc file we read the code object from that |
| 44 | # |
| 45 | sys.argv[0] = __file__ |
| 46 | _fp = open(__file__, 'rb') |
| 47 | _fp.read(8) |
| 48 | __code__ = marshal.load(_fp) |
| 49 | # |
| 50 | # Again, we create an almost-normal environment (only __code__ is |
| 51 | # funny) and go. |
| 52 | # |
| 53 | del argvemulator, os, sys, marshal, _dir, _fp |
| 54 | exec __code__ |
| 55 | else: |
| 56 | sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0]) |
| 57 | sys.exit(1) |