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

« back to all changes in this revision

Viewing changes to ivle/zip.py

  • Committer: chadnickbok
  • Date: 2009-01-22 02:14:14 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1173
Fixes Issue #14

When uploading files, students will now be shown an
error message if there upload would replace already existing
files.

This upload will not be performed until it will not clobber
any old files.

Note that there is currently a way to change this behaviour in
the code in action.py, to allow files to be overwritten. However
there is no way this flag can be set through the browser interface
(yet).

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import zipfile
28
28
 
29
29
from ivle import studpath
 
30
from ivle.fileservice_lib.exceptions import WillNotOverwrite
30
31
 
31
32
def make_zip(basepath, paths, file):
32
33
    """Zips up a bunch of files on the student file space and writes it as
86
87
 
87
88
    zip.close()
88
89
 
89
 
def unzip(path, file):
 
90
def unzip(path, file, overwrite=False):
90
91
    """Unzips a zip file (or file-like object) into a path.
91
92
    Note: All files go directly into the path. To avoid having a "zip bomb"
92
93
    situation, the zip file should have a single directory in it with all the
93
94
    files.
94
95
    The path is an absolute path in the current filesystem
95
 
    (if this code is executed inside the jail, then it's inside the jail,
96
 
    if it's not then it's not).
 
96
    (if this code is executed inside the jail, then it's inside the jail).
97
97
    """
98
98
    zip = zipfile.ZipFile(file, 'r')
99
99
    # First test the zip file
100
100
    if zip.testzip() is not None:
101
101
        raise OSError("ZIP: Bad zip file")
102
102
 
 
103
    if (not overwrite):
 
104
        for filename in zip.namelist():
 
105
            localpath = os.path.join(path, filename)
 
106
            if os.path.exists(localpath):
 
107
                raise WillNotOverwrite(filename)
 
108
 
103
109
    for filename in zip.namelist():
104
110
        localpath = os.path.join(path, filename)
105
111
        # Create directory for filename
106
112
        (file_dir, _) = os.path.split(localpath)
107
113
        if not os.path.exists(file_dir):
108
114
            os.makedirs(file_dir)
109
 
 
 
115
        
110
116
        if filename.endswith(os.sep):
111
117
            # Is a directory make the directory
112
118
            if not os.path.exists(localpath):