~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 06:06:10 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:531
www/auth: Split authenticate.py into 3 modules: autherror and ldap_auth.
    Now authenticate looks through the list of modules in conf.auth_modules
    to figure out what to import (does not import ldap_auth directly,
    only through auth_modules).
    Error messages now contain module name.
setup.py: Renamed ldap to ldap_auth (due to naming conflict with ldap lib).

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
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
        
 
37
class StdinFromWeb(object):
 
38
    def __init__(self, cmdQ, lineQ):
 
39
        self.cmdQ = cmdQ
 
40
        self.lineQ = lineQ
 
41
 
 
42
    def readline(self):
 
43
        # stop the clock!
 
44
        self.cmdQ.put({"input":None})
 
45
        expiry.ping()
 
46
        ln = self.lineQ.get()
 
47
        if 'chat' in ln:
 
48
            return ln['chat']
 
49
 
 
50
class PythonRunner(Thread):
 
51
    def __init__(self, cmdQ, lineQ):
 
52
        self.cmdQ = cmdQ
 
53
        self.lineQ = lineQ
 
54
        self.out = cStringIO.StringIO()
 
55
        Thread.__init__(self)
 
56
 
 
57
    def execCmd(self, cmd):
 
58
        try:
 
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)})
 
64
            self.curr_cmd = ''
 
65
            self.out = cStringIO.StringIO()
 
66
        except Exception, exc:
 
67
            self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
68
            self.curr_cmd = ''
 
69
            self.out = cStringIO.StringIO()
 
70
 
 
71
    def run(self):
 
72
        self.init_state()
 
73
        compiler = codeop.CommandCompiler()
 
74
 
 
75
        while True:
 
76
            ln = self.lineQ.get()
 
77
            if 'chat' in ln:
 
78
                if self.curr_cmd == '':
 
79
                    self.curr_cmd = ln['chat']
 
80
                else:
 
81
                    self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
 
82
                try:
 
83
                    cmd = compiler(self.curr_cmd)
 
84
                    if cmd is None:
 
85
                        # The command was incomplete,
 
86
                        # so send back a None, so the
 
87
                        # client can print a '...'
 
88
                        self.cmdQ.put({"more":None})
 
89
                    else:
 
90
                        self.execCmd(cmd)
 
91
                except Exception, exc:
 
92
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
93
                    self.curr_cmd = ''
 
94
                    self.out = cStringIO.StringIO()
 
95
            if 'block' in ln:
 
96
                # throw away a partial command.
 
97
                try:
 
98
                    cmd = compile(ln['block'], "<web session>", 'exec');
 
99
                    self.execCmd(cmd)
 
100
                except Exception, exc:
 
101
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
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 = ''
 
110
 
 
111
def daemonize():
 
112
    if os.fork():   # launch child and...
 
113
        os._exit(0) # kill off parent
 
114
    os.setsid()
 
115
    if os.fork():   # launch child and...
 
116
        os._exit(0) # kill off parent again.
 
117
    os.umask(077)
 
118
 
 
119
# The global 'magic' is the secret that the client and server share
 
120
# which is used to create and md5 digest to authenticate requests.
 
121
# It is assigned a real value at startup.
 
122
magic = ''
 
123
 
 
124
cmdQ = Queue.Queue()
 
125
lineQ = Queue.Queue()
 
126
interpThread = PythonRunner(cmdQ, lineQ)
 
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
 
 
141
if __name__ == "__main__":
 
142
    port = int(sys.argv[1])
 
143
    magic = sys.argv[2]
 
144
 
 
145
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)