def test_set_dir_readwrite(self): """Test for set_dir_readwrite.""" set_dir_readonly(self.basedir) # do not queue up any cleanup function since we're restoring perms in # the next call set_dir_readwrite(self.basedir) foo_dir = os.path.join(self.basedir, 'foo') os.mkdir(foo_dir) self.assertTrue(path_exists(foo_dir))
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 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)
def makedirs(self, path): """Custom makedirs that handle ro parent.""" parent = os.path.dirname(path) if path_exists(parent): set_dir_readwrite(parent) make_dir(path, recursive=True)