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

« back to all changes in this revision

Viewing changes to ivle/zip.py

Dispatch now generates an index for each plugin type, allowing plugins to
be written which are aware of other plugins, and other plugin types.

All view plugins now subclass from ivle.webapp.base.plugins.ViewPlugin,
as opposed to subclassing BasePlugin directly. This will allow us to
easily re-write console as an OverlayPlugin, and allow future new
plugins types to be created.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2008 The University of Melbourne
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
# Module: zip
 
19
# Author: Matt Giuca
 
20
# Date: 17/1/2008
 
21
 
 
22
# ZIP function wrappers. Provides easy methods for reading and writing zip
 
23
# files for the purpose of the IVLE file browser.
 
24
 
 
25
import os
 
26
import os.path
 
27
import zipfile
 
28
 
 
29
from ivle import studpath
 
30
from ivle.fileservice_lib.exceptions import WillNotOverwrite
 
31
 
 
32
def make_zip(basepath, paths, file):
 
33
    """Zips up a bunch of files on the student file space and writes it as
 
34
    a zip file.
 
35
 
 
36
    basepath: Path relative to student home. All file paths will be made
 
37
    relative to this path, such that unzipping the file in this path will
 
38
    place the files back in their original places.
 
39
    paths: List of paths relative to basepath. These are the files to be
 
40
    zipped. All paths must be relative.
 
41
    file: Either a filename to write to, or a file-like object.
 
42
 
 
43
    Throws an OSError if one or more of the files cannot be read. This error
 
44
    will be thrown before any writing takes place.
 
45
    """
 
46
    # First make sure all the files are valid
 
47
    newpaths = []       # Store tuples of (path, localpath)
 
48
    for path in paths:
 
49
        if len(path) == 0 or path[0] == os.sep:
 
50
            raise OSError("ZIP: Invalid path")
 
51
        else:
 
52
            # Relative to req.path
 
53
            relpath = os.path.join(basepath, path)
 
54
 
 
55
        #_, r = studpath.url_to_local(relpath)
 
56
        r = relpath
 
57
        if r is None:
 
58
            raise OSError("ZIP: Invalid path")
 
59
        if not os.access(r, os.R_OK):
 
60
            raise OSError("ZIP: Could not access a file")
 
61
        newpaths.append((path, r))
 
62
 
 
63
    # Now open the zip file and write each path to it
 
64
    zip = zipfile.ZipFile(file, 'w')
 
65
 
 
66
    for (path, localpath) in newpaths:
 
67
        if os.path.isdir(localpath):
 
68
            # Walk the directory tree
 
69
            if len(localpath) > 0 and localpath[-1] != os.sep:
 
70
                localpath += os.sep
 
71
            def error(err):
 
72
                raise OSError("ZIP: Could not access a file")
 
73
            for (dirpath, dirnames, filenames) in \
 
74
                os.walk(localpath, onerror=error):
 
75
                # Do not traverse into .svn directories
 
76
                try:
 
77
                    dirnames.remove(".svn")
 
78
                except ValueError:
 
79
                    pass
 
80
                # dirpath is local. Make arc_dirpath, relative to root
 
81
                arc_dirpath = os.path.join(path, dirpath[len(localpath):])
 
82
                for filename in filenames:
 
83
                    zip.write(os.path.join(dirpath, filename),
 
84
                                os.path.join(arc_dirpath, filename))
 
85
        else:
 
86
            zip.write(localpath, path)
 
87
 
 
88
    zip.close()
 
89
 
 
90
def unzip(path, file, overwrite=False):
 
91
    """Unzips a zip file (or file-like object) into a path.
 
92
    Note: All files go directly into the path. To avoid having a "zip bomb"
 
93
    situation, the zip file should have a single directory in it with all the
 
94
    files.
 
95
    The path is an absolute path in the current filesystem
 
96
    (if this code is executed inside the jail, then it's inside the jail).
 
97
    """
 
98
    zip = zipfile.ZipFile(file, 'r')
 
99
    # First test the zip file
 
100
    if zip.testzip() is not None:
 
101
        raise OSError("ZIP: Bad zip file")
 
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
 
 
109
    for filename in zip.namelist():
 
110
        localpath = os.path.join(path, filename)
 
111
        # Create directory for filename
 
112
        (file_dir, _) = os.path.split(localpath)
 
113
        if not os.path.exists(file_dir):
 
114
            os.makedirs(file_dir)
 
115
        
 
116
        if filename.endswith(os.sep):
 
117
            # Is a directory make the directory
 
118
            if not os.path.exists(localpath):
 
119
                os.mkdir(localpath)
 
120
        else:
 
121
            filedata = zip.read(filename)
 
122
            f = open(localpath, 'w')
 
123
            f.write(filedata)
 
124
            f.close()