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 != "", ivle.conf.login, ivle.conf.svn_pass, True)
154
# Make a Subversion client object
155
svnclient = pysvn.Client()
156
svnclient.callback_get_login = get_login
145
157
svnclient.exception_style = 0 # Simple (string) exceptions
147
159
DEFAULT_LOGMESSAGE = "No log message supplied."
251
263
except shutil.Error:
252
264
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
268
def action_delete(req, fields):
318
301
frompath = fields.getfirst('from')
319
302
topath = fields.getfirst('to')
320
svn = fields.getfirst('svn')
322
svn_movefile(req, frompath, topath)
324
movefile(req, frompath, topath)
303
movefile(req, frompath, topath)
326
305
def action_mkdir(req, fields):
327
306
"""Creates a directory with the given path.
409
389
for datum in data:
410
390
# Each of the uploaded files
411
391
filepath = os.path.join(path, datum.filename)
412
filepath_local = studpath.to_home_path(filepath)
413
if os.path.isdir(filepath_local):
414
raise ActionError("A directory already exists "
417
if os.path.exists(filepath_local):
418
raise ActionError("A file already exists with that name")
419
392
filedata = datum.file
421
394
if unpack and datum.filename.lower().endswith(".zip"):
427
400
# First get the entire path (within jail)
428
abspath = studpath.to_home_path(path)
401
_, _, abspath = studpath.url_to_jailpaths(path)
429
402
abspath = os.path.join(os.sep, abspath)
430
403
zip.unzip(abspath, filedata)
431
404
except (OSError, IOError):
433
except WillNotOverwrite, e:
434
raise ActionError("File '" + e.filename + "' already exists.")
437
filepath_local = studpath.to_home_path(filepath)
408
(_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
438
409
if filepath_local is None:
439
410
raise ActionError("Invalid path")
473
444
files = fields.getlist('file')
474
445
if dst is None or src is None or mode is None:
475
446
raise ActionError("Required field missing")
452
raise ActionError("Invalid mode (must be 'copy' or 'move')")
477
453
dst_local = actionpath_to_local(req, dst)
478
454
if not os.path.isdir(dst_local):
479
455
raise ActionError("dst is not a directory")
488
464
# The destination is found by taking just the basename of the file
489
465
topath = os.path.join(dst, os.path.basename(file))
492
movefile(req, frompath, topath, True)
494
movefile(req, frompath, topath, False)
495
elif mode == "svncopy":
496
svn_movefile(req, frompath, topath, True)
497
elif mode == "svnmove":
498
svn_movefile(req, frompath, topath, False)
500
raise ActionError("Invalid mode (must be '(svn)copy' or '(svn)move')")
467
movefile(req, frompath, topath, copy)
501
468
except ActionError, message:
502
469
# Store the error for later; we want to copy as many as possible
503
470
if errormsg is None:
521
488
Reads fields: 'path'
523
490
paths = fields.getlist('path')
524
user = util.split_path(req.path)[0]
491
user = studpath.url_to_local(req.path)[0]
525
492
homedir = "/home/%s" % user
527
494
paths = map(lambda path: actionpath_to_local(req, path), paths)
529
paths = [studpath.to_home_path(req.path)]
496
paths = [studpath.url_to_jailpaths(req.path)[2]]
531
498
# Set all the dirs in home dir world browsable (o+r,o+x)
532
499
#FIXME: Should really only do those in the direct path not all of the
576
543
Reads fields: 'path' (multiple)
578
545
paths = fields.getlist('path')
579
paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
546
paths = map(lambda path: actionpath_to_local(req, path), paths)
583
549
svnclient.add(paths, recurse=True, force=True)
590
556
Reads fields: 'path' (multiple)
592
558
paths = fields.getlist('path')
593
paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
559
paths = map(lambda path: actionpath_to_local(req, path), paths)
597
562
svnclient.remove(paths, force=True)
601
566
def action_svnupdate(req, fields):
602
567
"""Performs a "svn update" to each file specified.
604
Reads fields: 'path' and 'revision'
606
571
path = fields.getfirst('path')
607
revision = fields.getfirst('revision')
609
573
raise ActionError("Required field missing")
611
revision = pysvn.Revision( pysvn.opt_revision_kind.head )
614
revision = pysvn.Revision(pysvn.opt_revision_kind.number,
616
except ValueError, e:
617
raise ActionError("Bad revision number: '%s'"%revision,)
618
path = actionpath_to_local(req, path).decode('utf-8')
574
path = actionpath_to_local(req, path)
621
svnclient.update(path, recurse=True, revision=revision)
577
svnclient.update(path, recurse=True)
622
578
except pysvn.ClientError, e:
623
579
raise ActionError(str(e))
643
599
Reads fields: 'path' (multiple)
645
601
paths = fields.getlist('path')
646
paths = map(lambda path: actionpath_to_local(req, path).decode('utf-8'),
602
paths = map(lambda path: actionpath_to_local(req, path), paths)
650
605
svnclient.revert(paths, recurse=True)
651
606
except pysvn.ClientError, e:
652
607
raise ActionError(str(e))
609
def action_svnpublish(req, fields):
610
"""Sets svn property "ivle:published" on each file specified.
611
Should only be called on directories (only effective on directories
616
XXX Currently unused by the client (calls action_publish instead, which
617
has a completely different publishing model).
619
paths = fields.getlist('path')
621
paths = map(lambda path: actionpath_to_local(req, path), paths)
623
paths = [studpath.url_to_jailpaths(req.path)[2]]
627
# Note: Property value doesn't matter
628
svnclient.propset("ivle:published", "", path, recurse=False)
629
except pysvn.ClientError, e:
630
raise ActionError("Directory could not be published")
632
def action_svnunpublish(req, fields):
633
"""Deletes svn property "ivle:published" on each file specified.
637
XXX Currently unused by the client (calls action_unpublish instead, which
638
has a completely different publishing model).
640
paths = fields.getlist('path')
641
paths = map(lambda path: actionpath_to_local(req, path), paths)
645
svnclient.propdel("ivle:published", path, recurse=False)
646
except pysvn.ClientError, e:
647
raise ActionError("Directory could not be unpublished")
654
649
def action_svncommit(req, fields):
655
650
"""Performs a "svn commit" to each file specified.
657
652
Reads fields: 'path' (multiple), 'logmsg' (optional)
659
654
paths = fields.getlist('path')
661
paths = map(lambda path:actionpath_to_local(req,path).decode('utf-8'),
664
paths = [studpath.to_home_path(req.path).decode('utf-8')]
665
logmsg = str(fields.getfirst('logmsg',
666
DEFAULT_LOGMESSAGE)).decode('utf-8')
655
paths = map(lambda path: actionpath_to_local(req, str(path)), paths)
656
logmsg = str(fields.getfirst('logmsg', DEFAULT_LOGMESSAGE))
667
657
if logmsg == '': logmsg = DEFAULT_LOGMESSAGE
679
669
paths = fields.getlist('path')
680
670
if len(paths) != 2:
681
671
raise ActionError("usage: svncheckout url local-path")
682
url = ivle.conf.svn_addr + "/" + urllib.quote(paths[0])
672
url = ivle.conf.svn_addr + "/" + paths[0]
683
673
local_path = actionpath_to_local(req, str(paths[1]))
684
url = url.decode('utf-8')
685
local_path = local_path.decode('utf-8')
675
svnclient.callback_get_login = get_login
687
676
svnclient.checkout(url, local_path, recurse=True)
688
677
except pysvn.ClientError, e:
689
678
raise ActionError(str(e))
696
685
path = fields.getfirst('path')
697
686
logmsg = fields.getfirst('logmsg')
698
url = (ivle.conf.svn_addr + "/" + urllib.quote(path)).decode('utf-8')
687
url = ivle.conf.svn_addr + "/" + path
689
svnclient.callback_get_login = get_login
700
690
svnclient.mkdir(url, log_message=logmsg)
701
691
except pysvn.ClientError, e:
702
692
raise ActionError(str(e))
704
694
def action_svnrepostat(req, fields):
705
695
"""Discovers whether a path exists in a repo under the IVLE SVN root.
707
If it does exist, returns a dict containing its metadata.
709
697
Reads fields: 'path'
711
699
path = fields.getfirst('path')
712
url = (ivle.conf.svn_addr + "/" + urllib.quote(path)).decode('utf-8')
713
svnclient.exception_style = 1
700
url = ivle.conf.svn_addr + "/" + path
701
svnclient.exception_style = 1
716
info = svnclient.info2(url,
717
revision=pysvn.Revision(pysvn.opt_revision_kind.head))[0][1]
718
return {'svnrevision': info['rev'].number
720
info['rev'].kind == pysvn.opt_revision_kind.number
704
svnclient.callback_get_login = get_login
722
706
except pysvn.ClientError, e:
723
707
# Error code 170000 means ENOENT in this revision.
724
708
if e[1][0][1] == 170000:
726
raise ActionError('The specified repository path does not exist')
709
raise util.IVLEError(404, 'The specified repository path does not exist')
728
711
raise ActionError(str(e[0]))
731
def action_svncleanup(req, fields):
732
"""Recursively clean up the working copy, removing locks, resuming
733
unfinished operations, etc.
734
path: The path to be cleaned"""
736
path = fields.getfirst('path')
738
raise ActionError("Required field missing")
739
path = actionpath_to_local(req, path).decode('utf-8')
742
svnclient.cleanup(path)
743
except pysvn.ClientError, e:
744
raise ActionError(str(e))
747
713
# Table of all action functions #
748
714
# Each function has the interface f(req, fields).
762
728
"svnupdate" : action_svnupdate,
763
729
"svnresolved" : action_svnresolved,
764
730
"svnrevert" : action_svnrevert,
731
"svnpublish" : action_svnpublish,
732
"svnunpublish" : action_svnunpublish,
765
733
"svncommit" : action_svncommit,
766
734
"svncheckout" : action_svncheckout,
767
735
"svnrepomkdir" : action_svnrepomkdir,
768
736
"svnrepostat" : action_svnrepostat,
769
"svncleanup" : action_svncleanup,