41
41
# to: The path of the target filename. Error if the file already
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.
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.
48
47
# data: Bytes to be written to the file verbatim. May either be
49
48
# 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
49
# overwrite: Optional. If supplied, the file will be overwritten.
50
# Otherwise, error if path already exists.
54
52
# action=putfiles: Upload multiple files to the student workspace, and
55
53
# optionally accept zip files which will be unpacked.
112
110
# path: The [repository] path to the file or directory to be
113
# action=svnrepomkdir: Create a directory in a repository (not WC).
114
# path: The path to the directory to be created (under the IVLE
116
# logmsg: Text of the log message.
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
115
122
# TODO: Implement the following actions:
116
# putfiles, svnrevert, svnupdate, svncommit
117
# TODO: Implement ZIP unpacking in putfile and putfiles.
124
# TODO: Implement ZIP unpacking in putfiles (done?).
118
125
# TODO: svnupdate needs a digest to tell the user the files that were updated.
119
126
# This can be implemented by some message passing between action and
120
127
# listing, and having the digest included in the listing. (Problem if
130
137
from common import (util, studpath, zip)
133
def get_login(_realm, _username, _may_save):
134
"""Return the subversion credentials for the user."""
135
return (True, conf.login, conf.passwd, True)
140
def get_login(_realm, existing_login, _may_save):
141
"""Callback function used by pysvn for authentication.
142
realm, existing_login, _may_save: The 3 arguments passed by pysvn to
144
The following has been determined empirically, not from docs:
145
existing_login will be the name of the user who owns the process on
146
the first attempt, "" on subsequent attempts. We use this fact.
148
# Only provide credentials on the _first_ attempt.
149
# If we're being asked again, then it means the credentials failed for
150
# some reason and we should just fail. (This is not desirable, but it's
151
# better than being asked an infinite number of times).
152
return (existing_login != "", conf.login, conf.svn_pass, True)
137
154
# Make a Subversion client object
138
155
svnclient = pysvn.Client()
139
156
svnclient.callback_get_login = get_login
157
svnclient.exception_style = 0 # Simple (string) exceptions
141
159
DEFAULT_LOGMESSAGE = "No log message supplied."
321
341
if data is not None:
322
342
data = cStringIO.StringIO(data)
344
overwrite = fields.getfirst('overwrite')
345
if overwrite is None:
351
# Overwrite files; but can't if it's a directory
352
if os.path.isdir(path):
353
raise ActionError("A directory already exists "
356
if os.path.exists(path):
357
raise ActionError("A file already exists with that name")
324
359
# Copy the contents of file object 'data' to the path 'path'
326
361
dest = open(path, 'wb')
327
362
if data is not None:
328
363
shutil.copyfileobj(data, dest)
330
raise ActionError("Could not write to target file")
364
except (IOError, OSError), e:
365
raise ActionError("Could not write to target file: %s" % e.strerror)
332
367
def action_putfiles(req, fields):
333
368
"""Writes data to one or more files in a directory, overwriting them if
443
480
# XXX errorfiles contains a list of files that couldn't be pasted.
444
481
# we currently do nothing with this.
483
def action_publish(req,fields):
484
"""Marks the folder as published by adding a '.published' file to the
485
directory and ensuring that the parent directory permissions are correct
489
paths = fields.getlist('path')
490
user = studpath.url_to_local(req.path)[0]
491
homedir = "/home/%s" % user
493
paths = map(lambda path: actionpath_to_local(req, path), paths)
495
paths = [studpath.url_to_jailpaths(req.path)[2]]
497
# Set all the dirs in home dir world browsable (o+r,o+x)
498
#FIXME: Should really only do those in the direct path not all of the
499
# folders in a students home directory
500
for root,dirs,files in os.walk(homedir):
501
os.chmod(root, os.stat(root).st_mode|0005)
505
if os.path.isdir(path):
506
pubfile = open(os.path.join(path,'.published'),'w')
507
pubfile.write("This directory is published\n")
510
raise ActionError("Can only publish directories")
512
raise ActionError("Directory could not be published")
514
def action_unpublish(req,fields):
515
"""Marks the folder as unpublished by removing a '.published' file in the
516
directory (if it exits). It does not change the permissions of the parent
521
paths = fields.getlist('path')
523
paths = map(lambda path: actionpath_to_local(req, path), paths)
525
paths = [studpath.url_to_jailpaths(req.path)[2]]
529
if os.path.isdir(path):
530
pubfile = os.path.join(path,'.published')
531
if os.path.isfile(pubfile):
534
raise ActionError("Can only unpublish directories")
536
raise ActionError("Directory could not be unpublished")
446
539
def action_svnadd(req, fields):
447
540
"""Performs a "svn add" to each file specified.
455
548
svnclient.add(paths, recurse=True, force=True)
456
except pysvn.ClientError:
457
raise ActionError("One or more files could not be added")
549
except pysvn.ClientError, e:
550
raise ActionError(str(e))
552
def action_svnremove(req, fields):
553
"""Performs a "svn remove" on each file specified.
555
Reads fields: 'path' (multiple)
557
paths = fields.getlist('path')
558
paths = map(lambda path: actionpath_to_local(req, path), paths)
561
svnclient.remove(paths, force=True)
562
except pysvn.ClientError, e:
563
raise ActionError(str(e))
459
565
def action_svnupdate(req, fields):
460
566
"""Performs a "svn update" to each file specified.
470
576
svnclient.update(path, recurse=True)
471
except pysvn.ClientError:
472
raise ActionError("One or more files could not be updated")
577
except pysvn.ClientError, e:
578
raise ActionError(str(e))
580
def action_svnresolved(req, fields):
581
"""Performs a "svn resolved" to each file specified.
585
path = fields.getfirst('path')
587
raise ActionError("Required field missing")
588
path = actionpath_to_local(req, path)
591
svnclient.resolved(path, recurse=True)
592
except pysvn.ClientError, e:
593
raise ActionError(str(e))
474
595
def action_svnrevert(req, fields):
475
596
"""Performs a "svn revert" to each file specified.
532
659
svnclient.checkin(paths, logmsg, recurse=True)
533
except pysvn.ClientError:
534
raise ActionError("One or more files could not be committed")
660
except pysvn.ClientError, e:
661
raise ActionError(str(e))
536
663
def action_svncheckout(req, fields):
537
"""Performs a "svn checkout" of each path specified.
664
"""Performs a "svn checkout" of the first path into the second path.
539
666
Reads fields: 'path' (multiple)
541
668
paths = fields.getlist('path')
542
669
if len(paths) != 2:
543
670
raise ActionError("usage: svncheckout url local-path")
544
url = conf.svn_addr + "/" + login + "/" + paths[0]
671
url = conf.svn_addr + "/" + paths[0]
545
672
local_path = actionpath_to_local(req, str(paths[1]))
547
674
svnclient.callback_get_login = get_login
548
675
svnclient.checkout(url, local_path, recurse=True)
549
except pysvn.ClientError:
550
raise ActionError("One or more files could not be checked out")
676
except pysvn.ClientError, e:
677
raise ActionError(str(e))
679
def action_svnrepomkdir(req, fields):
680
"""Performs a "svn mkdir" on a path under the IVLE SVN root.
684
path = fields.getfirst('path')
685
logmsg = fields.getfirst('logmsg')
686
url = conf.svn_addr + "/" + path
688
svnclient.callback_get_login = get_login
689
svnclient.mkdir(url, log_message=logmsg)
690
except pysvn.ClientError, e:
691
raise ActionError(str(e))
693
def action_svnrepostat(req, fields):
694
"""Discovers whether a path exists in a repo under the IVLE SVN root.
698
path = fields.getfirst('path')
699
url = conf.svn_addr + "/" + path
700
svnclient.exception_style = 1
703
svnclient.callback_get_login = get_login
705
except pysvn.ClientError, e:
706
# Error code 170000 means ENOENT in this revision.
707
if e[1][0][1] == 170000:
708
raise util.IVLEError(404, 'The specified repository path does not exist')
710
raise ActionError(str(e[0]))
552
712
# Table of all action functions #
553
713
# Each function has the interface f(req, fields).
555
715
actions_table = {
556
"remove" : action_remove,
716
"delete" : action_delete,
557
717
"move" : action_move,
558
718
"mkdir" : action_mkdir,
559
719
"putfile" : action_putfile,
560
720
"putfiles" : action_putfiles,
561
721
"paste" : action_paste,
722
"publish" : action_publish,
723
"unpublish" : action_unpublish,
563
725
"svnadd" : action_svnadd,
726
"svnremove" : action_svnremove,
564
727
"svnupdate" : action_svnupdate,
728
"svnresolved" : action_svnresolved,
565
729
"svnrevert" : action_svnrevert,
566
730
"svnpublish" : action_svnpublish,
567
731
"svnunpublish" : action_svnunpublish,
568
732
"svncommit" : action_svncommit,
569
733
"svncheckout" : action_svncheckout,
734
"svnrepomkdir" : action_svnrepomkdir,
735
"svnrepostat" : action_svnrepostat,