2012-05-21 15:12:37 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-07-08 20:30:34 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
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()
|
|
|
|
|
2011-12-13 00:44:41 +04:00
|
|
|
paths.append(os.path.abspath(path))
|
2011-07-22 02:07:33 +04:00
|
|
|
continue
|
|
|
|
|
|
|
|
if arg.startswith('-I'):
|
2011-12-13 00:44:41 +04:00
|
|
|
paths.append(os.path.abspath(args.pop(0)[2:]))
|
2011-07-22 02:07:33 +04:00
|
|
|
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-12-13 00:44:41 +04:00
|
|
|
sys.path[0:0] = [os.path.abspath(os.path.dirname(script))] + paths
|
2011-07-22 02:07:33 +04:00
|
|
|
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)
|
|
|
|
|
2018-05-22 01:01:01 +03:00
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
# Freeze scope here ... why this makes things work I have no idea ...
|
|
|
|
frozenglobals = globals()
|
|
|
|
|
2018-05-22 01:01:01 +03:00
|
|
|
import sys
|
|
|
|
import os
|
2009-04-15 17:06:09 +04:00
|
|
|
|
2011-07-22 02:07:33 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|