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

« back to all changes in this revision

Viewing changes to ivle/fileservice_lib/action.py

  • Committer: drtomc
  • Date: 2008-02-01 04:13:23 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:369
Add stuff on installing and configuring pound.

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.
45
 
#       path:   The path to the file to be written. Error if the target
46
 
#               file is a directory.
47
 
#       data:   Bytes to be written to the file verbatim. May either be
48
 
#               a string variable or a file upload.
49
 
#       overwrite: Optional. If supplied, the file will be overwritten.
50
 
#               Otherwise, error if path already exists.
51
 
#
52
 
# action=putfiles: Upload multiple files to the student workspace, and
53
 
#                 optionally accept zip files which will be unpacked.
54
 
#       path:   The path to the DIRECTORY to place files in. Must not be a
55
 
#               file.
56
 
#       data:   A file upload (may not be a simple string). The filename
57
 
#               will be used to determine the target filename within
58
 
#               the given path.
59
 
#       unpack: Optional. If supplied, if any data is a valid ZIP file,
60
 
#               will create a directory instead and unpack the ZIP file
61
 
#               into it.
62
 
#
63
 
# action=mkdir: Create a directory. The parent dir must exist.
64
 
#       path:   The path to a file which does not exist, but whose parent
65
 
#               does. The dir will be made with this name.
66
 
#
67
 
# The differences between putfile and putfiles are:
68
 
# * putfile can only accept a single file, and can't unpack zipfiles.
69
 
# * putfile can accept string data, doesn't have to be a file upload.
70
 
# * putfile ignores the upload filename, the entire filename is specified on
71
 
#       path. putfiles calls files after the name on the user's machine.
72
 
#
73
 
# action=paste: Copy or move the files to a specified dir.
74
 
#       src:    The path to the DIRECTORY to get the files from (relative).
75
 
#       dst:    The path to the DIRECTORY to paste the files to. Must not
76
 
#               be a file.
77
 
#       mode:   'copy' or 'move'
78
 
#       file:   File to be copied or moved, relative to src, to a destination
79
 
#               relative to dst. Can be specified multiple times.
80
 
#
81
 
# Subversion actions.
82
 
# action=svnadd: Add an existing file(s) to version control.
83
 
#       path:   The path to the file to be added. Can be specified multiple
84
 
#               times.
85
 
#
86
 
# action=svnrevert: Revert a file(s) to its state as of the current revision
87
 
#               / undo local edits.
88
 
#       path:   The path to the file to be reverted. Can be specified multiple
89
 
#               times.
90
 
#
91
 
# action=svnupdate: Bring a file up to date with the head revision.
92
 
#       path:   The path to the file to be updated. Only one file may be
93
 
#               specified.
94
 
#
95
 
# action=svnpublish: Set the "published" flag on a file to True.
96
 
#       path:   The path to the file to be published. Can be specified
97
 
#               multiple times.
98
 
#
99
 
# action=svnunpublish: Set the "published" flag on a file to False.
100
 
#       path:   The path to the file to be unpublished. Can be specified
101
 
#               multiple times.
102
 
#
103
 
# action=svncommit: Commit a file(s) or directory(s) to the repository.
104
 
#       path:   The path to the file or directory to be committed. Can be
105
 
#               specified multiple times. Directories are committed
106
 
#               recursively.
107
 
#       logmsg: Text of the log message. Optional. There is a default log
108
 
#               message if unspecified.
109
 
# action=svncheckout: Checkout a file/directory from the repository.
110
 
#       path:   The [repository] path to the file or directory to be
111
 
#               checked out.
112
 
113
 
# action=svnrepomkdir: Create a directory in a repository (not WC).
114
 
#       path:   The path to the directory to be created (under the IVLE
115
 
#               repository base).
116
 
#       logmsg: Text of the log message.
117
 
118
 
# action=svnrepostat: Check if a path exists in a repository (not WC).
119
 
#       path:   The path to the directory to be checked (under the IVLE
120
 
#               repository base).
121
 
#
122
 
# TODO: Implement the following actions:
123
 
#   svnupdate (done?)
124
 
# TODO: Implement ZIP unpacking in putfiles (done?).
125
 
# TODO: svnupdate needs a digest to tell the user the files that were updated.
126
 
#   This can be implemented by some message passing between action and
127
 
#   listing, and having the digest included in the listing. (Problem if
128
 
#   the listing is not a directory, but we could make it an error to do an
129
 
#   update if the path is not a directory).
130
 
 
131
 
import os
132
 
import cStringIO
133
 
import shutil
134
 
import urllib
135
 
 
136
 
import pysvn
137
 
 
138
 
from ivle import (util, studpath, zip)
139
 
from ivle.fileservice_lib.exceptions import WillNotOverwrite
140
 
import ivle.conf
141
 
 
142
 
 
143
 
def get_login(_realm, existing_login, _may_save):
144
 
    """Callback function used by pysvn for authentication.
145
 
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
146
 
        callback_get_login.
147
 
        The following has been determined empirically, not from docs:
148
 
        existing_login will be the name of the user who owns the process on
149
 
        the first attempt, "" on subsequent attempts. We use this fact.
150
 
    """
151
 
    # Only provide credentials on the _first_ attempt.
152
 
    # If we're being asked again, then it means the credentials failed for
153
 
    # some reason and we should just fail. (This is not desirable, but it's
154
 
    # better than being asked an infinite number of times).
155
 
    return (existing_login != "", str(ivle.conf.login),
156
 
                                  str(ivle.conf.svn_pass), True)
157
 
 
158
 
# Make a Subversion client object
159
 
svnclient = pysvn.Client()
160
 
svnclient.callback_get_login = get_login
161
 
svnclient.exception_style = 0               # Simple (string) exceptions
162
 
 
163
 
DEFAULT_LOGMESSAGE = "No log message supplied."
164
 
 
165
 
# Mime types
166
 
# application/json is the "best" content type but is not good for
167
 
# debugging because Firefox just tries to download it
168
 
mime_dirlisting = "text/html"
169
 
#mime_dirlisting = "application/json"
170
 
 
171
 
class ActionError(Exception):
172
 
    """Represents an error processing an action. This can be
173
 
    raised by any of the action functions, and will be caught
174
 
    by the top-level handler, put into the HTTP response field,
175
 
    and continue.
176
 
 
177
 
    Important Security Consideration: The message passed to this
178
 
    exception will be relayed to the client.
179
 
    """
180
 
    pass
181
 
 
182
 
def handle_action(req, action, fields):
183
 
    """Perform the "action" part of the response.
184
 
    This function should only be called if the response is a POST.
185
 
    This performs the action's side-effect on the server. If unsuccessful,
186
 
    writes the X-IVLE-Action-Error header to the request object. Otherwise,
187
 
    does not touch the request object. Does NOT write any bytes in response.
188
 
 
189
 
    May throw an ActionError. The caller should put this string into the
190
 
    X-IVLE-Action-Error header, and then continue normally.
191
 
 
192
 
    action: String, the action requested. Not sanitised.
193
 
    fields: FieldStorage object containing all arguments passed.
194
 
    """
195
 
    global actions_table        # Table of function objects
196
 
    try:
197
 
        action = actions_table[action]
198
 
    except KeyError:
199
 
        # Default, just send an error but then continue
200
 
        raise ActionError("Unknown action")
201
 
    return action(req, fields)
202
 
 
203
 
def actionpath_to_urlpath(req, path):
204
 
    """Determines the URL path (relative to the student home) upon which the
205
 
    action is intended to act. See actionpath_to_local.
206
 
    """
207
 
    if path is None:
208
 
        return req.path
209
 
    elif len(path) > 0 and path[0] == os.sep:
210
 
        # Relative to student home
211
 
        return path[1:]
212
 
    else:
213
 
        # Relative to req.path
214
 
        return os.path.join(req.path, path)
