1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
22
# ZIP function wrappers. Provides easy methods for reading and writing zip
23
# files for the purpose of the IVLE file browser.
29
from ivle import studpath
31
def make_zip(basepath, paths, file):
32
"""Zips up a bunch of files on the student file space and writes it as
35
basepath: Path relative to student home. All file paths will be made
36
relative to this path, such that unzipping the file in this path will
37
place the files back in their original places.
38
paths: List of paths relative to basepath. These are the files to be
39
zipped. All paths must be relative.
40
file: Either a filename to write to, or a file-like object.
42
Throws an OSError if one or more of the files cannot be read. This error
43
will be thrown before any writing takes place.
45
# First make sure all the files are valid
46
newpaths = [] # Store tuples of (path, localpath)
48
if len(path) == 0 or path[0] == os.sep:
49
raise OSError("ZIP: Invalid path")
51
# Relative to req.path
52
relpath = os.path.join(basepath, path)
54
#_, r = studpath.url_to_local(relpath)
57
raise OSError("ZIP: Invalid path")
58
if not os.access(r, os.R_OK):
59
raise OSError("ZIP: Could not access a file")
60
newpaths.append((path, r))
62
# Now open the zip file and write each path to it
63
zip = zipfile.ZipFile(file, 'w')
65
for (path, localpath) in newpaths:
66
if os.path.isdir(localpath):
67
# Walk the directory tree
68
if len(localpath) > 0 and localpath[-1] != os.sep:
71
raise OSError("ZIP: Could not access a file")
72
for (dirpath, dirnames, filenames) in \
73
os.walk(localpath, onerror=error):
74
# Do not traverse into .svn directories
76
dirnames.remove(".svn")
79
# dirpath is local. Make arc_dirpath, relative to root
80
arc_dirpath = os.path.join(path, dirpath[len(localpath):])
81
for filename in filenames:
82
zip.write(os.path.join(dirpath, filename),
83
os.path.join(arc_dirpath, filename))
85
zip.write(localpath, path)
89
def unzip(path, file, overwrite=False):
90
"""Unzips a zip file (or file-like object) into a path.
91
Note: All files go directly into the path. To avoid having a "zip bomb"
92
situation, the zip file should have a single directory in it with all the
94
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).
97
# XXX: Really bad, but circular imports need avoiding.
98
from ivle.fileservice_lib.exceptions import WillNotOverwrite
100
zip = zipfile.ZipFile(file, 'r')
101
# First test the zip file
102
if zip.testzip() is not None:
103
raise OSError("ZIP: Bad zip file")
106
for filename in zip.namelist():
107
localpath = os.path.join(path, filename)
108
if os.path.exists(localpath):
109
raise WillNotOverwrite(filename)
111
for filename in zip.namelist():
112
localpath = os.path.join(path, filename)
113
# Create directory for filename
114
(file_dir, _) = os.path.split(localpath)
115
if not os.path.exists(file_dir):
116
os.makedirs(file_dir)
118
if filename.endswith(os.sep):
119
# Is a directory make the directory
120
if not os.path.exists(localpath):
123
filedata = zip.read(filename)
124
f = open(localpath, 'w')