blob: d2fcd6eb8c9491dc7f4b7201097a95e86bd3b570 [file] [log] [blame]
Jack Jansenba1c13d2002-08-02 14:57:43 +00001# 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#
7import argvemulator
8import os
9import sys
10import marshal
11
12#
13# Create sys.argv
14#
15argvemulator.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')
21if 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__)
28else:
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)