2009-04-15 13:06:09 +00:00
|
|
|
"""
|
|
|
|
Run a python script, adding extra directories to the python path.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2011-07-21 22:07:33 +00:00
|
|
|
def main(args):
|
|
|
|
def usage():
|
|
|
|
print >>sys.stderr, "pythonpath.py -I directory script.py [args...]"
|
|
|
|
sys.exit(150)
|
2009-04-15 13:06:09 +00:00
|
|
|
|
2011-07-21 22:07:33 +00:00
|
|
|
paths = []
|
2009-04-15 13:06:09 +00:00
|
|
|
|
2011-07-21 22:07:33 +00:00
|
|
|
while True:
|
2009-04-15 13:06:09 +00:00
|
|
|
try:
|
2011-07-21 22:07:33 +00:00
|
|
|
arg = args[0]
|
2009-04-15 13:06:09 +00:00
|
|
|
except IndexError:
|
|
|
|
usage()
|
|
|
|
|
2011-07-21 22:07:33 +00:00
|
|
|
if arg == '-I':
|
|
|
|
args.pop(0)
|
|
|
|
try:
|
|
|
|
path = args.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
usage()
|
|
|
|
|
2011-12-12 20:44:41 +00:00
|
|
|
paths.append(os.path.abspath(path))
|
2011-07-21 22:07:33 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if arg.startswith('-I'):
|
2011-12-12 20:44:41 +00:00
|
|
|
paths.append(os.path.abspath(args.pop(0)[2:]))
|
2011-07-21 22:07:33 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
break
|
2009-04-15 13:06:09 +00:00
|
|
|
|
2011-07-21 22:07:33 +00:00
|
|
|
script = args[0]
|
2009-04-15 13:06:09 +00:00
|
|
|
|
2011-12-12 20:44:41 +00:00
|
|
|
sys.path[0:0] = [os.path.abspath(os.path.dirname(script))] + paths
|
2011-07-21 22:07:33 +00:00
|
|
|
sys.argv = args
|
|
|
|
sys.argc = len(args)
|
2009-04-15 13:06:09 +00:00
|
|
|
|
2011-07-21 22:07:33 +00:00
|
|
|
frozenglobals['__name__'] = '__main__'
|
|
|
|
frozenglobals['__file__'] = script
|
|
|
|
|
|
|
|
execfile(script, frozenglobals)
|
|
|
|
|
|
|
|
# Freeze scope here ... why this makes things work I have no idea ...
|
|
|
|
frozenglobals = globals()
|
|
|
|
|
|
|
|
import sys, os
|
2009-04-15 13:06:09 +00:00
|
|
|
|
2011-07-21 22:07:33 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|