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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: Matt Giuca
  • Date: 2009-05-12 14:36:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1247.
  • Revision ID: matt.giuca@gmail.com-20090512143608-94oi7lkkzoougv09
ivle/util.py: Added safe_rmtree, copied from Python2.6 shutil.rmtree (which
    has a bugfix to avoid following symlinks). Don't use shutil.rmtree.
bin/ivle-fetchsubmissions: Replaced call to shutil.rmtree with safe_rmtree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# Contains common utility functions.
23
23
 
24
24
import os
 
25
import sys
 
26
import stat
25
27
 
26
28
class IVLEError(Exception):
27
29
    """Legacy general IVLE exception.
184
186
    """
185
187
    return dict((k, getattr(obj, k))
186
188
        for k in attrnames if not k.startswith('_'))
 
189
 
 
190
def safe_rmtree(path, ignore_errors=False, onerror=None):
 
191
    """Recursively delete a directory tree.
 
192
 
 
193
    Copied from shutil.rmtree from Python 2.6, which does not follow symbolic
 
194
    links (it is otherwise unsafe to call as root on untrusted directories; do
 
195
    not use shutil.rmtree in this case, as you may be running Python 2.5).
 
196
 
 
197
    If ignore_errors is set, errors are ignored; otherwise, if onerror
 
198
    is set, it is called to handle the error with arguments (func,
 
199
    path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
 
200
    path is the argument to that function that caused it to fail; and
 
201
    exc_info is a tuple returned by sys.exc_info().  If ignore_errors
 
202
    is false and onerror is None, an exception is raised.
 
203
 
 
204
    """
 
205
    if ignore_errors:
 
206
        def onerror(*args):
 
207
            pass
 
208
    elif onerror is None:
 
209
        def onerror(*args):
 
210
            raise
 
211
    try:
 
212
        if os.path.islink(path):
 
213
            # symlinks to directories are forbidden, see bug #1669
 
214
            raise OSError("Cannot call safe_rmtree on a symbolic link")
 
215
    except OSError:
 
216
        onerror(os.path.islink, path, sys.exc_info())
 
217
        # can't continue even if onerror hook returns
 
218
        return
 
219
    names = []
 
220
    try:
 
221
        names = os.listdir(path)
 
222
    except os.error, err:
 
223
        onerror(os.listdir, path, sys.exc_info())
 
224
    for name in names:
 
225
        fullname = os.path.join(path, name)
 
226
        try:
 
227
            mode = os.lstat(fullname).st_mode
 
228
        except os.error:
 
229
            mode = 0
 
230
        if stat.S_ISDIR(mode):
 
231
            safe_rmtree(fullname, ignore_errors, onerror)
 
232
        else:
 
233
            try:
 
234
                os.remove(fullname)
 
235
            except os.error, err:
 
236
                onerror(os.remove, fullname, sys.exc_info())
 
237
    try:
 
238
        os.rmdir(path)
 
239
    except os.error:
 
240
        onerror(os.rmdir, path, sys.exc_info())