summaryrefslogtreecommitdiffstats
path: root/Mac/Lib/appletrawmain.py
blob: d2fcd6eb8c9491dc7f4b7201097a95e86bd3b570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Emulate sys.argv and run __main__.py or __main__.pyc in an environment that
# is as close to "normal" as possible.
#
# This script is put into __rawmain__.pyc for applets that need argv
# emulation, by BuildApplet and friends.
#
import argvemulator
import os
import sys
import marshal

#
# Create sys.argv
#
argvemulator.ArgvCollector().mainloop()
#
# Find the realy main program to run
#
_dir = os.path.split(sys.argv[0])[0]
__file__ = os.path.join(_dir, '__main__.py')
if os.path.exists(__file__):
	#
	# Setup something resembling a normal environment and go.
	#
	sys.argv[0] = __file__
	del argvemulator, os, sys, _dir
	execfile(__file__)
else:
	__file__ = os.path.join(_dir, '__main__.pyc')
	if os.path.exists(__file__):
		#
		# If we have only a .pyc file we read the code object from that
		#
		sys.argv[0] = __file__
		_fp = open(__file__, 'rb')
		_fp.read(8)
		__code__ = marshal.load(_fp)
		#
		# Again, we create an almost-normal environment (only __code__ is
		# funny) and go.
		#
		del argvemulator, os, sys, marshal, _dir, _fp
		exec __code__
	else:
		sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0])
		sys.exit(1)