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

411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
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
#
592 by mattgiuca
fileservice, browser/listing: Added new fileservice action, "mkdir".
65
# action=mkdir: Create a directory. The parent dir must exist.
66
#       path:   The path to a file which does not exist, but whose parent
67
#               does. The dir will be made with this name.
68
#
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
69
# The differences between putfile and putfiles are:
70
# * putfile can only accept a single file.
71
# * putfile can accept string data, doesn't have to be a file upload.
72
# * putfile ignores the upload filename, the entire filename is specified on
73
#       path. putfiles calls files after the name on the user's machine.
74
#
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
75
# action=paste: Copy or move the files to a specified dir.
76
#       src:    The path to the DIRECTORY to get the files from (relative).
77
#       dst:    The path to the DIRECTORY to paste the files to. Must not
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
78
#               be a file.
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
79
#       mode:   'copy' or 'move'
80
#       file:   File to be copied or moved, relative to src, to a destination
81
#               relative to dst. Can be specified multiple times.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
82
#
83
# Subversion actions.
84
# action=svnadd: Add an existing file(s) to version control.
85
#       path:   The path to the file to be added. Can be specified multiple
86
#               times.
87
#
88
# action=svnrevert: Revert a file(s) to its state as of the current revision
89
#               / undo local edits.
90
#       path:   The path to the file to be reverted. Can be specified multiple
91
#               times.
92
#
93
# action=svnupdate: Bring a file up to date with the head revision.
94
#       path:   The path to the file to be updated. Only one file may be
95
#               specified.
96
#
97
# action=svnpublish: Set the "published" flag on a file to True.
98
#       path:   The path to the file to be published. Can be specified
99
#               multiple times.
100
#
101
# action=svnunpublish: Set the "published" flag on a file to False.
102
#       path:   The path to the file to be unpublished. Can be specified
103
#               multiple times.
104
#
105
# action=svncommit: Commit a file(s) or directory(s) to the repository.
106
#       path:   The path to the file or directory to be committed. Can be
107
#               specified multiple times. Directories are committed
108
#               recursively.
109
#       logmsg: Text of the log message. Optional. There is a default log
110
#               message if unspecified.
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
111
# action=svncheckout: Checkout a file/directory from the repository.
112
#       path:   The [repository] path to the file or directory to be
113
#               checked out.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
114
# 
115
# TODO: Implement the following actions:
116
#   putfiles, svnrevert, svnupdate, svncommit
117
# TODO: Implement ZIP unpacking in putfile and putfiles.
118
# TODO: svnupdate needs a digest to tell the user the files that were updated.
119
#   This can be implemented by some message passing between action and
120
#   listing, and having the digest included in the listing. (Problem if
121
#   the listing is not a directory, but we could make it an error to do an
122
#   update if the path is not a directory).
123
124
import os
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
125
import cStringIO
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
126
import shutil
127
128
import pysvn
129
130
from common import (util, studpath, zip)
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
131
import conf
132
133
def get_login(_realm, _username, _may_save):
134
    """Return the subversion credentials for the user."""
135
    return (True, conf.login, conf.passwd, True)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
136
137
# Make a Subversion client object
138
svnclient = pysvn.Client()
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
139
svnclient.callback_get_login = get_login
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
140
141
DEFAULT_LOGMESSAGE = "No log message supplied."
142
143
# Mime types
144
# application/json is the "best" content type but is not good for
145
# debugging because Firefox just tries to download it
146
mime_dirlisting = "text/html"
147
#mime_dirlisting = "application/json"
148
149
class ActionError(Exception):
150
    """Represents an error processing an action. This can be
151
    raised by any of the action functions, and will be caught
152
    by the top-level handler, put into the HTTP response field,
153
    and continue.
154
155
    Important Security Consideration: The message passed to this
156
    exception will be relayed to the client.
157
    """
158
    pass
159
160
def handle_action(req, action, fields):
161
    """Perform the "action" part of the response.
162
    This function should only be called if the response is a POST.
163
    This performs the action's side-effect on the server. If unsuccessful,
164
    writes the X-IVLE-Action-Error header to the request object. Otherwise,
165
    does not touch the request object. Does NOT write any bytes in response.
166
167
    May throw an ActionError. The caller should put this string into the
168
    X-IVLE-Action-Error header, and then continue normally.
169
170
    action: String, the action requested. Not sanitised.
171
    fields: FieldStorage object containing all arguments passed.
172
    """
173
    global actions_table        # Table of function objects
174
    try:
175
        action = actions_table[action]
176
    except KeyError:
177
        # Default, just send an error but then continue
178
        raise ActionError("Unknown action")
179
    action(req, fields)
180
181
def actionpath_to_urlpath(req, path):
182
    """Determines the URL path (relative to the student home) upon which the
183
    action is intended to act. See actionpath_to_local.
184
    """
185
    if path is None:
186
        return req.path
187
    elif len(path) > 0 and path[0] == os.sep:
188
        # Relative to student home
189
        return path[1:]
190
    else:
191
        # Relative to req.path
192
        return os.path.join(req.path, path)
193
194
def actionpath_to_local(req, path):
195
    """Determines the local path upon which an action is intended to act.
196
    Note that fileservice actions accept two paths: the request path,
197
    and the "path" argument given to the action.
198
    According to the rules, if the "path" argument begins with a '/' it is
199
    relative to the user's home; if it does not, it is relative to the
200
    supplied path.
201
202
    This resolves the path, given the request and path argument.
203
204
    May raise an ActionError("Invalid path"). The caller is expected to
205
    let this fall through to the top-level handler, where it will be
206
    put into the HTTP response field. Never returns None.
207
208
    Does not mutate req.
209
    """
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
210
    (_, _, r) = studpath.url_to_jailpaths(actionpath_to_urlpath(req, path))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
211
    if r is None:
212
        raise ActionError("Invalid path")
213
    return r
214
215
def movefile(req, frompath, topath, copy=False):
216
    """Performs a file move, resolving filenames, checking for any errors,
217
    and throwing ActionErrors if necessary. Can also be used to do a copy
218
    operation instead.
219
220
    frompath and topath are straight paths from the client. Will be checked.
221
    """
222
    # TODO: Do an SVN mv if the file is versioned.
223
    # TODO: Disallow tampering with student's home directory
224
    if frompath is None or topath is None:
225
        raise ActionError("Required field missing")
226
    frompath = actionpath_to_local(req, frompath)
227
    topath = actionpath_to_local(req, topath)
228
    if not os.path.exists(frompath):
229
        raise ActionError("The source file does not exist")
230
    if os.path.exists(topath):
231
        if frompath == topath:
232
            raise ActionError("Source and destination are the same")
233
        raise ActionError("Another file already exists with that name")
234
235
    try:
236
        if copy:
237
            if os.path.isdir(frompath):
238
                shutil.copytree(frompath, topath)
239
            else:
240
                shutil.copy2(frompath, topath)
241
        else:
242
            shutil.move(frompath, topath)
243
    except OSError:
244
        raise ActionError("Could not move the file specified")
245
    except shutil.Error:
246
        raise ActionError("Could not move the file specified")
247
248
### ACTIONS ###
249
250
def action_remove(req, fields):
251
    # TODO: Do an SVN rm if the file is versioned.
252
    # TODO: Disallow removal of student's home directory
253
    """Removes a list of files or directories.
254
255
    Reads fields: 'path' (multiple)
256
    """
257
    paths = fields.getlist('path')
258
    goterror = False
259
    for path in paths:
260
        path = actionpath_to_local(req, path)
261
        try:
262
            if os.path.isdir(path):
263
                shutil.rmtree(path)
264
            else:
265
                os.remove(path)
266
        except OSError:
267
            goterror = True
268
        except shutil.Error:
269
            goterror = True
270
    if goterror:
271
        if len(paths) == 1:
272
            raise ActionError("Could not delete the file specified")
273
        else:
274
            raise ActionError(
275
                "Could not delete one or more of the files specified")
276
277
def action_move(req, fields):
278
    # TODO: Do an SVN mv if the file is versioned.
279
    # TODO: Disallow tampering with student's home directory
280
    """Removes a list of files or directories.
281
282
    Reads fields: 'from', 'to'
283
    """
284
    frompath = fields.getfirst('from')
285
    topath = fields.getfirst('to')
286
    movefile(req, frompath, topath)
287
592 by mattgiuca
fileservice, browser/listing: Added new fileservice action, "mkdir".
288
def action_mkdir(req, fields):
289
    """Creates a directory with the given path.
290
    Reads fields: 'path'
291
    """
292
    path = fields.getfirst('path')
293
    if path is None:
294
        raise ActionError("Required field missing")
295
    path = actionpath_to_local(req, path)
296
297
    # Create the directory
298
    try:
299
        os.mkdir(path)
300
    except OSError:
301
        raise ActionError("Could not create directory")
302
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
303
def action_putfile(req, fields):
304
    """Writes data to a file, overwriting it if it exists and creating it if
305
    it doesn't.
306
307
    Reads fields: 'path', 'data' (file upload)
308
    """
309
    # TODO: Read field "unpack".
310
    # Important: Data is "None" if the file submitted is empty.
311
    path = fields.getfirst('path')
312
    data = fields.getfirst('data')
611 by mattgiuca
"New File" now works. (This is a MUCH better replacement for having to go to
313
    if path is None:
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
314
        raise ActionError("Required field missing")
611 by mattgiuca
"New File" now works. (This is a MUCH better replacement for having to go to
315
    if data is None:
316
        # Workaround - field reader treats "" as None, so this is the only
317
        # way to allow blank file uploads
318
        data = ""
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
319
    path = actionpath_to_local(req, path)
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
320
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
321
    if data is not None:
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
322
        data = cStringIO.StringIO(data)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
323
324
    # Copy the contents of file object 'data' to the path 'path'
325
    try:
326
        dest = open(path, 'wb')
327
        if data is not None:
328
            shutil.copyfileobj(data, dest)
329
    except OSError:
330
        raise ActionError("Could not write to target file")
331
332
def action_putfiles(req, fields):
333
    """Writes data to one or more files in a directory, overwriting them if
334
    it they exist.
335
336
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
337
    """
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
338
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
339
    # Important: Data is "None" if the file submitted is empty.
340
    path = fields.getfirst('path')
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
341
    data = fields['data']
342
    if type(data) != type([]):
343
        data = [data]
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
344
    unpack = fields.getfirst('unpack')
345
    if unpack is None:
346
        unpack = False
347
    else:
348
        unpack = True
349
    if path is None:
350
        raise ActionError("Required field missing")
351
    path = actionpath_to_urlpath(req, path)
352
    goterror = False
353
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
354
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
355
    for datum in data:
356
        # Each of the uploaded files
357
        filepath = os.path.join(path, datum.filename)
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
358
        filedata = datum.value
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
359
360
        if unpack and datum.filename.lower().endswith(".zip"):
361
            # A zip file - unpack it instead of just copying
362
            # TODO: Use the magic number instead of file extension
363
            # Note: Just unzip into the current directory (ignore the
364
            # filename)
365
            try:
366
                zip.unzip(path, filedata)
367
            except (OSError, IOError):
368
                goterror = True
369
        else:
370
            # Not a zip file
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
371
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
372
            if filepath_local is None:
373
                raise ActionError("Invalid path")
374
375
            # Copy the contents of file object 'data' to the path 'path'
376
            try:
377
                dest = open(filepath_local, 'wb')
378
                if data is not None:
436 by drtomc
fileservice: Make things less broken than they were. No claims of perfection yet! Unpacking form data with cgi.py is AWFUL.
379
                    shutil.copyfileobj(cStringIO.StringIO(filedata), dest)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
380
            except OSError:
381
                goterror = True
382
383
    if goterror:
384
        if len(data) == 1:
385
            raise ActionError("Could not write to target file")
386
        else:
387
            raise ActionError(
388
                "Could not write to one or more of the target files")
389
390
def action_paste(req, fields):
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
391
    """Performs the copy or move action with the files specified.
392
    Copies/moves the files to the specified directory.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
393
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
394
    Reads fields: 'src', 'dst', 'mode', 'file' (multiple).
395
    src: Base path that all the files are relative to (source).
396
    dst: Destination path to paste into.
397
    mode: 'copy' or 'move'.
398
    file: (Multiple) Files relative to base, which will be copied
399
        or moved to new locations relative to path.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
400
    """
401
    errormsg = None
402
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
403
    dst = fields.getfirst('dst')
404
    src = fields.getfirst('src')
405
    mode = fields.getfirst('mode')
406
    files = fields.getlist('file')
407
    if dst is None or src is None or mode is None:
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
408
        raise ActionError("Required field missing")
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
409
    if mode == "copy":
410
        copy = True
411
    elif mode == "move":
412
        copy = False
413
    else:
414
        raise ActionError("Invalid mode (must be 'copy' or 'move')")
415
    dst_local = actionpath_to_local(req, dst)
416
    if not os.path.isdir(dst_local):
417
        raise ActionError("dst is not a directory")
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
418
419
    errorfiles = []
420
    for file in files:
421
        # The source must not be interpreted as relative to req.path
422
        # Add a slash (relative to top-level)
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
423
        if src[:1] != '/':
424
            src = '/' + src
425
        frompath = os.path.join(src, file)
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
426
        # The destination is found by taking just the basename of the file
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
427
        topath = os.path.join(dst, os.path.basename(file))
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
428
        try:
429
            movefile(req, frompath, topath, copy)
430
        except ActionError, message:
431
            # Store the error for later; we want to copy as many as possible
432
            if errormsg is None:
433
                errormsg = message
434
            else:
435
                # Multiple errors; generic message
436
                errormsg = "One or more files could not be pasted"
437
            # Add this file to errorfiles; it will be put back on the
438
            # clipboard for possible future pasting.
439
            errorfiles.append(file)
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
440
    if errormsg is not None:
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
441
        raise ActionError(errormsg)
442
650 by mattgiuca
fileservice/action: Refactored broken clipboard.
443
    # XXX errorfiles contains a list of files that couldn't be pasted.
444
    # we currently do nothing with this.
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
445
726 by dcoles
Browser: Added code to allow directories to be published through the UI. Also
446
def action_publish(req,fields):
447
    """Marks the folder as published by adding a '.published' file to the 
448
    directory and ensuring that the parent directory permissions are correct
449
450
    Reads fields: 'path'
451
    """
452
    paths = fields.getlist('path')
453
    user = studpath.url_to_local(req.path)[0]
454
    homedir = "/home/%s" % user
455
    if len(paths):
456
        paths = map(lambda path: actionpath_to_local(req, path), paths)
457
    else:
458
        paths = [studpath.url_to_jailpaths(req.path)[2]]
459
460
    # Set all the dirs in home dir world browsable (o+r,o+x)
461
    #FIXME: Should really only do those in the direct path not all of the 
462
    # folders in a students home directory
463
    for root,dirs,files in os.walk(homedir):
464
        os.chmod(root, os.stat(root).st_mode|0005)
465
466
    try:
467
        for path in paths:
468
            if os.path.isdir(path):
469
                pubfile = open(os.path.join(path,'.published'),'w')
470
                pubfile.write("This directory is published\n")
471
                pubfile.close()
472
            else:
473
                raise ActionError("Can only publish directories")
474
    except OSError, e:
475
        raise ActionError("Directory could not be published")
476
477
def action_unpublish(req,fields):
478
    """Marks the folder as unpublished by removing a '.published' file in the 
479
    directory (if it exits). It does not change the permissions of the parent 
480
    directories.
481
482
    Reads fields: 'path'
483
    """
484
    paths = fields.getlist('path')
485
    if len(paths):
486
        paths = map(lambda path: actionpath_to_local(req, path), paths)
487
    else:
488
        paths = [studpath.url_to_jailpaths(req.path)[2]]
489
490
    try:
491
        for path in paths:
492
            if os.path.isdir(path):
493
                pubfile = os.path.join(path,'.published')
494
                if os.path.isfile(pubfile):
495
                    os.remove(pubfile)
496
            else:
497
                raise ActionError("Can only unpublish directories")
498
    except OSError, e:
499
        raise ActionError("Directory could not be unpublished")
500
501
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
502
def action_svnadd(req, fields):
503
    """Performs a "svn add" to each file specified.
504
505
    Reads fields: 'path' (multiple)
506
    """
507
    paths = fields.getlist('path')
508
    paths = map(lambda path: actionpath_to_local(req, path), paths)
509
510
    try:
511
        svnclient.add(paths, recurse=True, force=True)
512
    except pysvn.ClientError:
513
        raise ActionError("One or more files could not be added")
514
515
def action_svnupdate(req, fields):
516
    """Performs a "svn update" to each file specified.
517
518
    Reads fields: 'path'
519
    """
520
    path = fields.getfirst('path')
521
    if path is None:
522
        raise ActionError("Required field missing")
523
    path = actionpath_to_local(req, path)
524
525
    try:
526
        svnclient.update(path, recurse=True)
527
    except pysvn.ClientError:
528
        raise ActionError("One or more files could not be updated")
529
530
def action_svnrevert(req, fields):
531
    """Performs a "svn revert" to each file specified.
532
533
    Reads fields: 'path' (multiple)
534
    """
535
    paths = fields.getlist('path')
536
    paths = map(lambda path: actionpath_to_local(req, path), paths)
537
538
    try:
539
        svnclient.revert(paths, recurse=True)
540
    except pysvn.ClientError:
541
        raise ActionError("One or more files could not be reverted")
542
543
def action_svnpublish(req, fields):
544
    """Sets svn property "ivle:published" on each file specified.
545
    Should only be called on directories (only effective on directories
546
    anyway).
547
548
    Reads fields: 'path'
549
    """
550
    paths = fields.getlist('path')
633 by drtomc
publishing: Make publishing the current directory work.
551
    if len(paths):
552
        paths = map(lambda path: actionpath_to_local(req, path), paths)
553
    else:
554
        paths = [studpath.url_to_jailpaths(req.path)[2]]
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
555
556
    try:
557
        for path in paths:
558
            # Note: Property value doesn't matter
559
            svnclient.propset("ivle:published", "", path, recurse=False)
445 by drtomc
fileservice: Repair a couple of error messages suffering from cutandpastitis.
560
    except pysvn.ClientError, e:
561
        raise ActionError("Directory could not be published")
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
562
563
def action_svnunpublish(req, fields):
564
    """Deletes svn property "ivle:published" on each file specified.
565
566
    Reads fields: 'path'
567
    """
568
    paths = fields.getlist('path')
569
    paths = map(lambda path: actionpath_to_local(req, path), paths)
570
571
    try:
572
        for path in paths:
573
            svnclient.propdel("ivle:published", path, recurse=False)
574
    except pysvn.ClientError:
448 by mattgiuca
lib/fileservice_lib: Fixed a syntax error.
575
        raise ActionError("Directory could not be unpublished")
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
576
577
def action_svncommit(req, fields):
578
    """Performs a "svn commit" to each file specified.
579
580
    Reads fields: 'path' (multiple), 'logmsg' (optional)
581
    """
582
    paths = fields.getlist('path')
583
    paths = map(lambda path: actionpath_to_local(req, str(path)), paths)
584
    logmsg = str(fields.getfirst('logmsg', DEFAULT_LOGMESSAGE))
585
    if logmsg == '': logmsg = DEFAULT_LOGMESSAGE
586
587
    try:
588
        svnclient.checkin(paths, logmsg, recurse=True)
589
    except pysvn.ClientError:
590
        raise ActionError("One or more files could not be committed")
591
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
592
def action_svncheckout(req, fields):
593
    """Performs a "svn checkout" of each path specified.
594
595
    Reads fields: 'path'    (multiple)
596
    """
597
    paths = fields.getlist('path')
598
    if len(paths) != 2:
599
        raise ActionError("usage: svncheckout url local-path")
600
    url = conf.svn_addr + "/" + login + "/" + paths[0]
601
    local_path = actionpath_to_local(req, str(paths[1]))
602
    try:
603
        svnclient.callback_get_login = get_login
604
        svnclient.checkout(url, local_path, recurse=True)
605
    except pysvn.ClientError:
606
        raise ActionError("One or more files could not be checked out")
607
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
608
# Table of all action functions #
609
# Each function has the interface f(req, fields).
610
611
actions_table = {
612
    "remove" : action_remove,
613
    "move" : action_move,
592 by mattgiuca
fileservice, browser/listing: Added new fileservice action, "mkdir".
614
    "mkdir" : action_mkdir,
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
615
    "putfile" : action_putfile,
616
    "putfiles" : action_putfiles,
617
    "paste" : action_paste,
726 by dcoles
Browser: Added code to allow directories to be published through the UI. Also
618
    "publish" : action_publish,
619
    "unpublish" : action_unpublish,
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
620
621
    "svnadd" : action_svnadd,
622
    "svnupdate" : action_svnupdate,
623
    "svnrevert" : action_svnrevert,
624
    "svnpublish" : action_svnpublish,
625
    "svnunpublish" : action_svnunpublish,
626
    "svncommit" : action_svncommit,
522 by drtomc
Add quite a lot of stuff to get usrmgt happening.
627
    "svncheckout" : action_svncheckout,
411 by mattgiuca
Renamed lib/fileservice to lib/fileservice_lib (naming conflict).
628
}