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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: mattgiuca
  • Date: 2007-12-20 05:25:03 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:103
Fix to Makefile.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# usage:
4
4
#   python-console <port> <magic>
5
5
 
6
 
import sys
7
6
import web
8
7
import md5
9
8
import codeop
10
9
import cjson
11
 
import cgi
12
 
import cStringIO
13
 
import signal
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!
24
 
        signal.alarm(0)
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
 
        self.init_state()
41
 
        compiler = codeop.CommandCompiler()
42
 
 
43
 
        while True:
44
 
            l = self.lineQ.get()
45
 
            if self.curr_cmd == '':
46
 
                self.curr_cmd = l
47
 
            else:
48
 
                self.curr_cmd = self.curr_cmd + '\n' + l
49
 
            try:
50
 
                cmd = compiler(self.curr_cmd)
51
 
                if cmd is None:
52
 
                    # The command was incomplete,
53
 
                    # so send back a None, so the
54
 
                    # client can print a '...'
55
 
                    self.cmdQ.put({"more":None})
56
 
                else:
57
 
                    # The command was complete,
58
 
                    # so evaluate it!
59
 
                    sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
60
 
                    out = cStringIO.StringIO()
61
 
                    sys.stdout = out
62
 
                    sys.stderr = out
63
 
                    signal.alarm(5)
64
 
                    res = eval(cmd, globs, locls)
65
 
                    signal.alarm(0)
66
 
                    self.cmdQ.put({"okay":(out.getvalue(),res)})
67
 
                    curr_cmd = ''
68
 
            except Exception, exc:
69
 
                signal.alarm(0)
70
 
                self.cmdQ.put({"exc":str(exc)})
71
 
                curr_cmd = ''
72
 
 
73
 
    def init_state(self):
74
 
        self.globs = {}
75
 
        self.globs['__builtins__'] = globals()['__builtins__']
76
 
        self.locls = {}
77
 
        self.curr_cmd = ''
 
10
 
 
11
globs = {}
 
12
globs['__builtins__'] = globals()['__builtins__']
 
13
locls = {}
 
14
compiler = codeop.CommandCompiler()
 
15
curr_cmd = ''
 
16
 
 
17
def do_chat(txt):
 
18
    if curr_cmd == '':
 
19
        curr_cmd = txt
 
20
    else:
 
21
        curr_cmd = curr_cmd + '\n' + txt
 
22
    try:
 
23
        cmd = compiler(curr_cmd)
 
24
        if cmd is None:
 
25
            # The command was incomplete,
 
26
            # so send back a None, so the
 
27
            # client can print a '...'
 
28
            web.output(cjson.encode(None))
 
29
        else:
 
30
            # The command was complete,
 
31
            # so evaluate it!
 
32
            out = cStringIO.StringIO()
 
33
            sys.stdout = out
 
34
            sys.stderr = out
 
35
            signal.alarm(5)
 
36
            res = eval(cmd, globs, locls)
 
37
            signal.alarm(0)
 
38
            v = (out.getvalue(), res, None)
 
39
            web.output(json.encode(v))
 
40
            curr_cmd = ''
 
41
    except Exception, exc:
 
42
        v = (None, None, str(exc))
 
43
        web.output(json.encode(v))
 
44
        curr_cmd = ''
78
45
 
79
46
urls = (
80
47
    '/',            'index',
81
 
    '/index.html',  'index',
82
48
    '/(.*\.js)',    'jscript',
83
49
    '/(.*\.css)',   'style',
84
50
    '/chat',        'chat')
112
78
        web.output(file(name, "r").read())
113
79
 
114
80
class chat:
115
 
 
116
81
    def POST(self):
117
82
        inp = web.input()
118
83
 
119
84
        # Authenticate
120
85
        digest = md5.new(inp.text + magic).digest().encode('hex')
121
86
        if inp.digest != digest:
122
 
            web.output("401 Unauthorized")
123
87
            web.ctx.status = '401 Unauthorized'
124
88
            return
125
89
 
126
90
        # Okay, so the authentication succeeded,
127
91
        # so now we have the trivial matter of actually
128
92
        # executing the python....
129
 
        lineQ.put(inp.text)
130
 
        r = cmdQ.get()
131
 
        sys.__stderr__.write(cjson.encode(r) + "\n")
132
 
        web.output(cjson.encode(r))
133
 
 
134
 
cmdQ = Queue.Queue()
135
 
lineQ = Queue.Queue()
136
 
interpThread = PythonRunner(cmdQ, lineQ)
 
93
        web.output(do_chat(inp.text))
137
94
 
138
95
if __name__ == "__main__":
 
96
    # FIXME jail!
139
97
    magic = sys.argv[2]
140
 
    interpThread.setDaemon(True)
141
 
    interpThread.start()
142
98
    web.run(urls, globals())