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

« back to all changes in this revision

Viewing changes to ivle/fileservice_lib/action.py

  • Committer: David Coles
  • Date: 2009-08-13 21:28:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1318.
  • Revision ID: coles.david@gmail.com-20090813212832-o3dq367fwa0d2y75
Set project.deadline to NOT NULL since it is required for submits.

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
import os
132
132
import cStringIO
133
133
import shutil
 
134
import urllib
134
135
 
135
136
import pysvn
136
137
 
137
138
from ivle import (util, studpath, zip)
 
139
from ivle.fileservice_lib.exceptions import WillNotOverwrite
138
140
import ivle.conf
139
141
 
 
142
 
140
143
def get_login(_realm, existing_login, _may_save):
141
144
    """Callback function used by pysvn for authentication.
142
145
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
149
152
    # If we're being asked again, then it means the credentials failed for
150
153
    # some reason and we should just fail. (This is not desirable, but it's
151
154
    # better than being asked an infinite number of times).
152
 
    return (existing_login != "", ivle.conf.login, ivle.conf.svn_pass, True)
 
155
    return (existing_login != "", str(ivle.conf.login),
 
156
                                  str(ivle.conf.svn_pass), True)
153
157
 
154
158
# Make a Subversion client object
155
159
svnclient = pysvn.Client()
194
198
    except KeyError:
195
199
        # Default, just send an error but then continue
196
200
        raise ActionError("Unknown action")
197
 
    action(req, fields)
 
201
    return action(req, fields)
198
202
 
199
203
def actionpath_to_urlpath(req, path):
200
204
    """Determines the URL path (relative to the student home) upon which the
225
229
 
226
230
    Does not mutate req.
227
231
    """
228
 
    (_, _, r) = studpath.url_to_jailpaths(actionpath_to_urlpath(req, path))
 
232
    r = studpath.to_home_path(actionpath_to_urlpath(req, path))
229
233
    if r is None:
230
234
        raise ActionError("Invalid path")
231
235
    return r
399
403
 
400
404
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
401
405
    """
402
 
 
403
406
    # Important: Data is "None" if the file submitted is empty.
404
407
    path = fields.getfirst('path')
405
408
    data = fields['data']
418
421
    for datum in data:
419
422
        # Each of the uploaded files
420
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")
421
431
        filedata = datum.file
422
432
 
423
433
        if unpack and datum.filename.lower().endswith(".zip"):
427
437
            # filename)
428
438
            try:
429
439
                # First get the entire path (within jail)
430
 
                _, _, abspath = studpath.url_to_jailpaths(path)
 
440
                abspath = studpath.to_home_path(path)
431
441
                abspath = os.path.join(os.sep, abspath)
432
442
                zip.unzip(abspath, filedata)
433
443
            except (OSError, IOError):
434
444
                goterror = True
 
445
            except WillNotOverwrite, e:
 
446
                raise ActionError("File '" + e.filename + "' already exists.")
435
447
        else:
436
448
            # Not a zip file
437
 
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
 
449
            filepath_local = studpath.to_home_path(filepath)
438
450
            if filepath_local is None:
439
451
                raise ActionError("Invalid path")
440
452
 
521
533
    Reads fields: 'path'
522
534
    """
523
535
    paths = fields.getlist('path')
524
 
    user = studpath.url_to_local(req.path)[0]
 
536
    user = util.split_path(req.path)[0]
525
537
    homedir = "/home/%s" % user
526
538
    if len(paths):
527
539
        paths = map(lambda path: actionpath_to_local(req, path), paths)
528
540
    else:
529
 
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
541
        paths = [studpath.to_home_path(req.path)]
530
542
 
531
543
    # Set all the dirs in home dir world browsable (o+r,o+x)
532
544
    #FIXME: Should really only do those in the direct path not all of the 
556
568
    if len(paths):
557
569
        paths = map(lambda path: actionpath_to_local(req, path), paths)
558
570
    else:
559
 
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
571
        paths = [studpath.to_home_path(req.path)]
560
572
 
561
573
    try:
562
574
        for path in paths:
653
665
    if len(paths):
654
666
        paths = map(lambda path: actionpath_to_local(req, path), paths)
655
667
    else:
656
 
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
668
        paths = [studpath.to_home_path(req.path)]
657
669
 
658
670
    try:
659
671
        for path in paths:
702
714
    paths = fields.getlist('path')
703
715
    if len(paths) != 2:
704
716
        raise ActionError("usage: svncheckout url local-path")
705
 
    url = ivle.conf.svn_addr + "/" + paths[0]
 
717
    url = ivle.conf.svn_addr + "/" + urllib.quote(paths[0])
706
718
    local_path = actionpath_to_local(req, str(paths[1]))
707
719
    try:
708
720
        svnclient.callback_get_login = get_login
727
739
def action_svnrepostat(req, fields):
728
740
    """Discovers whether a path exists in a repo under the IVLE SVN root.
729
741
 
 
742
    If it does exist, returns a dict containing its metadata.
 
743
 
730
744
    Reads fields: 'path'
731
745
    """
732
746
    path = fields.getfirst('path')
733
747
    url = ivle.conf.svn_addr + "/" + path
734
 
    svnclient.exception_style = 1 
 
748
    svnclient.exception_style = 1
735
749
 
736
750
    try:
737
751
        svnclient.callback_get_login = get_login
738
 
        svnclient.info2(url)
 
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}
739
758
    except pysvn.ClientError, e:
740
759
        # Error code 170000 means ENOENT in this revision.
741
760
        if e[1][0][1] == 170000: