/Lib/plat-mac/appletrawmain.py

http://unladen-swallow.googlecode.com/ · Python · 67 lines · 32 code · 3 blank · 32 comment · 7 complexity · 552380f1e3f65dcc78f48c5bea729a58 MD5 · raw file

  1. # 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. #
  7. from warnings import warnpy3k
  8. warnpy3k("In 3.x, the appletrawmain module is removed.", stacklevel=2)
  9. import argvemulator
  10. import os
  11. import sys
  12. import marshal
  13. #
  14. # Make sure we have an argv[0], and make _dir point to the Resources
  15. # directory.
  16. #
  17. if not sys.argv or sys.argv[0][:1] == '-':
  18. # Insert our (guessed) name.
  19. _dir = os.path.split(sys.executable)[0] # removes "python"
  20. _dir = os.path.split(_dir)[0] # Removes "MacOS"
  21. _dir = os.path.join(_dir, 'Resources')
  22. sys.argv.insert(0, '__rawmain__')
  23. else:
  24. _dir = os.path.split(sys.argv[0])[0]
  25. #
  26. # Add the Resources directory to the path. This is where files installed
  27. # by BuildApplet.py with the --extra option show up, and if those files are
  28. # modules this sys.path modification is necessary to be able to import them.
  29. #
  30. sys.path.insert(0, _dir)
  31. #
  32. # Create sys.argv
  33. #
  34. argvemulator.ArgvCollector().mainloop()
  35. #
  36. # Find the real main program to run
  37. #
  38. __file__ = os.path.join(_dir, '__main__.py')
  39. if os.path.exists(__file__):
  40. #
  41. # Setup something resembling a normal environment and go.
  42. #
  43. sys.argv[0] = __file__
  44. del argvemulator, os, sys, _dir
  45. execfile(__file__)
  46. else:
  47. __file__ = os.path.join(_dir, '__main__.pyc')
  48. if os.path.exists(__file__):
  49. #
  50. # If we have only a .pyc file we read the code object from that
  51. #
  52. sys.argv[0] = __file__
  53. _fp = open(__file__, 'rb')
  54. _fp.read(8)
  55. __code__ = marshal.load(_fp)
  56. #
  57. # Again, we create an almost-normal environment (only __code__ is
  58. # funny) and go.
  59. #
  60. del argvemulator, os, sys, marshal, _dir, _fp
  61. exec __code__
  62. else:
  63. sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0])
  64. sys.exit(1)