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

« back to all changes in this revision

Viewing changes to lib/fileservice_lib/action.py

  • Committer: mattgiuca
  • Date: 2008-02-14 00:48:06 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:449
php/phpBB3/cache: Set svn:ignore on all the php files here, since they are
auto-generated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: File Service / Action
 
19
# Author: Matt Giuca
 
20
# Date: 10/1/2008
 
21
 
 
22
# Handles actions requested by the client as part of the 2-stage process of
 
23
# fileservice (the second part being the return listing).
 
24
 
 
25
### Actions ###
 
26
 
 
27
# The most important argument is "action". This determines which action is
 
28
# taken. Note that action, and all other arguments, are ignored unless the
 
29
# request is a POST request. The other arguments depend upon the action.
 
30
# Note that paths are often specified as arguments. Paths that begin with a
 
31
# slash are taken relative to the user's home directory (the top-level
 
32
# directory seen when fileservice has no arguments or path). Paths without a
 
33
# slash are taken relative to the specified path.
 
34
 
 
35
# action=remove: Delete a file(s) or directory(s) (recursively).
 
36
#       path:   The path to the file or directory to delete. Can be specified
 
37
#               multiple times.
 
38
#
 
39
# action=move: Move or rename a file or directory.
 
40
#       from:   The path to the file or directory to be renamed.
 
41
#       to:     The path of the target filename. Error if the file already
 
42
#               exists.
 
43
#
 
44
# action=putfile: Upload a file to the student workspace, and optionally
 
45
#               accept zip files which will be unpacked.
 
46
#       path:   The path to the file to be written. If it exists, will
 
47
#               overwrite. Error if the target file is a directory.
 
48
#       data:   Bytes to be written to the file verbatim. May either be
 
49
#               a string variable or a file upload.
 
50
#       unpack: Optional. If "true", and the data is a valid ZIP file,
 
51
#               will create a directory instead and unpack the ZIP file
 
52
#               into it.
 
53
#
 
54
# action=putfiles: Upload multiple files to the student workspace, and
 
55
#                 optionally accept zip files which will be unpacked.
 
56
#       path:   The path to the DIRECTORY to place files in. Must not be a
 
57
#               file.
 
58
#       data:   A file upload (may not be a simple string). The filename
 
59
#               will be used to determine the target filename within
 
60
#               the given path.
 
61
#       unpack: Optional. If "true", if any data is a valid ZIP file,
 
62
#               will create a directory instead and unpack the ZIP file
 
63
#               into it.
 
64
#
 
65
# The differences between putfile and putfiles are:
 
66
# * putfile can only accept a single file.
 
67
# * putfile can accept string data, doesn't have to be a file upload.
 
68
# * putfile ignores the upload filename, the entire filename is specified on
 
69
#       path. putfiles calls files after the name on the user's machine.
 
70
#
 
71
# Clipboard-based actions. Cut/copy/paste work in the same way as modern
 
72
# file browsers, by keeping a server-side clipboard of files that have been
 
73
# cut and copied. The clipboard is stored in the session data, so it persists
 
74
# across navigation, tabs and browser windows, but not across browser
 
75
# sessions.
 
76
 
77
# action=copy: Write file(s) to the session-based clipboard. Overrides any
 
78
#               existing clipboard data. Does not actually copy the file.
 
79
#               The files are physically copied when the clipboard is pasted.
 
80
#       path:   The path to the file or directory to copy. Can be specified
 
81
#               multiple times.
 
82
 
83
# action=cut: Write file(s) to the session-based clipboard. Overrides any
 
84
#               existing clipboard data. Does not actually move the file.
 
85
#               The files are physically moved when the clipboard is pasted.
 
86
#       path:   The path to the file or directory to cut. Can be specified
 
87
#               multiple times.
 
88
 
89
# action=paste: Copy or move the files stored in the clipboard. Clears the
 
