blob: 7fc51d8fda2f96a6d75bbd9509b11b18b8d1cbae [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#
Benjamin Peterson23681932008-05-12 21:42:13 +00007
8from warnings import warnpy3k
9warnpy3k("In 3.x, the appletrawmain module is removed.")
10
Jack Jansenba1c13d2002-08-02 14:57:43 +000011import argvemulator
12import os
13import sys
14import marshal
15
16#
Jack Jansen3e6c80b2002-08-02 15:31:25 +000017# Make sure we have an argv[0], and make _dir point to the Resources
18# directory.
19#
20if not sys.argv or sys.argv[0][:1] == '-':
Jack Jansen0ae32202003-04-09 13:25:43 +000021 # Insert our (guessed) name.
22 _dir = os.path.split(sys.executable)[0] # removes "python"
23 _dir = os.path.split(_dir)[0] # Removes "MacOS"
24 _dir = os.path.join(_dir, 'Resources')
25 sys.argv.insert(0, '__rawmain__')
Jack Jansen3e6c80b2002-08-02 15:31:25 +000026else:
Jack Jansen0ae32202003-04-09 13:25:43 +000027 _dir = os.path.split(sys.argv[0])[0]
Jack Jansen3e6c80b2002-08-02 15:31:25 +000028#
Jack Jansene87ed572002-09-12 21:58:47 +000029# Add the Resources directory to the path. This is where files installed
Tim Peters182b5ac2004-07-18 06:16:08 +000030# by BuildApplet.py with the --extra option show up, and if those files are
Jack Jansene87ed572002-09-12 21:58:47 +000031# modules this sys.path modification is necessary to be able to import them.
32#
33sys.path.insert(0, _dir)
34#
Jack Jansenba1c13d2002-08-02 14:57:43 +000035# Create sys.argv
36#
37argvemulator.ArgvCollector().mainloop()
38#
Jack Jansen3e6c80b2002-08-02 15:31:25 +000039# Find the real main program to run
Jack Jansenba1c13d2002-08-02 14:57:43 +000040#
Jack Jansenba1c13d2002-08-02 14:57:43 +000041__file__ = os.path.join(_dir, '__main__.py')
42if os.path.exists(__file__):
Jack Jansen0ae32202003-04-09 13:25:43 +000043 #
44 # Setup something resembling a normal environment and go.
45 #
46 sys.argv[0] = __file__
47 del argvemulator, os, sys, _dir
48 execfile(__file__)
Jack Jansenba1c13d2002-08-02 14:57:43 +000049else:
Jack Jansen0ae32202003-04-09 13:25:43 +000050 __file__ = os.path.join(_dir, '__main__.pyc')
51 if os.path.exists(__file__):
52 #
53 # If we have only a .pyc file we read the code object from that
54 #
55 sys.argv[0] = __file__
56 _fp = open(__file__, 'rb')
57 _fp.read(8)
58 __code__ = marshal.load(_fp)
59 #
60 # Again, we create an almost-normal environment (only __code__ is
61 # funny) and go.
62 #
63 del argvemulator, os, sys, marshal, _dir, _fp
64 exec __code__
65 else:
66 sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0])
67 sys.exit(1)