1 | import spidermonkey |
---|
2 | import threading |
---|
3 | import platform |
---|
4 | import httplib |
---|
5 | import time |
---|
6 | import sys |
---|
7 | import os |
---|
8 | |
---|
9 | runtime = spidermonkey.Runtime() |
---|
10 | context = runtime.new_context() |
---|
11 | lock = threading.Lock() |
---|
12 | |
---|
13 | def load(filename): |
---|
14 | script = '' |
---|
15 | f = open(filename, 'r') |
---|
16 | script = f.read() |
---|
17 | context.execute(script) |
---|
18 | |
---|
19 | def _print(string): |
---|
20 | print string |
---|
21 | |
---|
22 | def _open(url, mode): |
---|
23 | return open(url, mode) |
---|
24 | |
---|
25 | def _exit(): |
---|
26 | return os._exit(1) |
---|
27 | |
---|
28 | def new_global(): |
---|
29 | nc = runtime.new_context() |
---|
30 | configure_context(nc) |
---|
31 | return nc.execute(''' |
---|
32 | this.execute = global.execute; |
---|
33 | this; |
---|
34 | ''') |
---|
35 | |
---|
36 | def configure_context(context): |
---|
37 | context.add_global('os', os) |
---|
38 | context.add_global('sys', sys) |
---|
39 | context.add_global('time', time) |
---|
40 | context.add_global('$lock', lock); |
---|
41 | context.add_global('exit', _exit) |
---|
42 | context.add_global('fopen', _open) |
---|
43 | context.add_global('print', _print) |
---|
44 | context.add_global('httplib', httplib) |
---|
45 | context.add_global('platform', platform) |
---|
46 | context.add_global('threading', threading) |
---|
47 | context.add_global('global', context) |
---|
48 | context.add_global('runtime', runtime) |
---|
49 | context.add_global('new_global', new_global) |
---|
50 | |
---|
51 | configure_context(context) |
---|
52 | |
---|
53 | if __name__=='__main__': |
---|
54 | argv = sys.argv |
---|
55 | if os.path.isfile( argv[1] ): |
---|
56 | load(argv[1]) |
---|