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""" 6Run a python script, adding extra directories to the python path. 7""" 8 9 10def main(args): 11 def usage(): 12 print >>sys.stderr, "pythonpath.py -I directory script.py [args...]" 13 sys.exit(150) 14 15 paths = [] 16 17 while True: 18 try: 19 arg = args[0] 20 except IndexError: 21 usage() 22 23 if arg == '-I': 24 args.pop(0) 25 try: 26 path = args.pop(0) 27 except IndexError: 28 usage() 29 30 paths.append(os.path.abspath(path)) 31 continue 32 33 if arg.startswith('-I'): 34 paths.append(os.path.abspath(args.pop(0)[2:])) 35 continue 36 37 if arg.startswith('-D'): 38 os.chdir(args.pop(0)[2:]) 39 continue 40 41 break 42 43 script = args[0] 44 45 sys.path[0:0] = [os.path.abspath(os.path.dirname(script))] + paths 46 sys.argv = args 47 sys.argc = len(args) 48 49 frozenglobals['__name__'] = '__main__' 50 frozenglobals['__file__'] = script 51 52 execfile(script, frozenglobals) 53 54# Freeze scope here ... why this makes things work I have no idea ... 55frozenglobals = globals() 56 57import sys 58import os 59 60if __name__ == '__main__': 61 main(sys.argv[1:]) 62