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] == '-': |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 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__') |
Jack Jansen | 3e6c80b | 2002-08-02 15:31:25 +0000 | [diff] [blame] | 22 | else: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 23 | _dir = os.path.split(sys.argv[0])[0] |
Jack Jansen | 3e6c80b | 2002-08-02 15:31:25 +0000 | [diff] [blame] | 24 | # |
Jack Jansen | e87ed57 | 2002-09-12 21:58:47 +0000 | [diff] [blame] | 25 | # Add the Resources directory to the path. This is where files installed |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 26 | # by BuildApplet.py with the --extra option show up, and if those files are |
Jack Jansen | e87ed57 | 2002-09-12 21:58:47 +0000 | [diff] [blame] | 27 | # modules this sys.path modification is necessary to be able to import them. |
| 28 | # |
| 29 | sys.path.insert(0, _dir) |
| 30 | # |
Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 31 | # Create sys.argv |
| 32 | # |
| 33 | argvemulator.ArgvCollector().mainloop() |
| 34 | # |
Jack Jansen | 3e6c80b | 2002-08-02 15:31:25 +0000 | [diff] [blame] | 35 | # Find the real main program to run |
Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 36 | # |
Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 37 | __file__ = os.path.join(_dir, '__main__.py') |
| 38 | if os.path.exists(__file__): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 39 | # |
| 40 | # Setup something resembling a normal environment and go. |
| 41 | # |
| 42 | sys.argv[0] = __file__ |
| 43 | del argvemulator, os, sys, _dir |
| 44 | execfile(__file__) |
Jack Jansen | ba1c13d | 2002-08-02 14:57:43 +0000 | [diff] [blame] | 45 | else: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 46 | __file__ = os.path.join(_dir, '__main__.pyc') |
| 47 | if os.path.exists(__file__): |
| 48 | # |
| 49 | # If we have only a .pyc file we read the code object from that |
| 50 | # |
| 51 | sys.argv[0] = __file__ |
| 52 | _fp = open(__file__, 'rb') |
| 53 | _fp.read(8) |
| 54 | __code__ = marshal.load(_fp) |
| 55 | # |
| 56 | # Again, we create an almost-normal environment (only __code__ is |
| 57 | # funny) and go. |
| 58 | # |
| 59 | del argvemulator, os, sys, marshal, _dir, _fp |
| 60 | exec __code__ |
| 61 | else: |
| 62 | sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0]) |
| 63 | sys.exit(1) |