@@ -266,6 +266,12 @@ def __load():
266266exec "${executable}" "${main}" "$@"
267267"""
268268
269+ ARGVEMULATOR = """\
270+ import argvemulator, os
271+
272+ argvemulator.ArgvCollector().mainloop()
273+ execfile(os.path.join(os.path.split(__file__)[0], "%(realmainprogram)s"))
274+ """
269275
270276class AppBuilder (BundleBuilder ):
271277
@@ -293,6 +299,10 @@ class AppBuilder(BundleBuilder):
293299
294300 # If True, build standalone app.
295301 standalone = 0
302+
303+ # If True, add a real main program that emulates sys.argv before calling
304+ # mainprogram
305+ argv_emulation = 0
296306
297307 # The following attributes are only used when building a standalone app.
298308
@@ -368,6 +378,27 @@ def preProcess(self):
368378 if self .mainprogram is not None :
369379 mainprogram = os .path .basename (self .mainprogram )
370380 self .files .append ((self .mainprogram , pathjoin (resdir , mainprogram )))
381+ if self .argv_emulation :
382+ # Change the main program, and create the helper main program (which
383+ # does argv collection and then calls the real main).
384+ # Also update the included modules (if we're creating a standalone
385+ # program) and the plist
386+ realmainprogram = mainprogram
387+ mainprogram = '__argvemulator_' + mainprogram
388+ resdirpath = pathjoin (self .bundlepath , resdir )
389+ mainprogrampath = pathjoin (resdirpath , mainprogram )
390+ makedirs (resdirpath )
391+ open (mainprogrampath , "w" ).write (ARGVEMULATOR % locals ())
392+ if self .standalone :
393+ self .includeModules .append ("argvemulator" )
394+ self .includeModules .append ("os" )
395+ if not self .plist .has_key ("CFBundleDocumentTypes" ):
396+ self .plist ["CFBundleDocumentTypes" ] = [
397+ { "CFBundleTypeOSTypes" : [
398+ "****" ,
399+ "fold" ,
400+ "disk" ],
401+ "CFBundleTypeRole" : "Viewer" }]
371402 # Write bootstrap script
372403 executable = os .path .basename (self .executable )
373404 execdir = pathjoin (self .bundlepath , self .execdir )
@@ -619,6 +650,7 @@ def pathjoin(*args):
619650 -r, --resource=FILE extra file or folder to be copied to Resources
620651 -e, --executable=FILE the executable to be used
621652 -m, --mainprogram=FILE the Python main program
653+ -a, --argv add a wrapper main program to create sys.argv
622654 -p, --plist=FILE .plist file (default: generate one)
623655 --nib=NAME main nib name
624656 -c, --creator=CCCC 4-char creator code (default: '????')
@@ -647,10 +679,10 @@ def main(builder=None):
647679 if builder is None :
648680 builder = AppBuilder (verbosity = 1 )
649681
650- shortopts = "b:n:r:e:m:c:p:lx:i:hvq "
682+ shortopts = "b:n:r:e:m:c:p:lx:i:hvqa "
651683 longopts = ("builddir=" , "name=" , "resource=" , "executable=" ,
652684 "mainprogram=" , "creator=" , "nib=" , "plist=" , "link" ,
653- "link-exec" , "help" , "verbose" , "quiet" , "standalone" ,
685+ "link-exec" , "help" , "verbose" , "quiet" , "argv" , " standalone" ,
654686 "exclude=" , "include=" , "package=" , "strip" , "iconfile=" )
655687
656688 try :
@@ -669,6 +701,8 @@ def main(builder=None):
669701 builder .executable = arg
670702 elif opt in ('-m' , '--mainprogram' ):
671703 builder .mainprogram = arg
704+ elif opt in ('-a' , '--argv' ):
705+ builder .argv_emulation = 1
672706 elif opt in ('-c' , '--creator' ):
673707 builder .creator = arg
674708 elif opt == '--iconfile' :
0 commit comments