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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: mattgiuca
  • Date: 2008-02-21 07:08:07 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:533
auth/authenticate: Much done!
    Fixed immediate failure if user not in DB (now sets user to None to give
    it the chance to pulldown).
    Now writes the user to the DB if a user object is returned.
    Fixed simple_db_auth raising if the user is not in the DB. It now returns
    None, again, to give chance to pulldown.
    Fixed importing modules - the auth dir is not in sys.path so there are
    import errors. Did some foo (a LOT of magic) to get the auth dir in
    sys.path.

auth/ldap_auth: Fixed missing import.
auth/guest_auth: Added new auth module, guest. This is more of a toy.
    It auths guest/guest and also returns a new user which is written to the
    DB - so it's like a user pulldown module.

userdb/users.sql: Fixed rolenms allowed - these did not match caps.Role.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
import socket
14
14
import sys
15
15
from threading import Thread
16
 
 
 
16
from functools import partial
 
17
 
 
18
import common.chat
 
19
 
 
20
class ExpiryTimer(object):
 
21
    def __init__(self, idle):
 
22
        self.idle = idle
 
23
        signal.signal(signal.SIGALRM, partial(self.timeout,self))
 
24
 
 
25
    def ping(self):
 
26
        signal.alarm(self.idle)
 
27
 
 
28
    def start(self, time):
 
29
        signal.alarm(time)
 
30
 
 
31
    def stop(self):
 
32
        self.ping()
 
33
 
 
34
    def timeout(self, signum, frame):
 
35
        sys.exit(1)
 
36
        
17
37
class StdinFromWeb(object):
18
38
    def __init__(self, cmdQ, lineQ):
19
39
        self.cmdQ = cmdQ
21
41
 
22
42
    def readline(self):
23
43
        # stop the clock!
24
 
        signal.alarm(0)
25
44
        self.cmdQ.put({"input":None})
 
45
        expiry.ping()
26
46
        ln = self.lineQ.get()
27
47
        if 'chat' in ln:
28
 
            # restart the clock:
29
 
            # Some of our 5 seconds may have elapsed, but never mind.
30
 
            signal.alarm(5)
31
48
            return ln['chat']
32
49
 
33
50
class PythonRunner(Thread):
42
59
            sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
43
60
            sys.stdout = self.out
44
61
            sys.stderr = self.out
45
 
            signal.alarm(5)
46
62
            res = eval(cmd, self.globs, self.locls)
47
 
            signal.alarm(0)
48
63
            self.cmdQ.put({"okay":(self.out.getvalue(),res)})
49
64
            self.curr_cmd = ''
50
65
            self.out = cStringIO.StringIO()
51
66
        except Exception, exc:
52
 
            signal.alarm(0)
53
67
            self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
54
68
            self.curr_cmd = ''
55
69
            self.out = cStringIO.StringIO()
75
89
                    else:
76
90
                        self.execCmd(cmd)
77
91
                except Exception, exc:
78
 
                    signal.alarm(0)
79
92
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
80
93
                    self.curr_cmd = ''
81
94
                    self.out = cStringIO.StringIO()
85
98
                    cmd = compile(ln['block'], "<web session>", 'exec');
86
99
                    self.execCmd(cmd)
87
100
                except Exception, exc:
88
 
                    signal.alarm(0)
89
101
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
90
102
                    self.curr_cmd = ''
91
103
                    self.out = cStringIO.StringIO()
113
125
lineQ = Queue.Queue()
114
126
interpThread = PythonRunner(cmdQ, lineQ)
115
127
 
 
128
# Default expiry time of 15 minutes
 
129
expiry = ExpiryTimer(15 * 60)
 
130
 
 
131
def initializer():
 
132
    interpThread.setDaemon(True)
 
133
    interpThread.start()
 
134
    expiry.ping()
 
135
 
 
136
def dispatch_msg(msg):
 
137
    expiry.ping()
 
138
    lineQ.put({msg['cmd']:msg['text']})
 
139
    return cmdQ.get()
 
140
 
116
141
if __name__ == "__main__":
117
142
    port = int(sys.argv[1])
118
143
    magic = sys.argv[2]
119
144
 
120
 
    # Attempt to open the socket.
121
 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
122
 
    s.bind(('', port))
123
 
    s.listen(1)
124
 
 
125
 
    # Excellent! It worked. Let's turn ourself into a daemon,
126
 
    # then get on with the job of being a python interpreter.
127
 
    daemonize()
128
 
 
129
 
    interpThread.setDaemon(True)
130
 
    interpThread.start()
131
 
 
132
 
    while True:
133
 
        (conn, addr) = s.accept()
134
 
        try:
135
 
            # Grab the input
136
 
            buf = cStringIO.StringIO()
137
 
            blk = conn.recv(1024)
138
 
            while blk:
139
 
                buf.write(blk)
140
 
                try:
141
 
                    blk = conn.recv(1024, socket.MSG_DONTWAIT)
142
 
                except:
143
 
                    # Exception thrown if it WOULD block (but we
144
 
                    # told it not to wait) - ie. we are done
145
 
                    blk = None
146
 
            inp = buf.getvalue()
147
 
            msg = cjson.decode(inp)
148
 
            
149
 
            # Check that the message is 
150
 
            digest = md5.new(msg['text'] + magic).digest().encode('hex')
151
 
            if msg['digest'] != digest:
152
 
                conn.close()
153
 
                continue
154
 
 
155
 
            lineQ.put({msg['cmd']:msg['text']})
156
 
            r = cmdQ.get()
157
 
            conn.sendall(cjson.encode(r))
158
 
            conn.close()
159
 
        except Exception, e:
160
 
            conn.close()
 
145
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)