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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: mattgiuca
  • Date: 2008-01-11 05:11:08 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:180
util: Added function nice_filesize.

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
 
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!
 
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:
24
47
        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 = ''
 
48
        v = (None, None, str(exc))
 
49
        web.output(cjson.encode(v))
 
50
        curr_cmd = ''
78
51
 
79
52
urls = (
80
53
    '/',            'index',
112
85
        web.output(file(name, "r").read())
113
86
 
114
87
class chat:
115
 
 
116
88
    def POST(self):
117
89
        inp = web.input()
 
90
        sys.stderr.write(str(inp) + "\n")
118
91
 
119
92
        # Authenticate
120
93
        digest = md5.new(inp.text + magic).digest().encode('hex')
121
94
        if inp.digest != digest:
122
 
            web.output("401 Unauthorized")
123
95
            web.ctx.status = '401 Unauthorized'
124
96
            return
125
97
 
126
98
        # Okay, so the authentication succeeded,
127
99
        # so now we have the trivial matter of actually
128
100
        # 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)
 
101
        do_chat(inp.text)
137
102
 
138
103
if __name__ == "__main__":
 
104
    # FIXME jail!
139
105
    magic = sys.argv[2]
140
 
    interpThread.setDaemon(True)
141
 
    interpThread.start()
142
106
    web.run(urls, globals())