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

« back to all changes in this revision

Viewing changes to lib/common/zip.py

  • Committer: matt.giuca
  • Date: 2009-01-14 10:10:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1132
The new ivle.database.User class is now used in Request and usrmgt, which
    means it is now almost universally used in favour of ivle.user.User (now
    deprecated).

Noticeable change: The minor bug where the change to a user object in the
    database is not reflected in the user's session (eg. changing nick doesn't
    update title until log out).

ivle.dispatch:
    Session now contains 'login' (username string) rather than 'user' (full
        ivle.user.User object). This is a unicode string now.

    req.user is now a ivle.database.User object rather than an ivle.user.User
        object. This makes for a whole lot of really subtle differences, but
        largely conforms to the same interface. Note that strings must now all
        be unicode.

    login: Removed use of ivle.db. Now uses User object.

    html: Now handles unicode login and config options.

ivle.db: Removed update_user. Now replaced with Storm model.

ivle.database: Renamed has_cap back to hasCap (saved for later). Fixed small
    unicode bug.

ivle.makeuser.make_svn_auth now takes a store object.

usrmgt-server: Use new User class.

userservice: Now uses User class internally.
    get_user action now returns ISO 8601 date format, rather than a
        time tuple. (Wasn't being used).
    get_user action no longer transmits local_password (small security risk;
        note that it wasn't possible to see this for any user other than
        yourself unless admin).

ivle.util - added function object_to_dict.

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: common.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 common import studpath
30
 
 
31
 
def make_zip(basepath, paths, file):
32
 
    """Zips up a bunch of files on the student file space and writes it as
33
 
    a zip file.
34
 
 
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.
41
 
 
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.
44
 
    """
45
 
    # First make sure all the files are valid
46
 
    newpaths = []       # Store tuples of (path, localpath)
47
 
    for path in paths:
48
 
        if len(path) == 0 or path[0] == os.sep:
49
 
            raise OSError("ZIP: Invalid path")
50
 
        else:
51
 
            # Relative to req.path
52
 
            relpath = os.path.join(basepath, path)
53
 
 
54
 
        _, r = studpath.url_to_local(relpath)
55
 
        if r is None:
56
 
            raise OSError("ZIP: Invalid path")
57
 
        if not os.access(r, os.R_OK):
58
 
            raise OSError("ZIP: Could not access a file")
59
 
        newpaths.append((path, r))
60
 
 
61
 
    # Now open the zip file and write each path to it
62
 
    zip = zipfile.ZipFile(file, 'w')
63
 
 
64
 
    for (path, localpath) in newpaths:
65
 
        if os.path.isdir(localpath):
66
 
            # Walk the directory tree
67
 
            if len(localpath) > 0 and localpath[-1] != os.sep:
68
 
                localpath += os.sep
69
 
            def error(err):
70
 
                raise OSError("ZIP: Could not access a file")
71
 
            for (dirpath, dirnames, filenames) in \
72
 
                os.walk(localpath, onerror=error):
73
 
                # Do not traverse into .svn directories
74
 
                try:
75
 
                    dirnames.remove(".svn")
76
 
                except ValueError:
77
 
                    pass
78
 
                # dirpath is local. Make arc_dirpath, relative to root
79
 
                arc_dirpath = os.path.join(path, dirpath[len(localpath):])
80
 
                for filename in filenames:
81
 
                    zip.write(os.path.join(dirpath, filename),
82
 
                                os.path.join(arc_dirpath, filename))
83
 
        else:
84
 
            zip.write(localpath, path)
85
 
 
86
 
    zip.close()
87
 
 
88
 
def unzip(path, file):
89
 
    """Unzips a zip file (or file-like object) into a path.
90
 
    Note: All files go directly into the path. To avoid having a "zip bomb"
91
 
    situation, the zip file should have a single directory in it with all the
92
 
    files.
93
 
    """
94
 
    zip = zipfile.ZipFile(file, 'r')
95
 
    # First test the zip file
96
 
    if zip.testzip() is not None:
97
 
        raise OSError("ZIP: Bad zip file")
98
 
 
99
 
    for filename in zip.namelist():
100
 
        # Work out the name of this file on the local file system, and make
101
 
        # sure it is valid
102
 
        relpath = os.path.join(path, filename)
103
 
        _, localpath = studpath.url_to_local(relpath)
104
 
        if localpath is None:
105
 
            raise OSError("ZIP: Permission denied")
106
 
        # Create directory for filename
107
 
        (file_dir, _) = os.path.split(localpath)
108
 
        if not os.path.exists(file_dir):
109
 
            os.makedirs(file_dir)
110
 
 
111
 
        if filename.endswith(os.sep):
112
 
            # Is a directory make the directory
113
 
            if not os.path.exists(localpath):
114
 
                os.mkdir(localpath)
115
 
        else:
116
 
            filedata = zip.read(filename)
117
 
            f = open(localpath, 'w')
118
 
            f.write(filedata)
119
 
            f.close()