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

« back to all changes in this revision

Viewing changes to lib/fileservice_lib/action.py

  • Committer: wagrant
  • Date: 2008-08-19 12:49:58 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1031
Example worksheets: Fix the element names in worksheet 1 to be actually
                    correct. Also fix the first exercise so it too
                    functions properly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#       to:     The path of the target filename. Error if the file already
42
42
#               exists.
43
43
#
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
52
 
#               into it.
 
49
#       overwrite: Optional. If supplied, the file will be overwritten.
 
50
#               Otherwise, error if path already exists.
53
51
#
54
52
# action=putfiles: Upload multiple files to the student workspace, and
55
53
#                 optionally accept zip files which will be unpacked.
58
56
#       data:   A file upload (may not be a simple string). The filename
59
57
#               will be used to determine the target filename within
60
58
#               the given path.
61
 
#       unpack: Optional. If "true", if any data is a valid ZIP file,
 
59
#       unpack: Optional. If supplied, if any data is a valid ZIP file,
62
60
#               will create a directory instead and unpack the ZIP file
63
61
#               into it.
64
62
#
67
65
#               does. The dir will be made with this name.
68
66
#
69
67
# The differences between putfile and putfiles are:
70
 
# * putfile can only accept a single file.
 
68
# * putfile can only accept a single file, and can't unpack zipfiles.
71
69
# * putfile can accept string data, doesn't have to be a file upload.
72
70
# * putfile ignores the upload filename, the entire filename is specified on
73
71
#       path. putfiles calls files after the name on the user's machine.
112
110
#       path:   The [repository] path to the file or directory to be
113
111
#               checked out.
114
112
 
113
# action=svnrepomkdir: Create a directory in a repository (not WC).
 
114
#       path:   The path to the directory to be created (under the IVLE
 
115
#               repository base).
 
116
#       logmsg: Text of the log message.
 
117
 
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
 
120
#               repository base).
 
121
#
115
122
# TODO: Implement the following actions:
116
 
#   putfiles, svnrevert, svnupdate, svncommit
117
 
# TODO: Implement ZIP unpacking in putfile and putfiles.
 
123
#   svnupdate (done?)
 
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)
131
138
import conf
132
139
 
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
 
143
        callback_get_login.
 
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.
 
147
    """
 
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)
136
153
 
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
140
158
 
141
159
DEFAULT_LOGMESSAGE = "No log message supplied."
142
160
 
230
248
    if os.path.exists(topath):
231
249
        if frompath == topath:
232
250
            raise ActionError("Source and destination are the same")
233
 
        raise ActionError("Another file already exists with that name")
 
251
        raise ActionError("A file already exists with that name")
234
252
 
235
253
    try:
236
254
        if copy:
247
265
 
248
266
### ACTIONS ###
249
267
 
250
 
def action_remove(req, fields):
251
 
    # TODO: Do an SVN rm if the file is versioned.
 
268
def action_delete(req, fields):
252
269
    # TODO: Disallow removal of student's home directory
253
270
    """Removes a list of files or directories.
254
271
 
294
311
        raise ActionError("Required field missing")
295
312
    path = actionpath_to_local(req, path)
296
313
 
 
314
    if os.path.exists(path):
 
315
        raise ActionError("A file already exists with that name")
 
316
 
297
317
    # Create the directory
298
318
    try:
299
319
        os.mkdir(path)
304
324
    """Writes data to a file, overwriting it if it exists and creating it if
305
325
    it doesn't.
306
326
 
307
 
    Reads fields: 'path', 'data' (file upload)
 
327
    Reads fields: 'path', 'data' (file upload), 'overwrite'
308
328
    """
309
329
    # TODO: Read field "unpack".
310
330
    # Important: Data is "None" if the file submitted is empty.
321
341
    if data is not None:
322
342
        data = cStringIO.StringIO(data)
323
343
 
 
344
    overwrite = fields.getfirst('overwrite')
 
345
    if overwrite is None:
 
346
        overwrite = False
 
347
    else:
 
348
        overwrite = True
 
349
 
 
350
    if overwrite:
 
351
        # Overwrite files; but can't if it's a directory
 
352
        if os.path.isdir(path):
 
353
            raise ActionError("A directory already exists "
 
354
                    + "with that name")
 
355
    else:
 
356
        if os.path.exists(path):
 
357
            raise ActionError("A file already exists with that name")
 
358
 
324
359
    # Copy the contents of file object 'data' to the path 'path'
325
360
    try:
326
361
        dest = open(path, 'wb')
327
362
        if data is not None:
328
363
            shutil.copyfileobj(data, dest)
329
 
    except OSError:
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)
331
366
 
332
367
def action_putfiles(req, fields):
333
368
    """Writes data to one or more files in a directory, overwriting them if
351
386
    path = actionpath_to_urlpath(req, path)
352
387
    goterror = False
353
388
 
354
 
 
355
389
    for datum in data:
356
390
        # Each of the uploaded files
357
391
        filepath = os.path.join(path, datum.filename)
358
 
        filedata = datum.value
 
392
        filedata = datum.file
359
393
 
360
394
        if unpack and datum.filename.lower().endswith(".zip"):
361
395
            # A zip file - unpack it instead of just copying
363
397
            # Note: Just unzip into the current directory (ignore the
364
398
            # filename)
365
399
            try:
366
 
                zip.unzip(path, filedata)
 
400
                # First get the entire path (within jail)
 
401
                _, _, abspath = studpath.url_to_jailpaths(path)
 
402
                abspath = os.path.join(os.sep, abspath)
 
403
                zip.unzip(abspath, filedata)
367
404
            except (OSError, IOError):
368
405
                goterror = True
369
406
        else:
376
413
            try:
377
414
                dest = open(filepath_local, 'wb')
378
415
                if data is not None:
379
 
                    shutil.copyfileobj(cStringIO.StringIO(filedata), dest)
 
416
                    shutil.copyfileobj(filedata, dest)
380
417
            except OSError:
381
418
                goterror = True
382
419
 
443
480
    # XXX errorfiles contains a list of files that couldn't be pasted.
444
481
    # we currently do nothing with this.
445
482
 
 
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
 
486
 
 
487
    Reads fields: 'path'
 
488
    """
 
489
    paths = fields.getlist('path')
 
490
    user = studpath.url_to_local(req.path)[0]
 
491
    homedir = "/home/%s" % user
 
492
    if len(paths):
 
493
        paths = map(lambda path: actionpath_to_local(req, path), paths)
 
494
    else:
 
495
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
496
 
 
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)
 
502
 
 
503
    try:
 
504
        for path in paths:
 
505
            if os.path.isdir(path):
 
506
                pubfile = open(os.path.join(path,'.published'),'w')
 
507
                pubfile.write("This directory is published\n")
 
508
                pubfile.close()
 
509
            else:
 
510
                raise ActionError("Can only publish directories")
 
511
    except OSError, e:
 
512
        raise ActionError("Directory could not be published")
 
513
 
 
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 
 
517
    directories.
 
518
 
 
519
    Reads fields: 'path'
 
520
    """
 
521
    paths = fields.getlist('path')
 
522
    if len(paths):
 
523
        paths = map(lambda path: actionpath_to_local(req, path), paths)
 
524
    else:
 
525
        paths = [studpath.url_to_jailpaths(req.path)[2]]
 
526
 
 
527
    try:
 
528
        for path in paths:
 
529
            if os.path.isdir(path):
 
530
                pubfile = os.path.join(path,'.published')
 
531
                if os.path.isfile(pubfile):
 
532
                    os.remove(pubfile)
 
533
            else:
 
534
                raise ActionError("Can only unpublish directories")
 
535
    except OSError, e:
 
536
        raise ActionError("Directory could not be unpublished")
 
537
 
 
538
 
446
539
def action_svnadd(req, fields):
447
540
    """Performs a "svn add" to each file specified.
448
541
 
453
546
 
454
547
    try:
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))
 
551
 
 
552
def action_svnremove(req, fields):
 
553
    """Performs a "svn remove" on each file specified.
 
554
 
 
555
    Reads fields: 'path' (multiple)
 
556
    """
 
