mirror of
https://github.com/python/cpython.git
synced 2025-07-23 19:25:40 +00:00
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
'remove_tree()' can cooperate with 'mkpath()' in the maintenance of the PATH_CREATED cache: specifically, if a directory is created with 'mkpath()', later removed with 'remove_tree()', and 'mkpath()' is again requested to create it, then it would erroneously think the directory already existed, because it was in the PATH_CREATED cache. The patch (slightly tweaked by me) fixes that.
This commit is contained in:
parent
c566232c4d
commit
039accfb2c
1 changed files with 25 additions and 10 deletions
|
@ -180,19 +180,34 @@ def copy_tree (src, dst,
|
||||||
|
|
||||||
# copy_tree ()
|
# copy_tree ()
|
||||||
|
|
||||||
|
# Helper for remove_tree()
|
||||||
|
def _build_cmdtuple(path, cmdtuples):
|
||||||
|
for f in os.listdir(path):
|
||||||
|
real_f = os.path.join(path,f)
|
||||||
|
if os.path.isdir(real_f) and not os.path.islink(real_f):
|
||||||
|
_build_cmdtuple(real_f, cmdtuples)
|
||||||
|
else:
|
||||||
|
cmdtuples.append((os.remove, real_f))
|
||||||
|
cmdtuples.append((os.rmdir, path))
|
||||||
|
|
||||||
|
|
||||||
def remove_tree (directory, verbose=0, dry_run=0):
|
def remove_tree (directory, verbose=0, dry_run=0):
|
||||||
"""Recursively remove an entire directory tree. Any errors are ignored
|
"""Recursively remove an entire directory tree. Any errors are ignored
|
||||||
(apart from being reported to stdout if 'verbose' is true)."""
|
(apart from being reported to stdout if 'verbose' is true)."""
|
||||||
|
|
||||||
from shutil import rmtree
|
global PATH_CREATED
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print "removing '%s' (and everything under it)" % directory
|
print "removing '%s' (and everything under it)" % directory
|
||||||
if dry_run:
|
if dry_run:
|
||||||
return
|
return
|
||||||
|
cmdtuples = []
|
||||||
|
_build_cmdtuple(directory, cmdtuples)
|
||||||
|
for cmd in cmdtuples:
|
||||||
try:
|
try:
|
||||||
rmtree(directory,1)
|
apply(cmd[0], (cmd[1],))
|
||||||
|
# remove dir from cache if it's already there
|
||||||
|
if PATH_CREATED.has_key(cmd[1]):
|
||||||
|
del PATH_CREATED[cmd[1]]
|
||||||
except (IOError, OSError), exc:
|
except (IOError, OSError), exc:
|
||||||
if verbose:
|
if verbose:
|
||||||
if exc.filename:
|
if exc.filename:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue