| 1 | n/a | # Emulate sys.argv and run __main__.py or __main__.pyc in an environment that |
|---|
| 2 | n/a | # is as close to "normal" as possible. |
|---|
| 3 | n/a | # |
|---|
| 4 | n/a | # This script is put into __rawmain__.pyc for applets that need argv |
|---|
| 5 | n/a | # emulation, by BuildApplet and friends. |
|---|
| 6 | n/a | # |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | from warnings import warnpy3k |
|---|
| 9 | n/a | warnpy3k("In 3.x, the appletrawmain module is removed.", stacklevel=2) |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | import argvemulator |
|---|
| 12 | n/a | import os |
|---|
| 13 | n/a | import sys |
|---|
| 14 | n/a | import marshal |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | # |
|---|
| 17 | n/a | # Make sure we have an argv[0], and make _dir point to the Resources |
|---|
| 18 | n/a | # directory. |
|---|
| 19 | n/a | # |
|---|
| 20 | n/a | if not sys.argv or sys.argv[0][:1] == '-': |
|---|
| 21 | n/a | # Insert our (guessed) name. |
|---|
| 22 | n/a | _dir = os.path.split(sys.executable)[0] # removes "python" |
|---|
| 23 | n/a | _dir = os.path.split(_dir)[0] # Removes "MacOS" |
|---|
| 24 | n/a | _dir = os.path.join(_dir, 'Resources') |
|---|
| 25 | n/a | sys.argv.insert(0, '__rawmain__') |
|---|
| 26 | n/a | else: |
|---|
| 27 | n/a | _dir = os.path.split(sys.argv[0])[0] |
|---|
| 28 | n/a | # |
|---|
| 29 | n/a | # Add the Resources directory to the path. This is where files installed |
|---|
| 30 | n/a | # by BuildApplet.py with the --extra option show up, and if those files are |
|---|
| 31 | n/a | # modules this sys.path modification is necessary to be able to import them. |
|---|
| 32 | n/a | # |
|---|
| 33 | n/a | sys.path.insert(0, _dir) |
|---|
| 34 | n/a | # |
|---|
| 35 | n/a | # Create sys.argv |
|---|
| 36 | n/a | # |
|---|
| 37 | n/a | argvemulator.ArgvCollector().mainloop() |
|---|
| 38 | n/a | # |
|---|
| 39 | n/a | # Find the real main program to run |
|---|
| 40 | n/a | # |
|---|
| 41 | n/a | __file__ = os.path.join(_dir, '__main__.py') |
|---|
| 42 | n/a | if os.path.exists(__file__): |
|---|
| 43 | n/a | # |
|---|
| 44 | n/a | # Setup something resembling a normal environment and go. |
|---|
| 45 | n/a | # |
|---|
| 46 | n/a | sys.argv[0] = __file__ |
|---|
| 47 | n/a | del argvemulator, os, sys, _dir |
|---|
| 48 | n/a | execfile(__file__) |
|---|
| 49 | n/a | else: |
|---|
| 50 | n/a | __file__ = os.path.join(_dir, '__main__.pyc') |
|---|
| 51 | n/a | if os.path.exists(__file__): |
|---|
| 52 | n/a | # |
|---|
| 53 | n/a | # If we have only a .pyc file we read the code object from that |
|---|
| 54 | n/a | # |
|---|
| 55 | n/a | sys.argv[0] = __file__ |
|---|
| 56 | n/a | _fp = open(__file__, 'rb') |
|---|
| 57 | n/a | _fp.read(8) |
|---|
| 58 | n/a | __code__ = marshal.load(_fp) |
|---|
| 59 | n/a | # |
|---|
| 60 | n/a | # Again, we create an almost-normal environment (only __code__ is |
|---|
| 61 | n/a | # funny) and go. |
|---|
| 62 | n/a | # |
|---|
| 63 | n/a | del argvemulator, os, sys, marshal, _dir, _fp |
|---|
| 64 | n/a | exec __code__ |
|---|
| 65 | n/a | else: |
|---|
| 66 | n/a | sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0]) |
|---|
| 67 | n/a | sys.exit(1) |
|---|