557
    paths = fields.getlist('path')
 
558
    paths = map(lambda path: actionpath_to_local(req, path), paths)
 
559
 
 
560
    try:
 
561
        svnclient.remove(paths, force=True)
 
562
    except pysvn.ClientError, e:
 
563
        raise ActionError(str(e))
458
564
 
459
565
def action_svnupdate(req, fields):
460
566
    """Performs a "svn update" to each file specified.
468
574
 
469
575
    try:
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))
 
579
 
 
580
def action_svnresolved(req, fields):
 
581
    """Performs a "svn resolved" to each file specified.
 
582
 
 
583
    Reads fields: 'path'
 
584
    """
 
585
    path = fields.getfirst('path')
 
586
    if path is None:
 
587
        raise ActionError("Required field missing")
 
588
    path = actionpath_to_local(req, path)
 
589
 
 
590
    try:
 
591
        svnclient.resolved(path, recurse=True)
 
592
    except pysvn.ClientError, e:
 
593
        raise ActionError(str(e))
473
594
 
474
595
def action_svnrevert(req, fields):
475
596
    """Performs a "svn revert" to each file specified.
481
602
 
482
603
    try:
483
604
        svnclient.revert(paths, recurse=True)
484
 
    except pysvn.ClientError:
485
 
        raise ActionError("One or more files could not be reverted")
 
605
    except pysvn.ClientError, e:
 
606
        raise ActionError(str(e))
486
607
 
487
608
def action_svnpublish(req, fields):
488
609
    """Sets svn property "ivle:published" on each file specified.
490
611
    anyway).
491
612
 
492
613
    Reads fields: 'path'
 
614
 
 
615
    XXX Currently unused by the client (calls action_publish instead, which
 
616
    has a completely different publishing model).
493
617
    """
494
618
    paths = fields.getlist('path')
495
619
    if len(paths):
508
632
    """Deletes svn property "ivle:published" on each file specified.
509
633
 
510
634
    Reads fields: 'path'
 
635
 
 
636
    XXX Currently unused by the client (calls action_unpublish instead, which
 
637
    has a completely different publishing model).
511
638
    """
512
639
    paths = fields.getlist('path')
513
640
    paths = map(lambda path: actionpath_to_local(req, path), paths)
515
642
    try:
516
643
        for path in paths:
517
644
            svnclient.propdel("ivle:published", path, recurse=False)
518
 
    except pysvn.ClientError:
 
645
    except pysvn.ClientError, e:
519
646
        raise ActionError("Directory could not be unpublished")
520
647
 
521
648
def action_svncommit(req, fields):
530
657
 
531
658
    try:
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))
535
662
 
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.
538
665
 
539
666
    Reads fields: 'path'    (multiple)
540
667
    """
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]))
546
673
    try:
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))
 
678
 
 
679
def action_svnrepomkdir(req, fields):
 
680
    """Performs a "svn mkdir" on a path under the IVLE SVN root.
 
681
 
 
682
    Reads fields: 'path'
 
683
    """
 
684
    path = fields.getfirst('path')
 
685
    logmsg = fields.getfirst('logmsg')
 
686
    url = conf.svn_addr + "/" + path
 
687
    try:
 
688
        svnclient.callback_get_login = get_login
 
689
        svnclient.mkdir(url, log_message=logmsg)
 
690
    except pysvn.ClientError, e:
 
691
        raise ActionError(str(e))
 
692
 
 
693
def action_svnrepostat(req, fields):
 
694
    """Discovers whether a path exists in a repo under the IVLE SVN root.
 
695
 
 
696
    Reads fields: 'path'
 
697
    """
 
698
    path = fields.getfirst('path')
 
699
    url = conf.svn_addr + "/" + path
 
700
    svnclient.exception_style = 1 
 
701
 
 
702
    try:
 
703
        svnclient.callback_get_login = get_login
 
704
        svnclient.info2(url)
 
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')
 
709
        else:
 
710
            raise ActionError(str(e[0]))
551
711
 
552
712
# Table of all action functions #
553
713
# Each function has the interface f(req, fields).
554
714
 
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,
562
724
 
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,
570
736
}