90
#               clipboard. The files are copied or moved to a specified dir.
 
91
#       path:   The path to the DIRECTORY to paste the files to. Must not
 
92
#               be a file.
 
93
#
 
94
# Subversion actions.
 
95
# action=svnadd: Add an existing file(s) to version control.
 
96
#       path:   The path to the file to be added. Can be specified multiple
 
97
#               times.
 
98
#
 
99
# action=svnrevert: Revert a file(s) to its state as of the current revision
 
100
#               / undo local edits.
 
101
#       path:   The path to the file to be reverted. Can be specified multiple
 
102
#               times.
 
103
#
 
104
# action=svnupdate: Bring a file up to date with the head revision.
 
105
#       path:   The path to the file to be updated. Only one file may be
 
106
#               specified.
 
107
#
 
108
# action=svnpublish: Set the "published" flag on a file to True.
 
109
#       path:   The path to the file to be published. Can be specified
 
110
#               multiple times.
 
111
#
 
112
# action=svnunpublish: Set the "published" flag on a file to False.
 
113
#       path:   The path to the file to be unpublished. Can be specified
 
114
#               multiple times.
 
115
#
 
116
# action=svncommit: Commit a file(s) or directory(s) to the repository.
 
117
#       path:   The path to the file or directory to be committed. Can be
 
118
#               specified multiple times. Directories are committed
 
119
#               recursively.
 
120
#       logmsg: Text of the log message. Optional. There is a default log
 
121
#               message if unspecified.
 
122
 
123
# TODO: Implement the following actions:
 
124
#   putfiles, svnrevert, svnupdate, svncommit
 
125
# TODO: Implement ZIP unpacking in putfile and putfiles.
 
126
# TODO: svnupdate needs a digest to tell the user the files that were updated.
 
127
#   This can be implemented by some message passing between action and
 
128
#   listing, and having the digest included in the listing. (Problem if
 
129
#   the listing is not a directory, but we could make it an error to do an
 
130
#   update if the path is not a directory).
 
131
 
 
132
import os
 
133
import cStringIO
 
134
import shutil
 
135
 
 
136
import pysvn
 
137
 
 
138
from common import (util, studpath, zip)
 
139
 
 
140
# Make a Subversion client object
 
141
svnclient = pysvn.Client()
 
142
 
 
143
DEFAULT_LOGMESSAGE = "No log message supplied."
 
144
 
 
145
# Mime types
 
146
# application/json is the "best" content type but is not good for
 
147
# debugging because Firefox just tries to download it
 
148
mime_dirlisting = "text/html"
 
149
#mime_dirlisting = "application/json"
 
150
 
 
151
class ActionError(Exception):
 
152
    """Represents an error processing an action. This can be
 
153
    raised by any of the action functions, and will be caught
 
154
    by the top-level handler, put into the HTTP response field,
 
155
    and continue.
 
156
 
 
157
    Important Security Consideration: The message passed to this
 
158
    exception will be relayed to the client.
 
159
    """
 
160
    pass
 
161
 
 
162
def handle_action(req, action, fields):
 
163
    """Perform the "action" part of the response.
 
164
    This function should only be called if the response is a POST.
 
165
    This performs the action's side-effect on the server. If unsuccessful,
 
166
    writes the X-IVLE-Action-Error header to the request object. Otherwise,
 
167
    does not touch the request object. Does NOT write any bytes in response.
 
168
 
 
169
    May throw an ActionError. The caller should put this string into the
 
170
    X-IVLE-Action-Error header, and then continue normally.
 
171
 
 
172
    action: String, the action requested. Not sanitised.
 
173
    fields: FieldStorage object containing all arguments passed.
 
174
    """
 
175
    global actions_table        # Table of function objects
 
176
    try:
 
177
        action = actions_table[action]
 
178
    except KeyError:
 
179
        # Default, just send an error but then continue
 
180
        raise ActionError("Unknown action")
 
