Commit 5acfea73 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

Make buildout.rmtree working with symlink as a path argument.

ref https://bugs.launchpad.net/zc.buildout/+bug/144228 .
parent abc869df
...@@ -24,6 +24,8 @@ def rmtree (path): ...@@ -24,6 +24,8 @@ def rmtree (path):
read only file. read only file.
This tries to chmod the file to writeable and retries before giving up. This tries to chmod the file to writeable and retries before giving up.
Also it tries to remove symlink itself if a symlink as passed as
path argument.
>>> from tempfile import mkdtemp >>> from tempfile import mkdtemp
Let's make a directory ... Let's make a directory ...
...@@ -39,6 +41,8 @@ def rmtree (path): ...@@ -39,6 +41,8 @@ def rmtree (path):
>>> foo = os.path.join (d, 'foo') >>> foo = os.path.join (d, 'foo')
>>> _ = open (foo, 'w').write ('huhu') >>> _ = open (foo, 'w').write ('huhu')
>>> bar = os.path.join (d, 'bar')
>>> os.symlink(bar, bar)
and make it unwriteable and make it unwriteable
...@@ -50,12 +54,49 @@ def rmtree (path): ...@@ -50,12 +54,49 @@ def rmtree (path):
and now the directory is gone and now the directory is gone
>>> os.path.isdir (d)
0
Let's make a directory ...
>>> d = mkdtemp()
and make sure it is actually there
>>> os.path.isdir (d)
1
Now create a broken symlink ...
>>> foo = os.path.join (d, 'foo')
>>> os.symlink(foo + '.not_exist', foo)
rmtree should be able to remove it:
>>> rmtree (foo)
and now the directory is gone
>>> os.path.isdir (foo)
0
cleanup directory
>>> rmtree (d)
and now the directory is gone
>>> os.path.isdir (d) >>> os.path.isdir (d)
0 0
""" """
def retry_writeable (func, path, exc): def retry_writeable (func, path, exc):
os.chmod (path, 384) # 0600 if func == os.path.islink:
func (path) os.unlink(path)
elif func == os.lstat and os.path.islink(path):
os.unlink(path)
else:
os.chmod (path, 384) # 0600
func (path)
shutil.rmtree (path, onerror = retry_writeable) shutil.rmtree (path, onerror = retry_writeable)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment