2009-04-15 17:06:09 +04:00
|
|
|
"""
|
|
|
|
Run a python script, adding extra directories to the python path.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
def main(args):
|
|
|
|
def usage():
|
|
|
|
print >>sys.stderr, "pythonpath.py -I directory script.py [args...]"
|
|
|
|
sys.exit(150)
|
2009-04-15 17:06:09 +04:00
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
paths = []
|
2009-04-15 17:06:09 +04:00
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
while True:
|
2009-04-15 17:06:09 +04:00
|
|
|
try:
|
2011-07-22 02:07:33 +04:00
|
|
|
arg = args[0]
|
2009-04-15 17:06:09 +04:00
|
|
|
except IndexError:
|
|
|
|
usage()
|
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
if arg == '-I':
|
|
|
|
args.pop(0)
|
|
|
|
try:
|
|
|
|
path = args.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
paths.append(path)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if arg.startswith('-I'):
|
|
|
|
paths.append(args.pop(0)[2:])
|
|
|
|
continue
|
|
|
|
|
|
|
|
break
|
2009-04-15 17:06:09 +04:00
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
script = args[0]
|
2009-04-15 17:06:09 +04:00
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
sys.path[0:0] = [os.path.dirname(script)] + paths
|
|
|
|
sys.argv = args
|
|
|
|
sys.argc = len(args)
|
2009-04-15 17:06:09 +04:00
|
|
|
|
2011-07-22 02:07:33 +04: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 17:06:09 +04:00
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|