181
    action(req, fields)
 
182
 
 
183
def actionpath_to_urlpath(req, path):
 
184
    """Determines the URL path (relative to the student home) upon which the
 
185
    action is intended to act. See actionpath_to_local.
 
186
    """
 
187
    if path is None:
 
188
        return req.path
 
189
    elif len(path) > 0 and path[0] == os.sep:
 
190
        # Relative to student home
 
191
        return path[1:]
 
192
    else:
 
193
        # Relative to req.path
 
194
        return os.path.join(req.path, path)
 
195
 
 
196
def actionpath_to_local(req, path):
 
197
    """Determines the local path upon which an action is intended to act.
 
198
    Note that fileservice actions accept two paths: the request path,
 
199
    and the "path" argument given to the action.
 
200
    According to the rules, if the "path" argument begins with a '/' it is
 
201
    relative to the user's home; if it does not, it is relative to the
 
202
    supplied path.
 
203
 
 
204
    This resolves the path, given the request and path argument.
 
205
 
 
206
    May raise an ActionError("Invalid path"). The caller is expected to
 
207
    let this fall through to the top-level handler, where it will be
 
208
    put into the HTTP response field. Never returns None.
 
209
 
 
210
    Does not mutate req.
 
211
    """
 
212
    (_, _, r) = studpath.url_to_jailpaths(actionpath_to_urlpath(req, path))
 
213
    if r is None:
 
214
        raise ActionError("Invalid path")
 
215
    return r
 
216
 
 
217
def movefile(req, frompath, topath, copy=False):
 
218
    """Performs a file move, resolving filenames, checking for any errors,
 
219
    and throwing ActionErrors if necessary. Can also be used to do a copy
 
220
    operation instead.
 
221
 
 
222
    frompath and topath are straight paths from the client. Will be checked.
 
223
    """
 
224
    # TODO: Do an SVN mv if the file is versioned.
 
225
    # TODO: Disallow tampering with student's home directory
 
226
    if frompath is None or topath is None:
 
227
        raise ActionError("Required field missing")
 
228
    frompath = actionpath_to_local(req, frompath)
 
229
    topath = actionpath_to_local(req, topath)
 
230
    if not os.path.exists(frompath):
 
231
        raise ActionError("The source file does not exist")
 
232
    if os.path.exists(topath):
 
233
        if frompath == topath:
 
234
            raise ActionError("Source and destination are the same")
 
235
        raise ActionError("Another file already exists with that name")
 
236
 
 
237
    try:
 
238
        if copy:
 
239
            if os.path.isdir(frompath):
 
240
                shutil.copytree(frompath, topath)
 
241
            else:
 
242
                shutil.copy2(frompath, topath)
 
243
        else:
 
244
            shutil.move(frompath, topath)
 
245
    except OSError:
 
246
        raise ActionError("Could not move the file specified")
 
247
    except shutil.Error:
 
248
        raise ActionError("Could not move the file specified")
 
249
 
 
250
### ACTIONS ###
 
251
 
 
252
def action_remove(req, fields):
 
253
    # TODO: Do an SVN rm if the file is versioned.
 
254
    # TODO: Disallow removal of student's home directory
 
255
    """Removes a list of files or directories.
 
256
 
 
257
    Reads fields: 'path' (multiple)
 
258
    """
 
259
    paths = fields.getlist('path')
 
260
    goterror = False
 
261
    for path in paths:
 
262
        path = actionpath_to_local(req, path)
 
263
        try:
 
264
            if os.path.isdir(path):
 
265
                shutil.rmtree(path)
 
266
            else:
 
267
                os.remove(path)
 
268
        except OSError:
 
269
            goterror = True
 
270
        except shutil.Error:
 
271
            goterror = True
 
272
    if goterror:
 
273
        if len(paths) == 1:
 
274
            raise ActionError("Could not delete the file specified")
 
275
        else:
 
276
            raise ActionError(
 
277
                "Could not delete one or more of the files specified")
 
278
 
 
279
def action_move(req, fields):
 
280
    # TODO: Do an SVN mv if the file is versioned.
 
281
    # TODO: Disallow tampering with student's home directory
 
282
    """Removes a list of files or directories.
 
283
 
 
284
    Reads fields: 'from', 'to'
 
285
    """
 
286
    frompath = fields.getfirst('from')
 
287
    topath = fields.getfirst('to')
 
288
    movefile(req, frompath, topath)
 
289
 
 
290
def action_putfile(req, fields):
 
291
    """Writes data to a file, overwriting it if it exists and creating it if
 
292
    it doesn't.
 
293
 
 
294
    Reads fields: 'path', 'data' (file upload)
 
295
    """
 
296
    # TODO: Read field "unpack".
 
297
    # Important: Data is "None" if the file submitted is empty.
 
298
    path = fields.getfirst('path')
 
299
    data = fields.getfirst('data')
 
300
    if path is None or data is None:
 
301
        raise ActionError("Required field missing")
 
302
    path = actionpath_to_local(req, path)
 
303
 
 
304
    if data is not None:
 
305
        data = cStringIO.StringIO(data)
 
306
 
 
307
    # Copy the contents of file object 'data' to the path 'path'
 
308
    try:
 
309
        dest = open(path, 'wb')
 
310
        if data is not None:
 
311
            shutil.copyfileobj(data, dest)
 
312
    except OSError:
 
313
        raise ActionError("Could not write to target file")
 
314
 
 
315
def action_putfiles(req, fields):
 
316
    """Writes data to one or more files in a directory, overwriting them if
 
317
    it they exist.
 
318
 
 
319
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
 
320
    """
 
321
 
 
322
    # Important: Data is "None" if the file submitted is empty.
 
323
    path = fields.getfirst('path')
 
324
    data = fields['data']
 
325
    if type(data) != type([]):
 
326
        data = [data]
 
327
    unpack = fields.getfirst('unpack')
 
328
    if unpack is None:
 
329
        unpack = False
 
330
    else:
 
331
        unpack = True
 
332
    if path is None:
 
333
        raise ActionError("Required field missing")
 
334
    path = actionpath_to_urlpath(req, path)
 
335
    goterror = False
 
336
 
 
337
 
 
338
    for datum in data:
 
339
        # Each of the uploaded files
 
340
        filepath = os.path.join(path, datum.filename)
 
341
        filedata = datum.value
 
342
 
 
343
        if unpack and datum.filename.lower().endswith(".zip"):
 
344
            # A zip file - unpack it instead of just copying
 
345
            # TODO: Use the magic number instead of file extension
 
346
            # Note: Just unzip into the current directory (ignore the
 
347
            # filename)
 
348
            try:
 
349
                zip.unzip(path, filedata)
 
350
            except (OSError, IOError):
 
351
                goterror = True
 
352
        else:
 
353
            # Not a zip file
 
354
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
 
355
            if filepath_local is None:
 
356
                raise ActionError("Invalid path")
 
357
 
 
358
            # Copy the contents of file object 'data' to the path 'path'
 
359
            try:
 
360
                dest = open(filepath_local, 'wb')
 
361
                if data is not None:
 
362
                    shutil.copyfileobj(cStringIO.StringIO(filedata), dest)
 
363
            except OSError:
 
364
                goterror = True
 
365
 
 
366
    if goterror:
 
367
        if len(data) == 1:
 
368
            raise ActionError("Could not write to target file")
 
369
        else:
 
370
            raise ActionError(
 
371
                "Could not write to one or more of the target files")
 
372
 
 
373
def action_copy_or_cut(req, fields, mode):
 
