Patchwork [08/10] task.py: When the dir to clean is a symlink rmtree fails. Catch the error and unlink

login
register
mail settings
Submitter Esben Haabendal
Date Oct. 31, 2012, 9:20 a.m.
Message ID <87ip9rf16m.fsf@arh128.prevas.dk>
Download mbox | patch
Permalink /patch/195775/
State Accepted
Headers show

Comments

Esben Haabendal - Oct. 31, 2012, 9:20 a.m.
Esben Haabendal <esben.haabendal@dev.prevas.dk> writes:

> Morten Thunberg Svendsen <Morten.ThunbergSvendsen@prevas.dk> writes:
>
>> On 2012-02-09 10:49, Esben Haabendal wrote:
>>> Morten Thunberg Svendsen <moth@prevas.dk> writes:
>>> 
>>>> From: Morten Thunberg Svendsen <morten.thunbergsvendsen@prevas.dk>
>>>>
>>>> archive-image.oeclass creates symlink, and if archiving fails it remains
>>>> until next build and the rmtree would fail.
>>>> ---
>>>>  lib/oelite/task.py |    2 ++
>>>>  1 files changed, 2 insertions(+), 0 deletions(-)
>>>>
>>>> diff --git a/lib/oelite/task.py b/lib/oelite/task.py
>>>> index 47d83d7..d0b01ea 100644
>>>> --- a/lib/oelite/task.py
>>>> +++ b/lib/oelite/task.py
>>>> @@ -278,6 +278,8 @@ class OEliteTask:
>>>>                  try:
>>>>                      #print "cleandir %s"%(cleandir)
>>>>                      shutil.rmtree(cleandir)
>>>> +                except OSError:
>>>> +                    os.unlink(cleandir)
>>>>                  except Exception, e:
>>>>                      err("cleandir %s failed: %s"%(cleandir, e))
>>>>                      raise
>>> 
>>> Could there be other OSError exceptions?
>>
>> Only the symlink error is mentioned in the manual
>
>>>> import shutil
>>>> shutil.rmtree("/tmp")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python2.7/shutil.py", line 244, in rmtree
>     rmtree(fullname, ignore_errors, onerror)
>   File "/usr/lib/python2.7/shutil.py", line 253, in rmtree
>     onerror(os.rmdir, path, sys.exc_info())
>   File "/usr/lib/python2.7/shutil.py", line 251, in rmtree
>     os.rmdir(path)
> OSError: [Errno 1] Operation not permitted: '/tmp/.ICE-unix'
>
>>>> import shutil
>>>> shutil.rmtree("/FOO/BAR")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python2.7/shutil.py", line 236, in rmtree
>     onerror(os.listdir, path, sys.exc_info())
>   File "/usr/lib/python2.7/shutil.py", line 234, in rmtree
>     names = os.listdir(path)
> OSError: [Errno 2] No such file or directory: '/FOO/BAR'
>
> So there are at least 2 more.  Perhaps better to do something like
>
> if os.path.islink(cleandir):
>    os.unlink(cleandir)
> else:
>    try:
>        shutil.rmtree(cleandir)
>    ....
>
> Would be better.

Better late than never.  I pushed a fix for this to master and 3.2
branches.


commit fc81f8044cd404eb4e33cae6ef20cc7d292a0570
Author: Esben Haabendal <esben.haabendal@prevas.dk>
Date:   Wed Oct 31 08:48:45 2012 +0100

    lib/oelite: Handle cleandir on symlinks
    
    Handle cases where cleandir refers to a symlink, which fx. can happen
    after failure in do_compile when using archive-image class.
    
    Signed-off-by: Esben Haabendal <esben.haabendal@prevas.dk>



/Esben

Patch

diff --git a/lib/oelite/task.py b/lib/oelite/task.py
index 5dac890..0db4303 100644
--- a/lib/oelite/task.py
+++ b/lib/oelite/task.py
@@ -266,7 +266,10 @@  class OEliteTask:
                     continue
                 try:
                     #print "cleandir %s"%(cleandir)
-                    shutil.rmtree(cleandir)
+                    if os.path.islink(cleandir):
+                        os.unlink(cleandir)
+                    else:
+                        shutil.rmtree(cleandir)
                 except Exception, e:
                     err("cleandir %s failed: %s"%(cleandir, e))
                     raise