~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: drtomc
  • Date: 2008-01-13 20:27:18 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:212
Gosh, it all looks pretty simple in the end!
TODO: handle exit/^C correctly

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import cgi
12
12
import cStringIO
13
13
import signal
14
 
 
15
 
globs = {}
16
 
globs['__builtins__'] = globals()['__builtins__']
17
 
locls = {}
18
 
compiler = codeop.CommandCompiler()
19
 
curr_cmd = ''
20
 
 
21
 
def do_chat(txt):
22
 
    global curr_cmd
23
 
    if curr_cmd == '':
24
 
        curr_cmd = txt
25
 
    else:
26
 
        curr_cmd = curr_cmd + '\n' + txt
27
 
    try:
28
 
        cmd = compiler(curr_cmd)
29
 
        if cmd is None:
30
 
            # The command was incomplete,
31
 
            # so send back a None, so the
32
 
            # client can print a '...'
33
 
            web.output(cjson.encode(None))
34
 
        else:
35
 
            # The command was complete,
36
 
            # so evaluate it!
37
 
            out = cStringIO.StringIO()
38
 
            sys.stdout = out
39
 
            sys.stderr = out
40
 
            signal.alarm(5)
41
 
            res = eval(cmd, globs, locls)
42
 
            signal.alarm(0)
43
 
            v = (out.getvalue(), res, None)
44
 
            web.output(cjson.encode(v))
45
 
            curr_cmd = ''
46
 
    except Exception, exc:
 
14
import Queue
 
15
from threading import Thread
 
16
 
 
17
class StdinFromWeb(object):
 
18
    def __init__(self, cmdQ, lineQ):
 
19
        self.cmdQ = cmdQ
 
20
        self.lineQ = lineQ
 
21
 
 
22
    def readline(self):
 
23
        # stop the clock!
47
24
        signal.alarm(0)
48
 
        v = (None, None, str(exc))
49
 
        web.output(cjson.encode(v))
 
25
        self.cmdQ.put({"input":None})
 
26
        l = self.lineQ.get()
 
27
        # restart the clock:
 
28
        # Some of our 5 seconds may have elapsed, but
 
29
        # never mind.
 
30
        signal.alarm(5)
 
31
        return l
 
32
 
 
33
class PythonRunner(Thread):
 
34
    def __init__(self, cmdQ, lineQ):
 
35
        self.cmdQ = cmdQ
 
36
        self.lineQ = lineQ
 
37
        Thread.__init__(self)
 
38
 
 
39
    def run(self):
 
40
        globs = {}
 
41
        globs['__builtins__'] = globals()['__builtins__']
 
42
        locls = {}
 
43
        compiler = codeop.CommandCompiler()
50
44
        curr_cmd = ''
51
45
 
 
46
        while True:
 
47
            l = self.lineQ.get()
 
48
            if curr_cmd == '':
 
49
                curr_cmd = l
 
50
            else:
 
51
                curr_cmd = curr_cmd + '\n' + l
 
52
            try:
 
53
                cmd = compiler(curr_cmd)
 
54
                if cmd is None:
 
55
                    # The command was incomplete,
 
56
                    # so send back a None, so the
 
57
                    # client can print a '...'
 
58
                    self.cmdQ.put({"more":None})
 
59
                else:
 
60
                    # The command was complete,
 
61
                    # so evaluate it!
 
62
                    sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
63
                    out = cStringIO.StringIO()
 
64
                    sys.stdout = out
 
65
                    sys.stderr = out
 
66
                    signal.alarm(5)
 
67
                    res = eval(cmd, globs, locls)
 
68
                    signal.alarm(0)
 
69
                    self.cmdQ.put({"okay":(out.getvalue(),res)})
 
70
                    curr_cmd = ''
 
71
            except Exception, exc:
 
72
                signal.alarm(0)
 
73
                self.cmdQ.put({"exc":str(exc)})
 
74
                curr_cmd = ''
 
75
 
52
76
urls = (
53
77
    '/',            'index',
54
78
    '/index.html',  'index',
85
109
        web.output(file(name, "r").read())
86
110
 
87
111
class chat:
 
112
 
88
113
    def POST(self):
89
114
        inp = web.input()
90
 
        sys.stderr.write(str(inp) + "\n")
91
115
 
92
116
        # Authenticate
93
117
        digest = md5.new(inp.text + magic).digest().encode('hex')
98
122
        # Okay, so the authentication succeeded,
99
123
        # so now we have the trivial matter of actually
100
124
        # executing the python....
101
 
        do_chat(inp.text)
 
125
        lineQ.put(inp.text)
 
126
        r = cmdQ.get()
 
127
        sys.__stderr__.write(cjson.encode(r) + "\n")
 
128
        web.output(cjson.encode(r))
 
129
 
 
130
cmdQ = Queue.Queue()
 
131
lineQ = Queue.Queue()
 
132
interpThread = PythonRunner(cmdQ, lineQ)
102
133
 
103
134
if __name__ == "__main__":
104
135
    # FIXME jail!
105
136
    magic = sys.argv[2]
 
137
    interpThread.start()
106
138
    web.run(urls, globals())