]> granicus.if.org Git - python/commitdiff
Final step in making applets first-class citizens: if the applet wants
authorJack Jansen <jack.jansen@cwi.nl>
Fri, 2 Aug 2002 14:57:43 +0000 (14:57 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Fri, 2 Aug 2002 14:57:43 +0000 (14:57 +0000)
argv emulation (i.e. if the end user drops files and folders on the
applets these will show up in sys.argv) BuildApplet will add the required
code to the applet bundle, in __rawmain__.pyc.

This code is compiled from appletrawmain.py, it creates sys.argv, cleans
up most of the mess and executes either __main__.py or __main__.pyc.

Mac/Lib/appletrawmain.py [new file with mode: 0644]
Mac/Lib/buildtools.py

diff --git a/Mac/Lib/appletrawmain.py b/Mac/Lib/appletrawmain.py
new file mode 100644 (file)
index 0000000..d2fcd6e
--- /dev/null
@@ -0,0 +1,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)
index 982570855b65fe45a544b39754f977c9c4847b78..1047da5713004e88a697a1e79106fe0e9ac4bb97 100644 (file)
@@ -398,6 +398,16 @@ def process_common_macho(template, progress, code, rsrcname, destname, is_update
                        pycname = '__rawmain__.pyc'
                else:
                        pycname = '__main__.pyc'
+                       # And we also create __rawmain__.pyc
+                       outputfilename = os.path.join(destname, 'Contents', 'Resources', '__rawmain__.pyc')
+                       if progress:
+                               progress.label('Creating __rawmain__.pyc')
+                               progress.inc(0)
+                       rawsourcefile = os.path.join(sys.prefix, 'Mac', 'Lib', 'appletrawmain.py')
+                       rawsource = open(rawsourcefile, 'rU').read()
+                       rawcode = compile(rawsource, rawsourcefile, 'exec')
+                       writepycfile(rawcode, outputfilename)
+                       
                outputfilename = os.path.join(destname, 'Contents', 'Resources', pycname)
                if progress:
                        progress.label('Creating '+pycname)