<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
On 01/10/13 15:18, Chris Larson wrote:
<blockquote
cite="mid:CABcZANnD8C4gZXEcRWfR8f_ftZjndOT9UJ5796SEigRqw6tohA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra"><br>
<div class="gmail_quote">On Thu, Jan 10, 2013 at 7:11 AM,
Chris Larson <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:clarson@kergoth.com" target="_blank">clarson@kergoth.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="adM">
<div class="h5"><br>
<div class="gmail_quote">On Thu, Jan 10, 2013 at 7:10
AM, Chris Larson <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:clarson@kergoth.com"
target="_blank">clarson@kergoth.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<div class="gmail_quote">On Thu, Jan 10, 2013 at
1:46 AM, Martin Ertsaas <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:martiert@gmail.com"
target="_blank">martiert@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px
#ccc solid;padding-left:1ex">
<div>On mac, os.unlink does not remove
directories, and we therefor have<br>
to explicitly use shutil.rmtree if the
path is a directory.<br>
---<br>
lib/bb/utils.py | 5 ++++-<br>
1 file changed, 4 insertions(+), 1
deletion(-)<br>
<br>
diff --git a/lib/bb/utils.py
b/lib/bb/utils.py<br>
index cef0fdd..8b6d3f5 100644<br>
--- a/lib/bb/utils.py<br>
+++ b/lib/bb/utils.py<br>
@@ -561,7 +561,10 @@ def remove(path,
recurse=False):<br>
import os, errno, shutil, glob<br>
for name in glob.glob(path):<br>
try:<br>
- os.unlink(name)<br>
+ if os.path.isdir(name):<br>
+ shutil.rmtree(name)<br>
+ else:<br>
+ os.unlink(name)<br>
except OSError as exc:<br>
if recurse and exc.errno ==
errno.EISDIR:<br>
shutil.rmtree(name)</div>
</blockquote>
</div>
<br>
<br>
</div>
Look 2 lines down, where it checks to see if the
os.unlink failed due to it being a directory and
runs shutil.rmtree if that's the case.</blockquote>
</div>
<br>
</div>
</div>
I'm guessing you're trying to use it without passing
recurse=True.</blockquote>
</div>
<br>
Having through about it further, I think it might be best to
be alter the code to also handle the case where recurse==False
and the path is a directory by calling os.rmdir(). Perhaps
this would reduce confusion.<br>
-- <br>
Christopher Larson</div>
</div>
</blockquote>
<br>
You are almost right. On normal folders, this will work with
recurse=True, but on /tmp folders you get: OSError: [Errno 1]
Operation not permitted: '/tmp/tmpl8qBDW' on the unlink call. This
makes the if test inside the except catching false, and nothing
happens.<br>
<br>
I'm not sure why this happens, as I am allowed to remove it using
shutil.rmtree. Making a folder in linux with the exact same
permissions does work however, so it seems to be some difference in
how the os handles the call.<br>
<br>
Other than that, I feel that it is cleaner to have an explicit check
like this, and not use the exception framework for control flow.
Which means in the except just use raise, or just ignore it and let
someone else handle it like we actually do.<br>
To get back the same functionality though, we should change the
isdir call to: 'if os.path.isdir(name) and recurse:'<br>
<br>
Are you totally opposed to changing this? What I don't want to do is
to add exc.errno == errno.EPERM to the line, which will just bloath
it.<br>
<br>
- Martin<br>
</body>
</html>