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

1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
1
# IVLE
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Module: Console
19
# Author: Matt Giuca, Tom Conway, David Coles (refactor)
20
# Date: 13/8/2008
21
22
# Mainly refactored out of consoleservice
23
24
import errno
25
import cPickle
26
import md5
27
import os
28
import random
29
import socket
30
import StringIO
31
import uuid
32
33
import cjson
34
35
import ivle.conf
1212 by William Grant
Remove unused ivle.util imports.
36
from ivle import chat
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
37
38
# Outside Jail
39
trampoline_path = os.path.join(ivle.conf.lib_path, "trampoline")
40
# Inside Jail
41
python_path = "/usr/bin/python"
42
console_dir = os.path.join(ivle.conf.share_path, 'services')
43
console_path = os.path.join(console_dir, 'python-console')
44
45
class ConsoleError(Exception):
46
    """ The console failed in some way. This is bad. """
47
    def __init__(self, value):
48
        self.value = value
49
    def __str__(self):
50
        return repr(self.value)
51
52
class ConsoleException(Exception):
53
    """ The code being exectuted on the console returned an exception. 
54
    """
55
    def __init__(self, value):
56
        self.value = value
57
    def __str__(self):
58
        return repr(self.value)
59
60
class TruncateStringIO(StringIO.StringIO):
61
    """ A class that wraps around StringIO and truncates the buffer when the 
62
    contents are read (except for when using getvalue).
63
    """
64
    def __init__(self, buffer=None):
65
        StringIO.StringIO.__init__(self, buffer)
66
    
67
    def read(self, n=-1):
68
        """ Read at most size bytes from the file (less if the read hits EOF 
69
        before obtaining size bytes).
70
71
        If the size argument is negative or omitted, read all data until EOF is      
72
        reached. The bytes are returned as a string object. An empty string is 
73
        returned when EOF is encountered immediately.
74
75
        Truncates the buffer.
76
        """
77
78
        self.seek(0)
79
        res = StringIO.StringIO.read(self, n)
80
        self.truncate(0)
81
        return res
82
83
    def readline(self, length=None):
84
        """ Read one entire line from the file.
85
 
86
        A trailing newline character is kept in the string (but may be absent 
87
        when a file ends with an incomplete line). If the size argument is   
88
        present and non-negative, it is a maximum byte count (including the      
89
        trailing newline) and an incomplete line may be returned.
90
91
        An empty string is returned only when EOF is encountered immediately.
92
        
93
        Note: Unlike stdio's fgets(), the returned string contains null   
94
        characters ('\0') if they occurred in the input.
95
96
        Removes the line from the buffer.
97
        """
98
99
        self.seek(0)
100
        res = StringIO.StringIO.readline(self, length)
101
        rest = StringIO.StringIO.read(self)
102
        self.truncate(0)
103
        self.write(rest)
104
        return res
105
106
    def readlines(self, sizehint=0):
107
        """ Read until EOF using readline() and return a list containing the        
108
        lines thus read.
109
        
110
        If the optional sizehint argument is present, instead of reading up to 
111
        EOF, whole lines totalling approximately sizehint bytes (or more to      
112
        accommodate a final whole line).
113
114
        Truncates the buffer.
115
        """
116
117
        self.seek(0)
118
        res = StringIO.StringIO.readlines(self, length)
119
        self.truncate(0)
120
        return res
121
122
class Console(object):
123
    """ Provides a nice python interface to the console
124
    """
125
    def __init__(self, uid, jail_path, working_dir):
126
        """Starts up a console service for user uid, inside chroot jail 
127
        jail_path with work directory of working_dir
128
        """
129
        super(Console, self).__init__()
130
131
        self.uid = uid
132
        self.jail_path = jail_path
133
        self.working_dir = working_dir
134
135
        # Set up the buffers
136
        self.stdin = TruncateStringIO()
137
        self.stdout = TruncateStringIO()
138
        self.stderr = TruncateStringIO()
139
140
        # Fire up the console
141
        self.restart()
142
143
    def restart(self):
144
        # Empty all the buffers
145
        self.stdin.truncate(0)
146
        self.stdout.truncate(0)
147
        self.stderr.truncate(0)
148
149
        # TODO: Check if we are already running a console. If we are shut it 
150
        # down first.
151
152
        # TODO: Figure out the host name the console server is running on.
153
        self.host = socket.gethostname()
154
155
        # Create magic
156
        # TODO
157
        self.magic = md5.new(uuid.uuid4().bytes).digest().encode('hex')
158
159
        # Try to find a free port on the server.
160
        # Just try some random ports in the range [3000,8000)
161
        # until we either succeed, or give up. If you think this
162
        # sounds risky, it isn't:
163
        # For N ports (e.g. 5000) with k (e.g. 100) in use, the
164
        # probability of failing to find a free port in t (e.g. 5) tries
165
        # is (k / N) ** t (e.g. 3.2*10e-9).
166
167
        tries = 0
168
        while tries < 5:
169
            self.port = int(random.uniform(3000, 8000))
170
171
            # Start the console server (port, magic)
172
            # trampoline usage: tramp uid jail_dir working_dir script_path args
173
            # console usage:    python-console port magic