374
    """Marks specified files on the clipboard, stored in the
 
375
    browser session. Sets clipboard for either a cut or copy operation
 
376
    as specified.
 
377
 
 
378
    Reads fields: 'path'
 
379
    """
 
380
    # The clipboard object created conforms to the JSON clipboard
 
381
    # specification given at the top of listing.py.
 
382
    # Note that we do not check for the existence of files here. That is done
 
383
    # in the paste operation.
 
384
    files = fields.getlist('path')
 
385
    files = map(lambda field: field.value, files)
 
386
    clipboard = { "mode" : mode, "base" : req.path, "files" : files }
 
387
    session = req.get_session()
 
388
    session['clipboard'] = clipboard
 
389
    session.save()
 
390
 
 
391
def action_copy(req, fields):
 
392
    """Marks specified files on the clipboard, stored in the
 
393
    browser session. Sets clipboard for a "copy" action.
 
394
 
 
395
    Reads fields: 'path'
 
396
    """
 
397
    action_copy_or_cut(req, fields, "copy")
 
398
 
 
399
def action_cut(req, fields):
 
400
    """Marks specified files on the clipboard, stored in the
 
401
    browser session. Sets clipboard for a "cut" action.
 
402
 
 
403
    Reads fields: 'path'
 
404
    """
 
405
    action_copy_or_cut(req, fields, "cut")
 
406
 
 
407
def action_paste(req, fields):
 
408
    """Performs the copy or move action with the files stored on
 
409
    the clipboard in the browser session. Copies/moves the files
 
410
    to the specified directory. Clears the clipboard.
 
411
 
 
412
    Reads fields: 'path'
 
413
    """
 
414
    errormsg = None
 
415
 
 
416
    todir = fields.getfirst('path')
 
417
    if todir is None:
 
418
        raise ActionError("Required field missing")
 
419
    todir_local = actionpath_to_local(req, todir)
 
420
    if not os.path.isdir(todir_local):
 
421
        raise ActionError("Target is not a directory")
 
422
 
 
423
    session = req.get_session()
 
424
    try:
 
425
        clipboard = session['clipboard']
 
426
        files = clipboard['files']
 
427
        base = clipboard['base']
 
428
        if clipboard['mode'] == "copy":
 
429
            copy = True
 
430
        else:
 
431
            copy = False
 
432
    except KeyError:
 
433
        raise ActionError("Clipboard was empty")
 
434
 
 
435
    errorfiles = []
 
436
    for file in files:
 
437
        # The source must not be interpreted as relative to req.path
 
438
        # Add a slash (relative to top-level)
 
439
        frompath = os.sep + os.path.join(base, file)
 
440
        # The destination is found by taking just the basename of the file
 
441
        topath = os.path.join(todir, os.path.basename(file))
 
442
        try:
 
443
            movefile(req, frompath, topath, copy)
 
444
        except ActionError, message:
 
445
            # Store the error for later; we want to copy as many as possible
 
446
            if errormsg is None:
 
447
                errormsg = message
 
448
            else:
 
449
                # Multiple errors; generic message
 
450
                errormsg = "One or more files could not be pasted"
 
451
            # Add this file to errorfiles; it will be put back on the
 
452
            # clipboard for possible future pasting.
 
453
            errorfiles.append(file)
 
454
    # If errors occured, augment the clipboard and raise ActionError
 
455
    if len(errorfiles) > 0:
 
456
        clipboard['files'] = errorfiles
 
457
        session['clipboard'] = clipboard
 
458
        session.save()
 
459
        raise ActionError(errormsg)
 
460
 
 
461
    # Success: Clear the clipboard
 
462
    del session['clipboard']
 
463
    session.save()
 
464
 
 
465
def action_svnadd(req, fields):
 
466
    """Performs a "svn add" to each file specified.
 
467
 
 
468
    Reads fields: 'path' (multiple)
 
469
    """
 
470
    paths = fields.getlist('path')
 
471
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
472
 
 
473
    try:
 
474
        svnclient.add(paths, recurse=True, force=True)
 
475
    except pysvn.ClientError:
 
476
        raise ActionError("One or more files could not be added")
 
477
 
 
478
def action_svnupdate(req, fields):
 
479
    """Performs a "svn update" to each file specified.
 
480
 
 
481
    Reads fields: 'path'
 
482
    """
 
483
    path = fields.getfirst('path')
 
484
    if path is None:
 
485
        raise ActionError("Required field missing")
 
486
    path = actionpath_to_local(req, path)
 
487
 
 
488
    try:
 
489
        svnclient.update(path, recurse=True)
 
490
    except pysvn.ClientError:
 
491
        raise ActionError("One or more files could not be updated")
 
492
 
 
493
def action_svnrevert(req, fields):
 
494
    """Performs a "svn revert" to each file specified.
 
495
 
 
496
    Reads fields: 'path' (multiple)
 
497
    """
 
498
    paths = fields.getlist('path')
 
499
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
500
 
 
501
    try:
 
502
        svnclient.revert(paths, recurse=True)
 
503
    except pysvn.ClientError:
 
504
        raise ActionError("One or more files could not be reverted")
 
505
 
 
506
def action_svnpublish(req, fields):
 
507
    """Sets svn property "ivle:published" on each file specified.
 
508
    Should only be called on directories (only effective on directories
 
509
    anyway).
 
510
 
 
511
    Reads fields: 'path'
 
512
    """
 
513
    paths = fields.getlist('path')
 
514
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
515
 
 
516
    try:
 
517
        for path in paths:
 
518
            # Note: Property value doesn't matter
 
519
            svnclient.propset("ivle:published", "", path, recurse=False)
 
520
    except pysvn.ClientError, e:
 
521
        raise ActionError("Directory could not be published")
 
522
 
 
523
def action_svnunpublish(req, fields):
 
524
    """Deletes svn property "ivle:published" on each file specified.
 
525
 
 
526
    Reads fields: 'path'
 
527
    """
 
528
    paths = fields.getlist('path')
 
529
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
530
 
 
531
    try:
 
532
        for path in paths:
 
533
            svnclient.propdel("ivle:published", path, recurse=False)
 
534
    except pysvn.ClientError:
 
535
        raise ActionError("Directory could not be unpublished")
 
536
 
 
537
def action_svncommit(req, fields):
 
538
    """Performs a "svn commit" to each file specified.
 
539
 
 
540
    Reads fields: 'path' (multiple), 'logmsg' (optional)
 
541
    """
 
542
    paths = fields.getlist('path')
 
543
    paths = map(lambda path: actionpath_to_local(req, str(path)), paths)
 
544
    logmsg = str(fields.getfirst('logmsg', DEFAULT_LOGMESSAGE))
 
545
    if logmsg == '': logmsg = DEFAULT_LOGMESSAGE
 
546
 
 
547
    try:
 
548
        svnclient.checkin(paths, logmsg, recurse=True)
 
549
    except pysvn.ClientError:
 
550
        raise ActionError("One or more files could not be committed")
 
551
 
 
552
# Table of all action functions #
 
553
# Each function has the interface f(req, fields).
 
554
 
 
555
actions_table = {
 
556
    "remove" : action_remove,
 
557
    "move" : action_move,
 
558
    "putfile" : action_putfile,
 
559
    "putfiles" : action_putfiles,
 
560
 
 
561
    "copy" : action_copy,
 
562
    "cut" : action_cut,
 
563
    "paste" : action_paste,
 
564
 
 
565
    "svnadd" : action_svnadd,
 
566
    "svnupdate" : action_svnupdate,
 
567
    "svnrevert" : action_svnrevert,
 
568
    "svnpublish" : action_svnpublish,
 
569
    "svnunpublish" : action_svnunpublish,
 
570
    "svncommit" : action_svncommit,
 
571
}