def rmtree(self, path): """Custom rmtree that handle ro parent(s) and childs.""" # change perms to rw, so we can delete the temp dir if path != self.__root: platform.set_dir_readwrite(os.path.dirname(path)) if not platform.can_write(path): platform.set_dir_readwrite(path) for dirpath, dirs, files in os.walk(path): for adir in dirs: adir = os.path.join(dirpath, adir) if not platform.can_write(adir): platform.set_dir_readwrite(adir) shutil.rmtree(path)
def test_set_file_readwrite(self): """Test for set_file_readwrite.""" set_file_readonly(self.testfile) self.addCleanup(set_dir_readwrite, self.testfile) set_file_readwrite(self.testfile) open_file(self.testfile, 'w') self.assertTrue(can_write(self.testfile))
def rmtree(self, path): """Custom rmtree that handle ro parent(s) and childs.""" assert isinstance(path, str) # on windows the paths cannot be removed because the process running # them has the ownership and therefore are locked. if not path_exists(path): return # change perms to rw, so we can delete the temp dir if path != self.__root: set_dir_readwrite(os.path.dirname(path)) if not can_write(path): set_dir_readwrite(path) if sys.platform == "win32": # path is a byte sequence encoded with utf-8. If we pass this to # os.walk, in windows, we'll get results encoded with mbcs path = path.decode("utf-8") for dirpath, dirs, files in os.walk(path): for adir in dirs: adir = os.path.join(dirpath, adir) if sys.platform == "win32": assert isinstance(adir, unicode) adir = adir.encode("utf-8") if not can_write(adir): set_dir_readwrite(adir) if sys.platform == "win32": # in windows, we need to pass a unicode, literal path to # shutil.rmtree, otherwise we can't remove "deep and wide" paths path = u"\\\\?\\" + path.decode("utf-8") # Instead of ignoring the errors when removing trees, we are temporarly # printing a message to stdout to caught everyone's attention. # Once the tests are fixed in this regard, we're removing the # try-except block and having shutil.rmtree failing if the path # can not be removed. try: shutil.rmtree(path) except Exception, e: print "ERROR!! could not recursively remove %r " "(error is %r)." % (path, e)