Merge "Allow ls() to list recursively (using breadth-first)"

This commit is contained in:
Jenkins
2015-04-05 02:24:41 +00:00
committed by Gerrit Code Review
3 changed files with 48 additions and 8 deletions

View File

@@ -70,14 +70,9 @@ e.run()
print("---------")
print("After run")
print("---------")
entries = [os.path.join(backend.memory.root_path, child)
for child in backend.memory.ls(backend.memory.root_path)]
while entries:
path = entries.pop()
for path in backend.memory.ls(backend.memory.root_path, recursive=True):
value = backend.memory[path]
if value:
print("%s -> %s" % (path, value))
else:
print("%s" % (path))
entries.extend(os.path.join(path, child)
for child in backend.memory.ls(path))

View File

@@ -110,9 +110,25 @@ class FakeFilesystem(object):
else:
return self._copier(node.metadata['value'])
def ls(self, path):
def ls(self, path, recursive=False):
"""Return list of all children of the given path."""
return [node.item for node in self._fetch_node(path)]
if not recursive:
return [node.item for node in self._fetch_node(path)]
else:
paths = []
node = self._fetch_node(path)
for child in node.bfs_iter():
# Reconstruct the child's path...
hops = [child.item]
for parent in child.path_iter(include_self=False):
hops.append(parent.item)
hops.reverse()
# This avoids getting '//a/b' (duplicated sep at start)...
child_path = pp.sep.join(hops)
if child_path.startswith("//"):
child_path = child_path[1:]
paths.append(child_path)
return paths
def _iter_pieces(self, path, include_root=False):
if path == self._root.item:

View File

@@ -71,6 +71,35 @@ class MemoryFilesystemTest(test.TestCase):
self.assertEqual('c', fs['/c'])
self.assertEqual('db', fs['/d/b'])
def test_ls_recursive(self):
fs = impl_memory.FakeFilesystem()
fs.ensure_path("/d")
fs.ensure_path("/c/d")
fs.ensure_path("/b/c/d")
fs.ensure_path("/a/b/c/d")
contents = fs.ls("/", recursive=True)
self.assertEqual([
'/a',
'/b',
'/c',
'/d',
'/a/b',
'/b/c',
'/c/d',
'/a/b/c',
'/b/c/d',
'/a/b/c/d',
], contents)
def test_ls_recursive_targeted(self):
fs = impl_memory.FakeFilesystem()
fs.ensure_path("/d")
fs.ensure_path("/c/d")
fs.ensure_path("/b/c/d")
fs.ensure_path("/a/b/c/d")
contents = fs.ls("/a/b", recursive=True)
self.assertEqual(['/a/b/c', '/a/b/c/d'], contents)
def test_ensure_path(self):
fs = impl_memory.FakeFilesystem()
pieces = ['a', 'b', 'c']