michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: """ michael@0: Run a python script, adding extra directories to the python path. michael@0: """ michael@0: michael@0: michael@0: def main(args): michael@0: def usage(): michael@0: print >>sys.stderr, "pythonpath.py -I directory script.py [args...]" michael@0: sys.exit(150) michael@0: michael@0: paths = [] michael@0: michael@0: while True: michael@0: try: michael@0: arg = args[0] michael@0: except IndexError: michael@0: usage() michael@0: michael@0: if arg == '-I': michael@0: args.pop(0) michael@0: try: michael@0: path = args.pop(0) michael@0: except IndexError: michael@0: usage() michael@0: michael@0: paths.append(os.path.abspath(path)) michael@0: continue michael@0: michael@0: if arg.startswith('-I'): michael@0: paths.append(os.path.abspath(args.pop(0)[2:])) michael@0: continue michael@0: michael@0: break michael@0: michael@0: script = args[0] michael@0: michael@0: sys.path[0:0] = [os.path.abspath(os.path.dirname(script))] + paths michael@0: sys.argv = args michael@0: sys.argc = len(args) michael@0: michael@0: frozenglobals['__name__'] = '__main__' michael@0: frozenglobals['__file__'] = script michael@0: michael@0: execfile(script, frozenglobals) michael@0: michael@0: # Freeze scope here ... why this makes things work I have no idea ... michael@0: frozenglobals = globals() michael@0: michael@0: import sys, os michael@0: michael@0: if __name__ == '__main__': michael@0: main(sys.argv[1:])