def test_dir_empty(self): tmpdir = mkdtemp() try: subdir = mkdtemp(dir=tmpdir) assert not fs.dir_empty(tmpdir) assert fs.dir_empty(subdir) finally: shutil.rmtree(tmpdir)
def test_dir_empty_err(self): def _mock_os_listdir(path): raise OSError(13, "foo") with patch("os.listdir", _mock_os_listdir): try: fs.dir_empty("/tmp") except GlusterFileSystemOSError: pass else: self.fail("GlusterFileSystemOSError exception expected")
def test_dir_empty_notdir(self): fd, tmpfile = mkstemp() try: try: fs.dir_empty(tmpfile) except NotDirectoryError: pass else: self.fail("NotDirectoryError exception expected") finally: os.close(fd) os.unlink(tmpfile)
def test_dir_empty_notfound(self): try: assert fs.dir_empty(os.path.join('/tmp', str(random.random()))) except FileOrDirNotFoundError: pass else: self.fail("FileOrDirNotFoundError exception expected")
def empty(self): # If it does not exist, then it is empty. A value of True is # what is expected by OpenStack Swift when the directory does # not exist. Check swift/common/db.py:ContainerBroker.empty() # and swift/container/server.py:ContainerController.DELETE() # for more information try: return dir_empty(self.datadir) except FileOrDirNotFoundError: return True
def test_dir_empty_err(self): try: try: assert fs.dir_empty(os.path.join('/tmp', str(random.random()))) except FileOrDirNotFoundError: pass else: self.fail("FileOrDirNotFoundError exception expected") fd, tmpfile = mkstemp() try: fs.dir_empty(tmpfile) except NotDirectoryError: pass else: self.fail("NotDirectoryError exception expected") finally: os.close(fd) os.unlink(tmpfile)
def delete_db(self, timestamp): """ Delete the container (directory) if empty. :param timestamp: delete timestamp """ try: if not dir_empty(self.datadir): # FIXME: This is a failure condition here, isn't it? return except FileOrDirNotFoundError: return rmdirs(self.datadir)
def rmobjdir(dir_path): """ Removes the directory as long as there are no objects stored in it. This works for containers also. """ try: if dir_empty(dir_path): rmdirs(dir_path) return True except FileOrDirNotFoundError: # No such directory exists return False for (path, dirs, files) in do_walk(dir_path, topdown=False): for directory in dirs: fullpath = os.path.join(path, directory) metadata = read_metadata(fullpath) if not dir_is_object(metadata): # Directory is not an object created by the caller # so we can go ahead and delete it. try: if dir_empty(fullpath): rmdirs(fullpath) else: # Non-object dir is not empty! return False except FileOrDirNotFoundError: # No such dir! return False else: # Wait, this is an object created by the caller # We cannot delete return False rmdirs(dir_path) return True
def empty(self): try: return dir_empty(self.datadir) except FileOrDirNotFoundError: return True