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