|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 |
|
5 """ |
|
6 Run a python script, adding extra directories to the python path. |
|
7 """ |
|
8 |
|
9 |
|
10 def main(args): |
|
11 def usage(): |
|
12 print >>sys.stderr, "pythonpath.py -I directory script.py [args...]" |
|
13 sys.exit(150) |
|
14 |
|
15 paths = [] |
|
16 |
|
17 while True: |
|
18 try: |
|
19 arg = args[0] |
|
20 except IndexError: |
|
21 usage() |
|
22 |
|
23 if arg == '-I': |
|
24 args.pop(0) |
|
25 try: |
|
26 path = args.pop(0) |
|
27 except IndexError: |
|
28 usage() |
|
29 |
|
30 paths.append(os.path.abspath(path)) |
|
31 continue |
|
32 |
|
33 if arg.startswith('-I'): |
|
34 paths.append(os.path.abspath(args.pop(0)[2:])) |
|
35 continue |
|
36 |
|
37 break |
|
38 |
|
39 script = args[0] |
|
40 |
|
41 sys.path[0:0] = [os.path.abspath(os.path.dirname(script))] + paths |
|
42 sys.argv = args |
|
43 sys.argc = len(args) |
|
44 |
|
45 frozenglobals['__name__'] = '__main__' |
|
46 frozenglobals['__file__'] = script |
|
47 |
|
48 execfile(script, frozenglobals) |
|
49 |
|
50 # Freeze scope here ... why this makes things work I have no idea ... |
|
51 frozenglobals = globals() |
|
52 |
|
53 import sys, os |
|
54 |
|
55 if __name__ == '__main__': |
|
56 main(sys.argv[1:]) |