blob: 1be9187907b9019e992a122fe40f750ef4b0a366 [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#
Jack Jansen3e6c80b2002-08-02 15:31:25 +000013# Make sure we have an argv[0], and make _dir point to the Resources
14# directory.
15#
16if not sys.argv or sys.argv[0][:1] == '-':
Jack Jansen0ae32202003-04-09 13:25:43 +000017 # 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 Jansen3e6c80b2002-08-02 15:31:25 +000022else:
Jack Jansen0ae32202003-04-09 13:25:43 +000023 _dir = os.path.split(sys.argv[0])[0]
Jack Jansen3e6c80b2002-08-02 15:31:25 +000024#
Jack Jansene87ed572002-09-12 21:58:47 +000025# Add the Resources directory to the path. This is where files installed
Tim Peters182b5ac2004-07-18 06:16:08 +000026# by BuildApplet.py with the --extra option show up, and if those files are
Jack Jansene87ed572002-09-12 21:58:47 +000027# modules this sys.path modification is necessary to be able to import them.
28#
29sys.path.insert(0, _dir)
30#
Jack Jansenba1c13d2002-08-02 14:57:43 +000031# Create sys.argv
32#
33argvemulator.ArgvCollector().mainloop()
34#
Jack Jansen3e6c80b2002-08-02 15:31:25 +000035# Find the real main program to run
Jack Jansenba1c13d2002-08-02 14:57:43 +000036#
Jack Jansenba1c13d2002-08-02 14:57:43 +000037__file__ = os.path.join(_dir, '__main__.py')
38if os.path.exists(__file__):
Jack Jansen0ae32202003-04-09 13:25:43 +000039 #
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 Jansenba1c13d2002-08-02 14:57:43 +000045else:
Jack Jansen0ae32202003-04-09 13:25:43 +000046 __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)