config/pythonpath.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/config/pythonpath.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,56 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +"""
     1.9 +Run a python script, adding extra directories to the python path.
    1.10 +"""
    1.11 +
    1.12 +
    1.13 +def main(args):
    1.14 +    def usage():
    1.15 +        print >>sys.stderr, "pythonpath.py -I directory script.py [args...]"
    1.16 +        sys.exit(150)
    1.17 +
    1.18 +    paths = []
    1.19 +
    1.20 +    while True:
    1.21 +        try:
    1.22 +            arg = args[0]
    1.23 +        except IndexError:
    1.24 +            usage()
    1.25 +
    1.26 +        if arg == '-I':
    1.27 +            args.pop(0)
    1.28 +            try:
    1.29 +                path = args.pop(0)
    1.30 +            except IndexError:
    1.31 +                usage()
    1.32 +
    1.33 +            paths.append(os.path.abspath(path))
    1.34 +            continue
    1.35 +
    1.36 +        if arg.startswith('-I'):
    1.37 +            paths.append(os.path.abspath(args.pop(0)[2:]))
    1.38 +            continue
    1.39 +
    1.40 +        break
    1.41 +
    1.42 +    script = args[0]
    1.43 +
    1.44 +    sys.path[0:0] = [os.path.abspath(os.path.dirname(script))] + paths
    1.45 +    sys.argv = args
    1.46 +    sys.argc = len(args)
    1.47 +
    1.48 +    frozenglobals['__name__'] = '__main__'
    1.49 +    frozenglobals['__file__'] = script
    1.50 +
    1.51 +    execfile(script, frozenglobals)
    1.52 +
    1.53 +# Freeze scope here ... why this makes things work I have no idea ...
    1.54 +frozenglobals = globals()
    1.55 +
    1.56 +import sys, os
    1.57 +
    1.58 +if __name__ == '__main__':
    1.59 +    main(sys.argv[1:])

mercurial