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

418 by mattgiuca
Renamed trunk/console to trunk/scripts. We are now able to put more scripts in
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
526 by drtomc
python-console: trivial bugfix - missing import.
16
from functools import partial
418 by mattgiuca
Renamed trunk/console to trunk/scripts. We are now able to put more scripts in
17
432 by drtomc
usrmgt: more work on this. Still some work to go.
18
import common.chat
19
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
20
class ExpiryTimer(object):
21
    def __init__(self, idle):
22
        self.idle = idle
564 by drtomc
python-console: Fix the timeout code.
23
        signal.signal(signal.SIGALRM, partial(self.timeout))
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
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
        
418 by mattgiuca
Renamed trunk/console to trunk/scripts. We are now able to put more scripts in
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})
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
45
        expiry.ping()
418 by mattgiuca
Renamed trunk/console to trunk/scripts. We are now able to put more scripts in
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
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
128
# Default expiry time of 15 minutes
129
expiry = ExpiryTimer(15 * 60)
130
432 by drtomc
usrmgt: more work on this. Still some work to go.
131
def initializer():
132
    interpThread.setDaemon(True)
133
    interpThread.start()
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
134
    expiry.ping()
432 by drtomc
usrmgt: more work on this. Still some work to go.
135
136
def dispatch_msg(msg):
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
137
    expiry.ping()
432 by drtomc
usrmgt: more work on this. Still some work to go.
138
    lineQ.put({msg['cmd']:msg['text']})
139
    return cmdQ.get()
140
418 by mattgiuca
Renamed trunk/console to trunk/scripts. We are now able to put more scripts in
141
if __name__ == "__main__":
142
    port = int(sys.argv[1])
143
    magic = sys.argv[2]
144
432 by drtomc
usrmgt: more work on this. Still some work to go.
145
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)