config/pythonpath.py
author Kyle Huey <khuey@kylehuey.com>
Mon, 01 Aug 2011 14:25:20 -0400
changeset 73728 94aa13f4a1a6a63e95640a7551be992b59d80457
parent 73241 a751aa806dbfe26b5bbce3bf5d9468656f945d6c
child 82457 a05ecb395410818e85aa220f1900fd072967fa6c
permissions -rw-r--r--
Bug 675641: Enumerate nsIStyleRuleProcessors in about:memory. r=bz

"""
Run a python script, adding extra directories to the python path.
"""


def main(args):
    def usage():
        print >>sys.stderr, "pythonpath.py -I directory script.py [args...]"
        sys.exit(150)

    paths = []

    while True:
        try:
            arg = args[0]
        except IndexError:
            usage()

        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

    script = args[0]

    sys.path[0:0] = [os.path.dirname(script)] + paths
    sys.argv = args
    sys.argc = len(args)

    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

if __name__ == '__main__':
    main(sys.argv[1:])