91
91
# action=svnupdate: Bring a file up to date with the head revision.
92
92
# path: The path to the file to be updated. Only one file may be
94
# revision: The revision number to update to. If not provided this
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
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
97
103
# action=svncommit: Commit a file(s) or directory(s) to the repository.
98
104
# path: The path to the file or directory to be committed. Can be
113
119
# path: The path to the directory to be checked (under the IVLE
114
120
# repository base).
116
# action=svncleanup: Recursively clean up the working copy, removing locks,
117
# resuming unfinished operations, etc.
118
# path: The path to the directory to be cleaned
120
122
# TODO: Implement the following actions:
121
123
# svnupdate (done?)
122
124
# TODO: Implement ZIP unpacking in putfiles (done?).
136
137
from ivle import (util, studpath, zip)
137
from ivle.fileservice_lib.exceptions import WillNotOverwrite
141
# Make a Subversion client object (which will log in with this user's
142
# credentials, upon request)
143
svnclient = ivle.svn.create_auth_svn_client(username=ivle.conf.login,
144
password=ivle.conf.svn_pass)
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 != "", str(ivle.conf.login),
153
str(ivle.conf.svn_pass), True)
155
# Make a Subversion client object
156
svnclient = pysvn.Client()
157
svnclient.callback_get_login = get_login
145
158
svnclient.exception_style = 0 # Simple (string) exceptions
147
160
DEFAULT_LOGMESSAGE = "No log message supplied."
251
264
except shutil.Error:
252
265
raise ActionError("Could not move the file specified")
254
def svn_movefile(req, frompath, topath, copy=False):
255
"""Performs an svn move, resolving filenames, checking for any errors,
256
and throwing ActionErrors if necessary. Can also be used to do a copy
259
frompath and topath are straight paths from the client. Will be checked.
261
if frompath is None or topath is None:
262
raise ActionError("Required field missing")
263
frompath = actionpath_to_local(req, frompath)
264
topath = actionpath_to_local(req, topath)
265
if not os.path.exists(frompath):
266
raise ActionError("The source file does not exist")
267
if os.path.exists(topath):
268
if frompath == topath:
269
raise ActionError("Source and destination are the same")
270
raise ActionError("A file already exists with that name")
274
svnclient.copy(frompath, topath)
276
svnclient.move(frompath, topath)
278
raise ActionError("Could not move the file specified")
279
except pysvn.ClientError:
280
raise ActionError("Could not move the file specified")
285
269
def action_delete(req, fields):
405
390
for datum in data:
406
391
# Each of the uploaded files
407
392
filepath = os.path.join(path, datum.filename)
408
filepath_local = studpath.to_home_path(filepath)
409
if os.path.isdir(filepath_local):
410
raise ActionError("A directory already exists "
413
if os.path.exists(filepath_local):
414
raise ActionError("A file already exists with that name")
415
393
filedata = datum.file
417
395
if unpack and datum.filename.lower().endswith(".zip"):
423
401
# First get the entire path (within jail)
424
abspath = studpath.to_home_path(path)
402
_, _, abspath = studpath.url_to_jailpaths(path)
425
403
abspath = os.path.join(os.sep, abspath)
426
404
zip.unzip(abspath, filedata)
427
405
except (OSError, IOError):
429
except WillNotOverwrite, e:
430
raise ActionError("File '" + e.filename + "' already exists.")
433
filepath_local = studpath.to_home_path(filepath)
409
(_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
434
410
if filepath_local is None:
435
411
raise ActionError("Invalid path")
469
445
files = fields.getlist('file')
470
446
if dst is None or src is None or mode is None:
471
447
raise ActionError("Required field missing")
453
raise ActionError("Invalid mode (must be 'copy' or 'move')")
473
454
dst_local = actionpath_to_local(req, dst)
474
455
if not os.path.isdir(dst_local):
475
456
raise ActionError("dst is not a directory")
484
465
# The destination is found by taking just the basename of the file
485
466
topath = os.path.join(dst, os.path.basename(file))
488
movefile(req, frompath, topath, True)
490
movefile(req, frompath, topath, False)
491
elif mode == "svncopy":
492
svn_movefile(req, frompath, topath, True)
493
elif mode == "svnmove":
494
svn_movefile(req, frompath, topath, False)
496
raise ActionError("Invalid mode (must be '(svn)copy' or '(svn)move')")
468
movefile(req, frompath, topath, copy)
497
469
except ActionError, message:
498
470
# Store the error for later; we want to copy as many as possible
499
471
if errormsg is None:
517
489
Reads fields: 'path'
519
491
paths = fields.getlist('path')
520
user = util.split_path(req.path)[0]
492
user = studpath.url_to_local(req.path)[0]
521
493
homedir = "/home/%s" % user
523
495
paths = map(lambda path: actionpath_to_local(req, path), paths)
525
paths = [studpath.to_home_path(req.path)]
497
paths = [studpath.url_to_jailpaths(req.path)[2]]
527
499
# Set all the dirs in home dir world browsable (o+r,o+x)
528
500
#FIXME: Should really only do those in the direct path not all of the
572
544
Reads fields: 'path' (multiple)
574
546
paths = fields.getlist('path')
575
paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
547
paths = map(lambda path: actionpath_to_local(req, path), paths)
579
550
svnclient.add(paths, recurse=True, force=True)
586
557
Reads fields: 'path' (multiple)
588
559
paths = fields.getlist('path')
589
paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
560
paths = map(lambda path: actionpath_to_local(req, path), paths)
593
563
svnclient.remove(paths, force=True)
597
567
def action_svnupdate(req, fields):
598
568
"""Performs a "svn update" to each file specified.
600
Reads fields: 'path' and 'revision'
602
572
path = fields.getfirst('path')
603
revision = fields.getfirst('revision')
605
574
raise ActionError("Required field missing")
607
revision = pysvn.Revision( pysvn.opt_revision_kind.head )
610
revision = pysvn.Revision(pysvn.opt_revision_kind.number,
612
except ValueError, e:
613
raise ActionError("Bad revision number: '%s'"%revision,)
614
path = actionpath_to_local(req, path).decode('utf-8')
575
path = actionpath_to_local(req, path)
617
svnclient.update(path, recurse=True, revision=revision)
578
svnclient.update(path, recurse=True)
618
579
except pysvn.ClientError, e:
619
580
raise ActionError(str(e))
639
600
Reads fields: 'path' (multiple)
641
602
paths = fields.getlist('path')
642
paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
603
paths = map(lambda path: actionpath_to_local(req, path), paths)
646
606
svnclient.revert(paths, recurse=True)
647
607
except pysvn.ClientError, e:
648
608
raise ActionError(str(e))
610
def action_svnpublish(req, fields):
611
"""Sets svn property "ivle:published" on each file specified.
612
Should only be called on directories (only effective on directories
617
XXX Currently unused by the client (calls action_publish instead, which
618
has a completely different publishing model).
620
paths = fields.getlist('path')
622
paths = map(lambda path: actionpath_to_local(req, path), paths)
624
paths = [studpath.url_to_jailpaths(req.path)[2]]
628
# Note: Property value doesn't matter
629
svnclient.propset("ivle:published", "", path, recurse=False)
630
except pysvn.ClientError, e:
631
raise ActionError("Directory could not be published")
633
def action_svnunpublish(req, fields):
634
"""Deletes svn property "ivle:published" on each file specified.
638
XXX Currently unused by the client (calls action_unpublish instead, which
639
has a completely different publishing model).
641
paths = fields.getlist('path')
642
paths = map(lambda path: actionpath_to_local(req, path), paths)
646
svnclient.propdel("ivle:published", path, recurse=False)
647
except pysvn.ClientError, e:
648
raise ActionError("Directory could not be unpublished")
650
650
def action_svncommit(req, fields):
651
651
"""Performs a "svn commit" to each file specified.
653
653
Reads fields: 'path' (multiple), 'logmsg' (optional)
655
655
paths = fields.getlist('path')
657
paths = map(lambda path:actionpath_to_local(req,path).decode('utf-8'),
660
paths = [studpath.to_home_path(req.path).decode('utf-8')]
661
logmsg = str(fields.getfirst('logmsg',
662
DEFAULT_LOGMESSAGE)).decode('utf-8')
656
paths = map(lambda path: actionpath_to_local(req, str(path)), paths)
657
logmsg = str(fields.getfirst('logmsg', DEFAULT_LOGMESSAGE))
663
658
if logmsg == '': logmsg = DEFAULT_LOGMESSAGE
675
670
paths = fields.getlist('path')
676
671
if len(paths) != 2:
677
672
raise ActionError("usage: svncheckout url local-path")
678
url = ivle.conf.svn_addr + "/" + urllib.quote(paths[0])
673
url = ivle.conf.svn_addr + "/" + paths[0]
679
674
local_path = actionpath_to_local(req, str(paths[1]))
680
url = url.decode('utf-8')
681
local_path = local_path.decode('utf-8')
676
svnclient.callback_get_login = get_login
683
677
svnclient.checkout(url, local_path, recurse=True)
684
678
except pysvn.ClientError, e:
685
679
raise ActionError(str(e))
692
686
path = fields.getfirst('path')
693
687
logmsg = fields.getfirst('logmsg')
694
url = (ivle.conf.svn_addr + "/" + urllib.quote(path)).decode('utf-8')
688
url = ivle.conf.svn_addr + "/" + path
690
svnclient.callback_get_login = get_login
696
691
svnclient.mkdir(url, log_message=logmsg)
697
692
except pysvn.ClientError, e:
698
693
raise ActionError(str(e))
700
695
def action_svnrepostat(req, fields):
701
696
"""Discovers whether a path exists in a repo under the IVLE SVN root.
703
If it does exist, returns a dict containing its metadata.
705
698
Reads fields: 'path'
707
700
path = fields.getfirst('path')
708
url = (ivle.conf.svn_addr + "/" + urllib.quote(path)).decode('utf-8')
709
svnclient.exception_style = 1
701
url = ivle.conf.svn_addr + "/" + path
702
svnclient.exception_style = 1
712
info = svnclient.info2(url,
713
revision=pysvn.Revision(pysvn.opt_revision_kind.head))[0][1]
714
return {'svnrevision': info['rev'].number
716
info['rev'].kind == pysvn.opt_revision_kind.number
705
svnclient.callback_get_login = get_login
718
707
except pysvn.ClientError, e:
719
708
# Error code 170000 means ENOENT in this revision.
720
709
if e[1][0][1] == 170000:
723
712
raise ActionError(str(e[0]))
726
def action_svncleanup(req, fields):
727
"""Recursively clean up the working copy, removing locks, resuming
728
unfinished operations, etc.
729
path: The path to be cleaned"""
731
path = fields.getfirst('path')
733
raise ActionError("Required field missing")
734
path = actionpath_to_local(req, path).decode('utf-8')
737
svnclient.cleanup(path)
738
except pysvn.ClientError, e:
739
raise ActionError(str(e))
742
714
# Table of all action functions #
743
715
# Each function has the interface f(req, fields).
757
729
"svnupdate" : action_svnupdate,
758
730
"svnresolved" : action_svnresolved,
759
731
"svnrevert" : action_svnrevert,
732
"svnpublish" : action_svnpublish,
733
"svnunpublish" : action_svnunpublish,
760
734
"svncommit" : action_svncommit,
761
735
"svncheckout" : action_svncheckout,
762
736
"svnrepomkdir" : action_svnrepomkdir,
763
737
"svnrepostat" : action_svnrepostat,
764
"svncleanup" : action_svncleanup,