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 | # |
| 13 | # Create sys.argv |
| 14 | # |
| 15 | argvemulator.ArgvCollector().mainloop() |
| 16 | # |
| 17 | # Find the realy main program to run |
| 18 | # |
| 19 | _dir = os.path.split(sys.argv[0])[0] |
| 20 | __file__ = os.path.join(_dir, '__main__.py') |
| 21 | if os.path.exists(__file__): |
| 22 | # |
| 23 | # Setup something resembling a normal environment and go. |
| 24 | # |
| 25 | sys.argv[0] = __file__ |
| 26 | del argvemulator, os, sys, _dir |
| 27 | execfile(__file__) |
| 28 | else: |
| 29 | __file__ = os.path.join(_dir, '__main__.pyc') |
| 30 | if os.path.exists(__file__): |
| 31 | # |
| 32 | # If we have only a .pyc file we read the code object from that |
| 33 | # |
| 34 | sys.argv[0] = __file__ |
| 35 | _fp = open(__file__, 'rb') |
| 36 | _fp.read(8) |
| 37 | __code__ = marshal.load(_fp) |
| 38 | # |
| 39 | # Again, we create an almost-normal environment (only __code__ is |
| 40 | # funny) and go. |
| 41 | # |
| 42 | del argvemulator, os, sys, marshal, _dir, _fp |
| 43 | exec __code__ |
| 44 | else: |
| 45 | sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0]) |
| 46 | sys.exit(1) |