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

« back to all changes in this revision

Viewing changes to ivle/fileservice_lib/action.py

  • Committer: me at id
  • Date: 2009-01-15 03:02:36 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1150
ivle.makeuser.make_jail: Just take an ivle.database.User, rather than some
    attributes.

services/usrmgt-server: Give make_jail a User.

bin/ivle-remakeuser: Rewrite to use ivle.database.User.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
import pysvn
136
136
 
137
137
from ivle import (util, studpath, zip)
138
 
from ivle.fileservice_lib.exceptions import WillNotOverwrite
139
138
import ivle.conf
140
139
 
141
 
 
142
140
def get_login(_realm, existing_login, _may_save):
143
141
    """Callback function used by pysvn for authentication.
144
142
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
197
195
    except KeyError:
198
196
        # Default, just send an error but then continue
199
197
        raise ActionError("Unknown action")
200
 
    return action(req, fields)
 
198
    action(req, fields)
201
199
 
202
200
def actionpath_to_urlpath(req, path):
203
201
    """Determines the URL path (relative to the student home) upon which the
266
264
    except shutil.Error:
267
265
        raise ActionError("Could not move the file specified")
268
266
 
269
 
def svn_movefile(req, frompath, topath, copy=False):
270
 
    """Performs an svn move, resolving filenames, checking for any errors,
271
 
    and throwing ActionErrors if necessary. Can also be used to do a copy
272
 
    operation instead.
273
 
 
274
 
    frompath and topath are straight paths from the client. Will be checked.
275
 
    """
276
 
    if frompath is None or topath is None:
277
 
        raise ActionError("Required field missing")
278
 
    frompath = actionpath_to_local(req, frompath)
279
 
    topath = actionpath_to_local(req, topath)
280
 
    if not os.path.exists(frompath):
281
 
        raise ActionError("The source file does not exist")
282
 
    if os.path.exists(topath):
283
 
        if frompath == topath:
284
 
            raise ActionError("Source and destination are the same")
285
 
        raise ActionError("A file already exists with that name")
286
 
 
287
 
    try:
288
 
        if copy:
289
 
            svnclient.copy(frompath, topath)
290
 
        else:
291
 
            svnclient.move(frompath, topath)
292
 
    except OSError:
293
 
        raise ActionError("Could not move the file specified")
294
 
    except pysvn.ClientError:
295
 
        raise ActionError("Could not move the file specified")  
296
 
 
297
 
 
298
267
### ACTIONS ###
299
268
 
300
269
def action_delete(req, fields):
402
371
 
403
372
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
404
373
    """
 
374
 
405
375
    # Important: Data is "None" if the file submitted is empty.
406
376
    path = fields.getfirst('path')
407
377
    data = fields['data']
420
390
    for datum in data:
421
391
        # Each of the uploaded files
422
392
        filepath = os.path.join(path, datum.filename)
423
 
        (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
424
 
        if os.path.isdir(filepath_local):
425
 
            raise ActionError("A directory already exists "
426
 
                    + "with that name")
427
 
        else:
428
 
            if os.path.exists(filepath_local):
429
 
                raise ActionError("A file already exists with that name")
430
393
        filedata = datum.file
431
394
 
432
395
        if unpack and datum.filename.lower().endswith(".zip"):
441
404
                zip.unzip(abspath, filedata)
442
405
            except (OSError, IOError):
443
406
                goterror = True
444
 
            except WillNotOverwrite, e:
445
 
                raise ActionError("File '" + e.filename + "' already exists.")
446
407
        else:
447
408
            # Not a zip file
448
409
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
484
445
    files = fields.getlist('file')
485
446
    if dst is None or src is None or mode is None:
486
447
        raise ActionError("Required field missing")
487
 
 
 
448
    if mode == "copy":
 
449
        copy = True
 
450
    elif mode == "move":
 
451
        copy = False
 
452
    else:
 
453
        raise ActionError("Invalid mode (must be 'copy' or 'move')")
488
454
    dst_local = actionpath_to_local(req, dst)
489
455
    if not os.path.isdir(dst_local):
490
456
        raise ActionError("dst is not a directory")
499
465
        # The destination is found by taking just the basename of the file
500
466
        topath = os.path.join(dst, os.path.basename(file))
501
467
        try:
502
 
            if mode == "copy":
503
 
                movefile(req, frompath, topath, True)
504
 
            elif mode == "move":
505
 
                movefile(req, frompath, topath, False)
506
 
            elif mode == "svncopy":
507
 
                svn_movefile(req, frompath, topath, True)
508
 
            elif mode == "svnmove":
509
 
                svn_movefile(req, frompath, topath, False)
510
 
            else:
511
 
                raise ActionError("Invalid mode (must be '(svn)copy' or '(svn)move')")
 
468
            movefile(req, frompath, topath, copy)
512
469
        except ActionError, message:
513
470
            # Store the error for later; we want to copy as many as possible
514
471
            if errormsg is None:
738
695
def action_svnrepostat(req, fields):
739
696
    """Discovers whether a path exists in a repo under the IVLE SVN root.
740
697
 
741
 
    If it does exist, returns a dict containing its metadata.
742
 
 
743
698
    Reads fields: 'path'
744
699
    """
745
700
    path = fields.getfirst('path')
746
701
    url = ivle.conf.svn_addr + "/" + path
747
 
    svnclient.exception_style = 1
 
702
    svnclient.exception_style = 1 
748
703
 
749
704
    try:
750
705
        svnclient.callback_get_login = get_login
751
 
        info = svnclient.info2(url,
752
 
            revision=pysvn.Revision(pysvn.opt_revision_kind.head))[0][1]
753
 
        return {'svnrevision': info['rev'].number
754
 
                  if info['rev'] and
755
 
                     info['rev'].kind == pysvn.opt_revision_kind.number
756
 
                  else None}
 
706
        svnclient.info2(url)
757
707
    except pysvn.ClientError, e:
758
708
        # Error code 170000 means ENOENT in this revision.
759
709
        if e[1][0][1] == 170000:
760
710
            raise util.IVLEError(404, 'The specified repository path does not exist')
761
711
        else:
762
712
            raise ActionError(str(e[0]))
763
 
            
764
713
 
765
714
# Table of all action functions #
766
715
# Each function has the interface f(req, fields).