174
            res = os.spawnv(os.P_WAIT, trampoline_path, [
175
                trampoline_path,
176
                str(self.uid),
1092.1.49 by William Grant
trampoline now takes the jail paths in argv.
177
                ivle.conf.jail_base,
178
                ivle.conf.jail_src_base,
179
                ivle.conf.jail_system,
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
180
                self.jail_path,
181
                console_dir,
182
                python_path,
183
                console_path,
184
                str(self.port),
185
                str(self.magic),
186
                self.working_dir
187
                ])
188
189
            if res == 0:
190
                # success
191
                break;
192
193
            tries += 1
194
195
        # If we can't start the console after 5 attemps (can't find a free port 
196
        # during random probing, syntax errors, segfaults) throw an exception.
197
        if tries == 5:
198
            raise ConsoleError("Unable to start console service!")
199
200
    def __chat(self, cmd, args):
201
        """ A wrapper around chat.chat to comunicate directly with the 
202
        console.
203
        """
204
        try:
205
            response = chat.chat(self.host, self.port,
206
                {'cmd': cmd, 'text': args}, self.magic)
207
        except socket.error, (enumber, estring):
208
            if enumber == errno.ECONNREFUSED:
209
                # Timeout
210
                raise ConsoleError(
211
                    "Could not establish a connection to the python console")
212
            else:
213
                # Some other error - probably serious
214
                raise socket.error, (enumber, estring)
215
        except cjson.DecodeError:
216
            # Couldn't decode the JSON
217
            raise ConsoleError(
218
                "Could not understand the python console response")
219
220
        return response
221
222
    def __handle_chat(self, cmd, args):
223
        """ A wrapper around self.__chat that handles all the messy responses 
224
        of chat for higher level interfaces such as inspect
225
        """
226
        # Do the request
227
        response = self.__chat(cmd, args)
228
229
        # Process I/O requests
230
        while 'output' in response or 'input' in response:
231
            if 'output' in response:
232
                self.stdout.write(response['output'])
233
                response = self.chat()
234
            elif 'input' in response:
235
                response = self.chat(self.stdin.readline())
236
237
        # Process user exceptions
238
        if 'exc' in response:
239
            raise ConsoleException(response['exc'])
240
241
        return response
242
243
    def chat(self, code=''):
244
        """ Executes a partial block of code """
245
        return self.__chat('chat', code)
246
247
    def block(self, code):
248
        """ Executes a block of code and returns the output """
249
        block = self.__handle_chat('block', code)
250
        if 'output' in block:
251
            return block['output']
252
        elif 'okay' in block:
253
            return
254
        else:
255
            raise ConsoleException("Bad response from console: %s"%str(block))
256
257
    def globals(self, globs=None):
258
        """ Returns a dictionary of the console's globals and optionally set 
259
        them to a new value
260
        """
261
        # Pickle the globals
262
        pickled_globs = None
263
        if globs is not None:
264
            pickled_globs = {}
265
            for g in globs:
266
                pickled_globs[g] = cPickle.dumps(globs[g])
267
268
        globals = self.__handle_chat('globals', pickled_globs)
269
270
        # Unpickle the globals
271
        for g in globals['globals']:
272
            globals['globals'][g] = cPickle.loads(globals['globals'][g])
273
274
        return globals['globals']
275
        
276
277
    def call(self, function, *args, **kwargs):
278
        """ Calls a function in the python console. Can take in a list of 
279
        repr() args and dictionary of repr() values kwargs. These will be 
280
        evaluated on the server side.
281
        """
282
        call_args = {
283
            'function': function,
284
            'args': args,
285
            'kwargs': kwargs}
286
        call = self.__handle_chat('call', call_args)
287
288
        # Unpickle any exceptions
289
        if 'exception' in call:
290
            call['exception']['except'] = \
291
                cPickle.loads(call['exception']['except'])
292
293
        return call
294
295
    def execute(self, code=''):
296
        """ Runs a block of code in the python console.
297
        If an exception was thrown then returns an exception object.
298
        """
299
        execute = self.__handle_chat('execute', code)
300
              
301
        # Unpickle any exceptions
302
        if 'exception' in execute:
303
            return cPickle.loads(execute['exception'])
304
        else:
305
            return execute
306
307
308
    def set_vars(self, variables):
309
        """ Takes a dictionary of varibles to add to the console's global 
310
        space. These are evaluated in the local space so you can't use this to 
311
        set a varible to a value to be calculated on the console side.
312
        """
313
        vars = {}
314
        for v in variables:
315
            vars[v] = repr(variables[v])
316
317
        set_vars = self.__handle_chat('set_vars', vars)
318
319
        if set_vars.get('response') != 'okay':
320
            raise ConsoleError("Could not set variables")
321
322
    def close(self):
323
        """ Causes the console process to terminate """
324
        return self.__chat('terminate', None)
325
    
326
class ExistingConsole(Console):
327
    """ Provides a nice python interface to an existing console.
328
    Note: You can't restart an existing console since there is no way to infer 
329
    all the starting parameters. Just start a new Console instead.
330
    """
331
    def __init__(self, host, port, magic):
332
        self.host = host
333
        self.port = port
334
        self.magic = magic
335
336
        # Set up the buffers
337
        self.stdin = TruncateStringIO()
338
        self.stdout = TruncateStringIO()
339
        self.stderr = TruncateStringIO()
340
341
    def restart():
342
        raise NotImplementedError('You can not restart an existing console')
343