215
 
 
216
 
def actionpath_to_local(req, path):
217
 
    """Determines the local path upon which an action is intended to act.
218
 
    Note that fileservice actions accept two paths: the request path,
219
 
    and the "path" argument given to the action.
220
 
    According to the rules, if the "path" argument begins with a '/' it is
221
 
    relative to the user's home; if it does not, it is relative to the
222
 
    supplied path.
223
 
 
224
 
    This resolves the path, given the request and path argument.
225
 
 
226
 
    May raise an ActionError("Invalid path"). The caller is expected to
227
 
    let this fall through to the top-level handler, where it will be
228
 
    put into the HTTP response field. Never returns None.
229
 
 
230
 
    Does not mutate req.
231
 
    """
232
 
    r = studpath.to_home_path(actionpath_to_urlpath(req, path))
233
 
    if r is None:
234
 
        raise ActionError("Invalid path")
235
 
    return r
236
 
 
237
 
def movefile(req, frompath, topath, copy=False):
238
 
    """Performs a file move, resolving filenames, checking for any errors,
239
 
    and throwing ActionErrors if necessary. Can also be used to do a copy
240
 
    operation instead.
241
 
 
242
 
    frompath and topath are straight paths from the client. Will be checked.
243
 
    """
244
 
    # TODO: Do an SVN mv if the file is versioned.
245
 
    # TODO: Disallow tampering with student's home directory
246
 
    if frompath is None or topath is None:
247
 
        raise ActionError("Required field missing")
248
 
    frompath = actionpath_to_local(req, frompath)
249
 
    topath = actionpath_to_local(req, topath)
250
 
    if not os.path.exists(frompath):
251
 
        raise ActionError("The source file does not exist")
252
 
    if os.path.exists(topath):
253
 
        if frompath == topath:
254
 
            raise ActionError("Source and destination are the same")
255
 
        raise ActionError("A file already exists with that name")
256
 
 
257
 
    try:
258
 
        if copy:
259
 
            if os.path.isdir(frompath):
260
 
                shutil.copytree(frompath, topath)
261
 
            else:
262
 
                shutil.copy2(frompath, topath)
263
 
        else:
264
 
            shutil.move(frompath, topath)
265
 
    except OSError:
266
 
        raise ActionError("Could not move the file specified")
267
 
    except shutil.Error:
268
 
        raise ActionError("Could not move the file specified")
269
 
 
270
 
def svn_movefile(req, frompath, topath, copy=False):
271
 
    """Performs an svn move, resolving filenames, checking for any errors,
272
 
    and throwing ActionErrors if necessary. Can also be used to do a copy
273
 
    operation instead.
274
 
 
275
 
    frompath and topath are straight paths from the client. Will be checked.
276
 
    """
277
 
    if frompath is None or topath is None:
278
 
        raise ActionError("Required field missing")
279
 
    frompath = actionpath_to_local(req, frompath)
280
 
    topath = actionpath_to_local(req, topath)
281
 
    if not os.path.exists(frompath):
282
 
        raise ActionError("The source file does not exist")
283
 
    if os.path.exists(topath):
284
 
        if frompath == topath:
285
 
            raise ActionError("Source and destination are the same")
286
 
        raise ActionError("A file already exists with that name")
287
 
 
288
 
    try:
289
 
        if copy:
290
 
            svnclient.copy(frompath, topath)
291
 
        else:
292
 
            svnclient.move(frompath, topath)
293
 
    except OSError:
294
 
        raise ActionError("Could not move the file specified")
295
 
    except pysvn.ClientError:
296
 
        raise ActionError("Could not move the file specified")  
297
 
 
298
 
 
299
 
### ACTIONS ###
300
 
 
301
 
def action_delete(req, fields):
302
 
    # TODO: Disallow removal of student's home directory
303
 
    """Removes a list of files or directories.
304
 
 
305
 
    Reads fields: 'path' (multiple)
306
 
    """
307
 
    paths = fields.getlist('path')
308
 
    goterror = False
309
 
    for path in paths:
310
 
        path = actionpath_to_local(req, path)
311
 
        try:
312
 
            if os.path.isdir(path):
313
 
                shutil.rmtree(path)
314
 
            else:
315
 
                os.remove(path)
316
 
        except OSError:
317
 
            goterror = True
318
 
        except shutil.Error:
319
 
            goterror = True
320
 
    if goterror:
321
 
        if len(paths) == 1:
322
 
            raise ActionError("Could not delete the file specified")
323
 
        else:
324
 
            raise ActionError(
325
 
                "Could not delete one or more of the files specified")
326
 
 
327
 
def action_move(req, fields):
328
 
    # TODO: Do an SVN mv if the file is versioned.
329
 
    # TODO: Disallow tampering with student's home directory
330
 
    """Removes a list of files or directories.
331
 
 
332
 
    Reads fields: 'from', 'to'
333
 
    """
334
 
    frompath = fields.getfirst('from')
335
 
    topath = fields.getfirst('to')
336
 
    movefile(req, frompath, topath)
337
 
 
338
 
def action_mkdir(req, fields):
339
 
    """Creates a directory with the given path.
340
 
    Reads fields: 'path'
341
 
    """
342
 
    path = fields.getfirst('path')
343
 
    if path is None:
344
 
        raise ActionError("Required field missing")
345
 
    path = actionpath_to_local(req, path)
346
 
 
347
 
    if os.path.exists(path):
348
 
        raise ActionError("A file already exists with that name")
349
 
 
350
 
    # Create the directory
351
 
    try:
352
 
        os.mkdir(path)
353
 
    except OSError:
354
 
        raise ActionError("Could not create directory")
355
 
 
356
 
def action_putfile(req, fields):
357
 
    """Writes data to a file, overwriting it if it exists and creating it if
358
 
    it doesn't.
359
 
 
360
 
    Reads fields: 'path', 'data' (file upload), 'overwrite'
361
 
    """
362
 
    # TODO: Read field "unpack".
363
 
    # Important: Data is "None" if the file submitted is empty.
364
 
    path = fields.getfirst('path')
365
 
    data = fields.getfirst('data')
366
 
    if path is None:
367
 
        raise ActionError("Required field missing")
368
 
    if data is None:
369
 
        # Workaround - field reader treats "" as None, so this is the only
370
 
        # way to allow blank file uploads
371
 
        data = ""
372
 
    path = actionpath_to_local(req, path)
373
 
 
374
 
    if data is not None:
375
 
        data = cStringIO.StringIO(data)
376
 
 
377
 
    overwrite = fields.getfirst('overwrite')
378
 
    if overwrite is None:
379
 
        overwrite = False
380
 
    else:
381
 
        overwrite = True
382
 
 
383
 
    if overwrite:
384
 
        # Overwrite files; but can't if it's a directory
385
 
        if os.path.isdir(path):
386
 
            raise ActionError("A directory already exists "
387
 
                    + "with that name")
388
 
    else:
389
 
        if os.path.exists(path):
390
 
            raise ActionError("A file already exists with that name")
391
 
 
392
 
    # Copy the contents of file object 'data' to the path 'path'
393
 
    try:
394
 
        dest = open(path, 'wb')
395
 
        if data is not None:
396
 
            shutil.copyfileobj(data, dest)
397
 
    except (IOError, OSError), e:
398
 
        raise ActionError("Could not write to target file: %s" % e.strerror)
399
 
 
400
 
def action_putfiles(req, fields):
401
 
    """Writes data to one or more files in a directory, overwriting them if
402
 
    it they exist.
403
 
 
404
 
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
405
 
    """
406
 
    # Important: Data is "None" if the file submitted is empty.
407
 
    path = fields.getfirst('path')
408
 
    data = fields['data']
409
 
    if type(data) != type([]):
410
 
        data = [data]
411
 
    unpack = fields.getfirst('unpack')
412
 
    if unpack is None:
413
 
        unpack = False
414
 
    else:
415
 
        unpack = True
416
 
    if path is None:
417
 
        raise ActionError("Required field missing")
418
 
    path = actionpath_to_urlpath(req, path)
419
 
    goterror = False
420
 
 
421
 
    for datum in data:
422
 
        # Each of the uploaded files
423
 
        filepath = os.path.join(path, datum.filename)
424
 
        filepath_local = studpath.to_home_path(filepath)
425
 
        if os.path.isdir(filepath_local):
426
 
            raise ActionError("A directory already exists "
427
 
                    + "with that name")
428
 
        else:
429
 
            if os.path.exists(filepath_local):
430
 
                raise ActionError("A file already exists with that name")
431
 
        filedata = datum.file
432
 
 
433
 
        if unpack and datum.filename.lower().endswith(".zip"):
434
 
            # A zip file - unpack it instead of just copying
435
 
            # TODO: Use the magic number instead of file extension
436
 
            # Note: Just unzip into the current directory (ignore the
437
 
            # filename)
438
 
            try:
439
 
                # First get the entire path (within jail)
440
 
                abspath = studpath.to_home_path(path)
441
 
                abspath = os.path.join(os.sep, abspath)
442
 
                zip.unzip(abspath, filedata)
443
 
            except (OSError, IOError):
444
 
                goterror = True
445
 
            except WillNotOverwrite, e:
446
 
                raise ActionError("File '" + e.filename + "' already exists.")
447
 
        else:
448
 
            # Not a zip file
449
 
            filepath_local = studpath.to_home_path(filepath)
450
 
            if filepath_local is None:
451
 
                raise ActionError("Invalid path")
452
 
 
453
 
            # Copy the contents of file object 'data' to the path 'path'
454
 
            try:
455
 
                dest = open(filepath_local, 'wb')
456
 
                if data is not None:
457
 
                    shutil.copyfileobj(filedata, dest)
458
 
            except (OSError, IOError):
459
 
                # TODO: Be more descriptive.
460
 
                goterror = True
461
 
 
462
 
    if goterror:
463
 
        if len(data) == 1:
464
 
            raise ActionError("Could not write to target file")
465
 
        else:
466
 
            raise ActionError(
467
 
                "Could not write to one or more of the target files")
468
 
 
469
 
def action_paste(req, fields):
470
 
    """Performs the copy or move action with the files specified.
471
 
    Copies/moves the files to the specified directory.
472
 
 
473
 
    Reads fields: 'src', 'dst', 'mode', 'file' (multiple).
474
 
    src: Base path that all the files are relative to (source).
475
 
    dst: Destination path to paste into.
476
 
    mode: 'copy' or 'move'.
477
 
    file: (Multiple) Files relative to base, which will be copied
478
 
        or moved to new locations relative to path.
479
 
    """
480
 
    errormsg = None
481
 
 
482
 
    dst = fields.getfirst('dst')
483
 
    src = fields.getfirst('src')
484
 
    mode = fields.getfirst('mode')
485
 
    files = fields.getlist('file')
486
 
    if dst is None or src is None or mode is None:
487
 
        raise ActionError("Required field missing")
488
 
 
489
 
    dst_local = actionpath_to_local(req, dst)
490
 
    if not os.path.isdir(dst_local):
491
 
        raise ActionError("dst is not a directory")
492
 
 
493
 
    errorfiles = []
494
 
    for file in files:
495
 
        # The source must not be interpreted as relative to req.path
496
 
        # Add a slash (relative to top-level)
497
 
        if src[:1] != '/':
498
 
            src = '/' + src
499
 
        frompath = os.path.join(src, file)
500
 
        # The destination is found by taking just the basename of the file
501
 
        topath = os.path.join(dst, os.path.basename(file))
502
 
        try:
503
 
            if mode == "copy":
504
 
                movefile(req, frompath, topath, True)
505
 
            elif mode == "move":
506
 
                movefile(req, frompath, topath, False)
507
 
            elif mode == "svncopy":
508
 
                svn_movefile(req, frompath, topath, True)
509
 
            elif mode == "svnmove":
510
 
                svn_movefile(req, frompath, topath, False)
511
 
            else:
512
 
                raise ActionError("Invalid mode (must be '(svn)copy' or '(svn)move')")
513
 
        except ActionError, message:
514
 
            # Store the error for later; we want to copy as many as possible
515
 
            if errormsg is None:
516
 
                errormsg = message
517
 
            else:
518
 
                # Multiple errors; generic message
519
 
                errormsg = "One or more files could not be pasted"
520
 
            # Add this file to errorfiles; it will be put back on the
521
 
            # clipboard for possible future pasting.
522
 
            errorfiles.append(file)
523
 
    if errormsg is not None:
524
 
        raise ActionError(errormsg)
525
 
 
526
 
    # XXX errorfiles contains a list of files that couldn't be pasted.
527
 
    # we currently do nothing with this.
528
 
 
529
 
def action_publish(req,fields):
530
 
    """Marks the folder as published by adding a '.published' file to the 
531
 
    directory and ensuring that the parent directory permissions are correct
532
 
 
533
 
    Reads fields: 'path'
534
 
    """
535
 
    paths = fields.getlist('path')
536
 
    user = util.split_path(req.path)[0]
537
 
    homedir = "/home/%s" % user
538
 
    if len(paths):
539
 
        paths = map(lambda path: actionpath_to_local(req, path), paths)
540
 
    else:
541
 
        paths = [studpath.to_home_path(req.path)]
542
 
 
543
 
    # Set all the dirs in home dir world browsable (o+r,o+x)
544
 
    #FIXME: Should really only do those in the direct path not all of the 
545
 
    # folders in a students home directory
546
 
    for root,dirs,files in os.walk(homedir):
547
 
        os.chmod(root, os.stat(root).st_mode|0005)
548
 
 
549
 
    try:
550
 
        for path in paths:
551
 
            if os.path.isdir(path):
552
 
                pubfile = open(os.path.join(path,'.published'),'w')
553
 
                pubfile.write("This directory is published\n")
554
 
                pubfile.close()
555
 
            else:
556
 
                raise ActionError("Can only publish directories")
557
 
    except OSError, e:
558
 
        raise ActionError("Directory could not be published")
559
 
 
560
 
def action_unpublish(req,fields):
561
 
    """Marks the folder as unpublished by removing a '.published' file in the 
562
 
    directory (if it exits). It does not change the permissions of the parent 
563
 
    directories.
564
 
 
565
 
    Reads fields: 'path'
566
 
    """
567
 
    paths = fields.getlist('path')
568
 
    if len(paths):
569
 
        paths = map(lambda path: actionpath_to_local(req, path), paths)
570
 
    else:
571
 
        paths = [studpath.to_home_path(req.path)]
572
 
 
573
 
    try:
574
 
        for path in paths:
575
 
            if os.path.isdir(path):
576
 
                pubfile = os.path.join(path,'.published')
577
 
                if os.path.isfile(pubfile):
578
 
                    os.remove(pubfile)
579
 
            else:
580
 
                raise ActionError("Can only unpublish directories")
581
 
    except OSError, e:
582
 
        raise ActionError("Directory could not be unpublished")
583
 
 
584
 
 
585
 
def action_svnadd(req, fields):
586
 
    """Performs a "svn add" to each file specified.
587
 
 
588
 
    Reads fields: 'path' (multiple)
589
 
    """
590
 
    paths = fields.getlist('path')
591
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
592
 
 
593
 
    try:
594
 
        svnclient.add(paths, recurse=True, force=True)
595
 
    except pysvn.ClientError, e:
596
 
        raise ActionError(str(e))
597
 
 
598
 
def action_svnremove(req, fields):
599
 
    """Performs a "svn remove" on each file specified.
600
 
 
601
 
    Reads fields: 'path' (multiple)
602
 
    """
603
 
    paths = fields.getlist('path')
604
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
605
 
 
606
 
    try:
607
 
        svnclient.remove(paths, force=True)
608
 
    except pysvn.ClientError, e:
609
 
        raise ActionError(str(e))
610
 
 
611
 
def action_svnupdate(req, fields):
612
 
    """Performs a "svn update" to each file specified.
613
 
 
614
 
    Reads fields: 'path'
615
 
    """
616
 
    path = fields.getfirst('path')
617
 
    if path is None:
618
 
        raise ActionError("Required field missing")
619
 
    path = actionpath_to_local(req, path)
620
 
 
621
 
    try:
622
 
        svnclient.update(path, recurse=True)
623
 
    except pysvn.ClientError, e:
624
 
        raise ActionError(str(e))
625
 
 
626
 
def action_svnresolved(req, fields):
627
 
    """Performs a "svn resolved" to each file specified.
628
 
 
629
 
    Reads fields: 'path'
630
 
    """
631
 
    path = fields.getfirst('path')
632
 
    if path is None:
633
 
        raise ActionError("Required field missing")
634
 
    path = actionpath_to_local(req, path)
635
 
 
636
 
    try:
637
 
        svnclient.resolved(path, recurse=True)
638
 
    except pysvn.ClientError, e:
639
 
        raise ActionError(str(e))
640
 
 
641
 
def action_svnrevert(req, fields):
642
 
    """Performs a "svn revert" to each file specified.
643
 
 
644
 
    Reads fields: 'path' (multiple)
645
 
    """
646
 
    paths = fields.getlist('path')
647
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
648
 
 
649
 
    try:
650
 
        svnclient.revert(paths, recurse=True)
651
 
    except pysvn.ClientError, e:
652
 
        raise ActionError(str(e))
653
 
 
654
 
def action_svnpublish(req, fields):
655
 
    """Sets svn property "ivle:published" on each file specified.
656
 
    Should only be called on directories (only effective on directories
657
 
    anyway).
658
 
 
659
 
    Reads fields: 'path'
660
 
 
661
 
    XXX Currently unused by the client (calls action_publish instead, which
662
 
    has a completely different publishing model).
663
 
    """
664
 
    paths = fields.getlist('path')
665
 
    if len(paths):
666
 
        paths = map(lambda path: actionpath_to_local(req, path), paths)
667
 
    else:
668
 
        paths = [studpath.to_home_path(req.path)]
669
 
 
670
 
    try:
671
 
        for path in paths:
672
 
            # Note: Property value doesn't matter
673
 
            svnclient.propset("ivle:published", "", path, recurse=False)
674
 
    except pysvn.ClientError, e:
675
 
        raise ActionError("Directory could not be published")
676
 
 
677
 
def action_svnunpublish(req, fields):
678
 
    """Deletes svn property "ivle:published" on each file specified.
679
 
 
680
 
    Reads fields: 'path'
681
 
 
682
 
    XXX Currently unused by the client (calls action_unpublish instead, which
683
 
    has a completely different publishing model).
684
 
    """
685
 
    paths = fields.getlist('path')
686
 
    paths = map(lambda path: actionpath_to_local(req, path), paths)
687
 
 
688
 
    try:
689
 
        for path in paths:
690
 
            svnclient.propdel("ivle:published", path, recurse=False)
691
 
    except pysvn.ClientError, e:
692
 
        raise ActionError("Directory could not be unpublished")
693
 
 
694
 
def action_svncommit(req, fields):
695
 
    """Performs a "svn commit" to each file specified.
696
 
 
697
 
    Reads fields: 'path' (multiple), 'logmsg' (optional)
698
 
    """
699
 
    paths = fields.getlist('path')
700
 
    paths = map(lambda path: actionpath_to_local(req, str(path)), paths)
701
 
    logmsg = str(fields.getfirst('logmsg', DEFAULT_LOGMESSAGE))
702
 
    if logmsg == '': logmsg = DEFAULT_LOGMESSAGE
703
 
 
704
 
    try:
705
 
        svnclient.checkin(paths, logmsg, recurse=True)
706
 
    except pysvn.ClientError, e:
707
 
        raise ActionError(str(e))
708
 
 
709
 
def action_svncheckout(req, fields):
710
 
    """Performs a "svn checkout" of the first path into the second path.
711
 
 
712
 
    Reads fields: 'path'    (multiple)
713
 
    """
714
 
    paths = fields.getlist('path')
715
 
    if len(paths) != 2:
716
 
        raise ActionError("usage: svncheckout url local-path")
717
 
    url = ivle.conf.svn_addr + "/" + urllib.quote(paths[0])
718
 
    local_path = actionpath_to_local(req, str(paths[1]))
719
 
    try:
720
 
        svnclient.callback_get_login = get_login
721
 
        svnclient.checkout(url, local_path, recurse=True)
722
 
    except pysvn.ClientError, e:
723
 
        raise ActionError(str(e))
724
 
 
725
 
def action_svnrepomkdir(req, fields):
726
 
    """Performs a "svn mkdir" on a path under the IVLE SVN root.
727
 
 
728
 
    Reads fields: 'path'
729
 
    """
730
 
    path = fields.getfirst('path')
731
 
    logmsg = fields.getfirst('logmsg')
732
 
    url = ivle.conf.svn_addr + "/" + path
733
 
    try:
734
 
        svnclient.callback_get_login = get_login
735
 
        svnclient.mkdir(url, log_message=logmsg)
736
 
    except pysvn.ClientError, e:
737
 
        raise ActionError(str(e))
738
 
 
739
 
def action_svnrepostat(req, fields):
740
 
    """Discovers whether a path exists in a repo under the IVLE SVN root.
741
 
 
742
 
    If it does exist, returns a dict containing its metadata.
743
 
 
744
 
    Reads fields: 'path'
745
 
    """
746
 
    path = fields.getfirst('path')
747
 
    url = ivle.conf.svn_addr + "/" + path
748
 
    svnclient.exception_style = 1
749
 
 
750
 
    try:
751
 
        svnclient.callback_get_login = get_login
752
 
        info = svnclient.info2(url,
753
 
            revision=pysvn.Revision(pysvn.opt_revision_kind.head))[0][1]
754
 
        return {'svnrevision': info['rev'].number
755
 
                  if info['rev'] and
756
 
                     info['rev'].kind == pysvn.opt_revision_kind.number
757
 
                  else None}
758
 
    except pysvn.ClientError, e:
759
 
        # Error code 170000 means ENOENT in this revision.
760
 
        if e[1][0][1] == 170000:
761
 
            raise util.IVLEError(404, 'The specified repository path does not exist')
762
 
        else:
763
 
            raise ActionError(str(e[0]))
764
 
            
765
 
 
766
 
# Table of all action functions #
767
 
# Each function has the interface f(req, fields).
768
 
 
769
 
actions_table = {
770
 
    "delete" : action_delete,
771
 
    "move" : action_move,
772
 
    "mkdir" : action_mkdir,
773
 
    "putfile" : action_putfile,
774
 
    "putfiles" : action_putfiles,
775
 
    "paste" : action_paste,
776
 
    "publish" : action_publish,
777
 
    "unpublish" : action_unpublish,
778
 
 
779
 
    "svnadd" : action_svnadd,
780
 
    "svnremove" : action_svnremove,
781
 
    "svnupdate" : action_svnupdate,
782
 
    "svnresolved" : action_svnresolved,
783
 
    "svnrevert" : action_svnrevert,
784
 
    "svnpublish" : action_svnpublish,
785
 
    "svnunpublish" : action_svnunpublish,
786
 
    "svncommit" : action_svncommit,
787
 
    "svncheckout" : action_svncheckout,
788
 
    "svnrepomkdir" : action_svnrepomkdir,
789
 
    "svnrepostat" : action_svnrepostat,
790
 
}