eggs/zc.buildout-1.5.2-py2.6.egg/zc/buildout/rmtree.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 ##############################################################################
       
     2 #
       
     3 # Copyright (c) 2006 Zope Corporation and Contributors.
       
     4 # All Rights Reserved.
       
     5 #
       
     6 # This software is subject to the provisions of the Zope Public License,
       
     7 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    11 # FOR A PARTICULAR PURPOSE.
       
    12 #
       
    13 ##############################################################################
       
    14 
       
    15 
       
    16 import shutil
       
    17 import os
       
    18 import doctest
       
    19 
       
    20 def rmtree (path):
       
    21     """
       
    22     A variant of shutil.rmtree which tries hard to be successful
       
    23     On windows shutil.rmtree aborts when it tries to delete a
       
    24     read only file.
       
    25     This tries to chmod the file to writeable and retries before giving up.
       
    26 
       
    27     >>> from tempfile import mkdtemp
       
    28 
       
    29     Let's make a directory ...
       
    30 
       
    31     >>> d = mkdtemp()
       
    32 
       
    33     and make sure it is actually there
       
    34 
       
    35     >>> os.path.isdir (d)
       
    36     1
       
    37 
       
    38     Now create a file ...
       
    39 
       
    40     >>> foo = os.path.join (d, 'foo')
       
    41     >>> open (foo, 'w').write ('huhu')
       
    42 
       
    43     and make it unwriteable
       
    44 
       
    45     >>> os.chmod (foo, 0400)
       
    46 
       
    47     rmtree should be able to remove it:
       
    48 
       
    49     >>> rmtree (d)
       
    50 
       
    51     and now the directory is gone
       
    52 
       
    53     >>> os.path.isdir (d)
       
    54     0
       
    55     """
       
    56     def retry_writeable (func, path, exc):
       
    57         os.chmod (path, 0600)
       
    58         func (path)
       
    59 
       
    60     shutil.rmtree (path, onerror = retry_writeable)
       
    61 
       
    62 def test_suite():
       
    63     return doctest.DocTestSuite()
       
    64 
       
    65 if "__main__" == __name__:
       
    66     doctest.testmod()