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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: dcoles
  • Date: 2008-02-22 05:59:38 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:547
settup.py: Added nss libaries and /etc files needed for resolving hostnames.  
Made jail /tmp file world writable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
3
# usage:
4
 
#   python-console <port> <magic> [<working-dir>]
 
4
#   python-console <port> <magic>
5
5
 
6
6
import cjson
7
7
import codeop
 
8
import cStringIO
8
9
import md5
9
10
import os
10
11
import Queue
16
17
 
17
18
import common.chat
18
19
 
19
 
class Interrupt(Exception):
20
 
    def __init__(self):
21
 
        Exception.__init__(self, "Interrupted!")
22
 
 
23
20
class ExpiryTimer(object):
24
21
    def __init__(self, idle):
25
22
        self.idle = idle
26
 
        signal.signal(signal.SIGALRM, partial(self.timeout))
 
23
        signal.signal(signal.SIGALRM, partial(self.timeout,self))
27
24
 
28
25
    def ping(self):
29
26
        signal.alarm(self.idle)
43
40
        self.lineQ = lineQ
44
41
 
45
42
    def readline(self):
 
43
        # stop the clock!
46
44
        self.cmdQ.put({"input":None})
47
45
        expiry.ping()
48
46
        ln = self.lineQ.get()
49
47
        if 'chat' in ln:
50
48
            return ln['chat']
51
 
        if 'interrupt' in ln:
52
 
            raise Interrupt()
53
 
 
54
 
class StdoutToWeb(object):
55
 
    def __init__(self, cmdQ, lineQ):
56
 
        self.cmdQ = cmdQ
57
 
        self.lineQ = lineQ
58
 
        self.remainder = ''
59
 
 
60
 
    def write(self, stuff):
61
 
        self.remainder = self.remainder + stuff
62
 
 
63
 
        # if there's less than 128 bytes, buffer
64
 
        if len(self.remainder) < 128:
65
 
            return
66
 
 
67
 
        # if there's lots, then send it in 1/2K blocks
68
 
        while len(self.remainder) > 512:
69
 
            blk = self.remainder[0:512]
70
 
            self.cmdQ.put({"output":blk})
71
 
            expiry.ping()
72
 
            ln = self.lineQ.get()
73
 
            self.remainder = self.remainder[512:]
74
 
 
75
 
        # Finally, split the remainder up into lines, and ship all the
76
 
        # completed lines off to the server.
77
 
        lines = self.remainder.split("\n")
78
 
        self.remainder = lines[-1]
79
 
        del lines[-1]
80
 
 
81
 
        if len(lines) > 0:
82
 
            lines.append('')
83
 
            text = "\n".join(lines)
84
 
            self.cmdQ.put({"output":text})
85
 
            expiry.ping()
86
 
            ln = self.lineQ.get()
87
 
            if 'interrupt' in ln:
88
 
                raise Interrupt()
89
 
 
90
 
    def flush(self):
91
 
        if len(self.remainder) > 0:
92
 
            self.cmdQ.put({"output":self.remainder})
93
 
            expiry.ping()
94
 
            ln = self.lineQ.get()
95
 
            self.remainder = ''
96
 
            if 'interrupt' in ln:
97
 
                raise Interrupt()
98
 
 
99
 
class WebIO(object):
100
 
    """Provides a file like interface to the Web front end of the console.
101
 
    You may print text to the console using write(), flush any buffered output 
102
 
    using flush(), or request text from the console using readline()"""
103
 
    
104
 
    def __init__(self, cmdQ, lineQ):
105
 
        self.cmdQ = cmdQ
106
 
        self.lineQ = lineQ
107
 
        self.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
108
 
        self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
109
 
 
110
 
    def write(self, stuff):
111
 
        self.stdout.write(stuff)
112
 
 
113
 
    def flush(self):
114
 
        self.stdout.flush()
115
 
 
116
 
    def readline(self):
117
 
        self.stdout.flush()
118
 
        return self.stdin.readline()
119
49
 
120
50
class PythonRunner(Thread):
121
51
    def __init__(self, cmdQ, lineQ):
122
52
        self.cmdQ = cmdQ
123
53
        self.lineQ = lineQ
124
 
        self.webio = WebIO(self.cmdQ, self.lineQ)
 
54
        self.out = cStringIO.StringIO()
125
55
        Thread.__init__(self)
126
56
 
127
57
    def execCmd(self, cmd):
128
58
        try:
129
 
            sys.stdin = self.webio
130
 
            sys.stdout = self.webio
131
 
            sys.stderr = self.webio
132
 
            res = eval(cmd, self.globs)
133
 
            self.webio.flush()
134
 
            self.cmdQ.put({"okay":res})
 
59
            sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
60
            sys.stdout = self.out
 
61
            sys.stderr = self.out
 
62
            res = eval(cmd, self.globs, self.locls)
 
63
            self.cmdQ.put({"okay":(self.out.getvalue(),res)})
135
64
            self.curr_cmd = ''
 
65
            self.out = cStringIO.StringIO()
136
66
        except Exception, exc:
137
 
            self.webio.flush()
138
 
            exc_classname = exc.__class__.__name__
139
 
            self.cmdQ.put({"exc": exc_classname + ": " + str(exc)})
 
67
            self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
140
68
            self.curr_cmd = ''
 
69
            self.out = cStringIO.StringIO()
141
70
 
142
71
    def run(self):
143
 
        self.globs = {}
144
 
        self.globs['__builtins__'] = globals()['__builtins__']
145
 
        self.curr_cmd = ''
 
72
        self.init_state()
146
73
        compiler = codeop.CommandCompiler()
147
74
 
148
75
        while True:
162
89
                    else:
163
90
                        self.execCmd(cmd)
164
91
                except Exception, exc:
165
 
                    self.webio.flush()
166
 
                    self.cmdQ.put({"exc":str(exc)})
 
92
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
167
93
                    self.curr_cmd = ''
 
94
                    self.out = cStringIO.StringIO()
168
95
            if 'block' in ln:
169
96
                # throw away a partial command.
170
97
                try:
171
98
                    cmd = compile(ln['block'], "<web session>", 'exec');
172
99
                    self.execCmd(cmd)
173
100
                except Exception, exc:
174
 
                    self.webio.flush()
175
 
                    self.cmdQ.put({"exc":str(exc)})
 
101
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
176
102
                    self.curr_cmd = ''
 
103
                    self.out = cStringIO.StringIO()
 
104
 
 
105
    def init_state(self):
 
106
        self.globs = {}
 
107
        self.globs['__builtins__'] = globals()['__builtins__']
 
108
        self.locls = {}
 
109
        self.curr_cmd = ''
177
110
 
178
111
def daemonize():
179
112
    if os.fork():   # launch child and...
208
141
if __name__ == "__main__":
209
142
    port = int(sys.argv[1])
210
143
    magic = sys.argv[2]
211
 
    if len(sys.argv) >= 4:
212
 
        # working_dir
213
 
        os.chdir(sys.argv[3])
214
 
        # Make python's search path follow the cwd
215
 
        sys.path[0] = ''
216
 
        os.environ['HOME'] = sys.argv[3]
217
144
 
218
145
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)