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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: mattgiuca
  • Date: 2008-02-14 00:49:06 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:450
Set svn:ignore on a a few more things.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# usage:
 
4
#   python-console <port> <magic>
 
5
 
 
6
import cjson
 
7
import codeop
 
8
import cStringIO
 
9
import md5
 
10
import os
 
11
import Queue
 
12
import signal
 
13
import socket
 
14
import sys
 
15
from threading import Thread
 
16
 
 
17
import common.chat
 
18
 
 
19
class StdinFromWeb(object):
 
20
    def __init__(self, cmdQ, lineQ):
 
21
        self.cmdQ = cmdQ
 
22
        self.lineQ = lineQ
 
23
 
 
24
    def readline(self):
 
25
        # stop the clock!
 
26
        signal.alarm(0)
 
27
        self.cmdQ.put({"input":None})
 
28
        ln = self.lineQ.get()
 
29
        if 'chat' in ln:
 
30
            # restart the clock:
 
31
            # Some of our 5 seconds may have elapsed, but never mind.
 
32
            signal.alarm(5)
 
33
            return ln['chat']
 
34
 
 
35
class PythonRunner(Thread):
 
36
    def __init__(self, cmdQ, lineQ):
 
37
        self.cmdQ = cmdQ
 
38
        self.lineQ = lineQ
 
39
        self.out = cStringIO.StringIO()
 
40
        Thread.__init__(self)
 
41
 
 
42
    def execCmd(self, cmd):
 
43
        try:
 
44
            sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
45
            sys.stdout = self.out
 
46
            sys.stderr = self.out
 
47
            signal.alarm(5)
 
48
            res = eval(cmd, self.globs, self.locls)
 
49
            signal.alarm(0)
 
50
            self.cmdQ.put({"okay":(self.out.getvalue(),res)})
 
51
            self.curr_cmd = ''
 
52
            self.out = cStringIO.StringIO()
 
53
        except Exception, exc:
 
54
            signal.alarm(0)
 
55
            self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
56
            self.curr_cmd = ''
 
57
            self.out = cStringIO.StringIO()
 
58
 
 
59
    def run(self):
 
60
        self.init_state()
 
61
        compiler = codeop.CommandCompiler()
 
62
 
 
63
        while True:
 
64
            ln = self.lineQ.get()
 
65
            if 'chat' in ln:
 
66
                if self.curr_cmd == '':
 
67
                    self.curr_cmd = ln['chat']
 
68
                else:
 
69
                    self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
 
70
                try:
 
71
                    cmd = compiler(self.curr_cmd)
 
72
                    if cmd is None:
 
73
                        # The command was incomplete,
 
74
                        # so send back a None, so the
 
75
                        # client can print a '...'
 
76
                        self.cmdQ.put({"more":None})
 
77
                    else:
 
78
                        self.execCmd(cmd)
 
79
                except Exception, exc:
 
80
                    signal.alarm(0)
 
81
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
82
                    self.curr_cmd = ''
 
83
                    self.out = cStringIO.StringIO()
 
84
            if 'block' in ln:
 
85
                # throw away a partial command.
 
86
                try:
 
87
                    cmd = compile(ln['block'], "<web session>", 'exec');
 
88
                    self.execCmd(cmd)
 
89
                except Exception, exc:
 
90
                    signal.alarm(0)
 
91
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
92
                    self.curr_cmd = ''
 
93
                    self.out = cStringIO.StringIO()
 
94
 
 
95
    def init_state(self):
 
96
        self.globs = {}
 
97
        self.globs['__builtins__'] = globals()['__builtins__']
 
98
        self.locls = {}
 
99
        self.curr_cmd = ''
 
100
 
 
101
def daemonize():
 
102
    if os.fork():   # launch child and...
 
103
        os._exit(0) # kill off parent
 
104
    os.setsid()
 
105
    if os.fork():   # launch child and...
 
106
        os._exit(0) # kill off parent again.
 
107
    os.umask(077)
 
108
 
 
109
# The global 'magic' is the secret that the client and server share
 
110
# which is used to create and md5 digest to authenticate requests.
 
111
# It is assigned a real value at startup.
 
112
magic = ''
 
113
 
 
114
cmdQ = Queue.Queue()
 
115
lineQ = Queue.Queue()
 
116
interpThread = PythonRunner(cmdQ, lineQ)
 
117
 
 
118
def initializer():
 
119
    interpThread.setDaemon(True)
 
120
    interpThread.start()
 
121
 
 
122
def dispatch_msg(msg):
 
123
    lineQ.put({msg['cmd']:msg['text']})
 
124
    return cmdQ.get()
 
125
 
 
126
if __name__ == "__main__":
 
127
    port = int(sys.argv[1])
 
128
    magic = sys.argv[2]
 
129
 
 
130
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)