pythonpath.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. """
  5. Run a python script, adding extra directories to the python path.
  6. """
  7. def main(args):
  8. def usage():
  9. print >>sys.stderr, "pythonpath.py -I directory script.py [args...]"
  10. sys.exit(150)
  11. paths = []
  12. while True:
  13. try:
  14. arg = args[0]
  15. except IndexError:
  16. usage()
  17. if arg == '-I':
  18. args.pop(0)
  19. try:
  20. path = args.pop(0)
  21. except IndexError:
  22. usage()
  23. paths.append(os.path.abspath(path))
  24. continue
  25. if arg.startswith('-I'):
  26. paths.append(os.path.abspath(args.pop(0)[2:]))
  27. continue
  28. break
  29. script = args[0]
  30. sys.path[0:0] = [os.path.abspath(os.path.dirname(script))] + paths
  31. sys.argv = args
  32. sys.argc = len(args)
  33. frozenglobals['__name__'] = '__main__'
  34. frozenglobals['__file__'] = script
  35. execfile(script, frozenglobals)
  36. # Freeze scope here ... why this makes things work I have no idea ...
  37. frozenglobals = globals()
  38. import sys, os
  39. if __name__ == '__main__':
  40. main(sys.argv[1:])