Commit 315ea957 by willmcgugan

Added a remove_all function to utils

parent 39ef2b67
...@@ -84,3 +84,4 @@ ...@@ -84,3 +84,4 @@
* Ported to Python 3.X * Ported to Python 3.X
* Added a DeleteRootError to exceptions thrown when trying to delete '/' * Added a DeleteRootError to exceptions thrown when trying to delete '/'
* Added a remove_all function to utils
...@@ -88,19 +88,26 @@ class TestUtils(unittest.TestCase): ...@@ -88,19 +88,26 @@ class TestUtils(unittest.TestCase):
self.assert_(not fs1.exists("from")) self.assert_(not fs1.exists("from"))
self._check_fs(fs2) self._check_fs(fs2)
if __name__ == "__main__": def test_remove_all(self):
"""Test remove_all function"""
def _make_fs(fs): fs = TempFS()
fs.setcontents("f1", "file 1") fs.setcontents("f1", "file 1")
fs.setcontents("f2", "file 2") fs.setcontents("f2", "file 2")
fs.setcontents("f3", "file 3") fs.setcontents("f3", "file 3")
fs.makedir("foo/bar", recursive=True) fs.makedir("foo/bar", recursive=True)
fs.setcontents("foo/bar/fruit", "apple") fs.setcontents("foo/bar/fruit", "apple")
fs.setcontents("foo/baz", "baz")
utils.remove_all(fs, "foo/bar")
self.assert_(not fs.exists("foo/bar/fruit"))
self.assert_(fs.exists("foo/bar"))
self.assert_(fs.exists("foo/baz"))
utils.remove_all(fs, "")
self.assert_(not fs.exists("foo/bar/fruit"))
self.assert_(not fs.exists("foo/bar/baz"))
self.assert_(not fs.exists("foo/baz"))
self.assert_(not fs.exists("foo"))
self.assert_(not fs.exists("f1"))
self.assert_(fs.isdirempty('/'))
\ No newline at end of file
fs1 = TempFS()
fs2 = TempFS()
fs1sub = fs1.makeopendir("from")
_make_fs(fs1sub)
utils.movedir((fs1, "from"), fs2)
#self.assert_(not fs1.exists("from"))
#self._check_fs(fs2)
\ No newline at end of file
...@@ -253,6 +253,23 @@ def copydir(fs1, fs2, create_destination=True, ignore_errors=False, chunk_size=6 ...@@ -253,6 +253,23 @@ def copydir(fs1, fs2, create_destination=True, ignore_errors=False, chunk_size=6
chunk_size=chunk_size) chunk_size=chunk_size)
def remove_all(fs, path):
"""Remove everything in a directory. Returns True if successful.
:param fs: A filesystem
:param path: Path to a directory
"""
fs.tree()
sub_fs = fs.opendir(path)
for sub_path in sub_fs.listdir():
if sub_fs.isdir(sub_path):
sub_fs.removedir(sub_path, force=True)
else:
sub_fs.remove(sub_path)
return fs.isdirempty(path)
def copystructure(src_fs, dst_fs): def copystructure(src_fs, dst_fs):
"""Copies the directory structure from one filesystem to another, so that """Copies the directory structure from one filesystem to another, so that
all directories in `src_fs` will have a corresponding directory in `dst_fs` all directories in `src_fs` will have a corresponding directory in `dst_fs`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment