Add ListFiles a function to recursively list files in a directory.

Add unittest for ListFiles to cover common use cases.

BUG=
TEST=

Review URL: http://codereview.chromium.org/3325017
This commit is contained in:
Scott Zawalski 2010-09-08 15:59:23 -07:00
parent 57e9808254
commit 57729c3615
2 changed files with 94 additions and 0 deletions

View File

@ -4,6 +4,7 @@
"""Common python commands used by various build scripts."""
import os
import subprocess
import sys
@ -125,3 +126,27 @@ def Info(message):
"""
print >> sys.stderr, (
Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message))
def ListFiles(base_dir):
"""Recurively list files in a directory.
Keyword arguments:
base_dir: directory to start recursively listing in.
Returns:
A list of files relative to the base_dir path or
An empty list of there are no files in the directories.
"""
directories = [base_dir]
files_list = []
while directories:
directory = directories.pop()
for name in os.listdir(directory):
fullpath = os.path.join(directory, name)
if os.path.isfile(fullpath):
files_list.append(fullpath)
elif os.path.isdir(fullpath):
directories.append(fullpath)
return files_list

View File

@ -0,0 +1,69 @@
#!/usr/bin/python
import errno
import os
import unittest
import shutil
import tempfile
import cros_build_lib
class TestListFiles(unittest.TestCase):
def setUp(self):
self.root_dir = tempfile.mkdtemp(prefix='listfiles_unittest')
def tearDown(self):
shutil.rmtree(self.root_dir)
def _createNestedDir(self, dir_structure):
for entry in dir_structure:
full_path = os.path.join(os.path.join(self.root_dir, entry))
# ensure dirs are created
try:
os.makedirs(os.path.dirname(full_path))
if full_path.endswith('/'):
# we only want to create directories
return
except OSError, err:
if err.errno == errno.EEXIST:
# we don't care if the dir already exists
pass
else:
raise
# create dummy files
tmp = open(full_path, 'w')
tmp.close()
def testTraverse(self):
"""
Test that we are traversing the directory properly
"""
dir_structure = ['one/two/test.txt', 'one/blah.py',
'three/extra.conf']
self._createNestedDir(dir_structure)
files = cros_build_lib.ListFiles(self.root_dir)
for file in files:
file = file.replace(self.root_dir, '').lstrip('/')
if file not in dir_structure:
self.fail('%s was not found in %s' % (file, dir_structure))
def testEmptyFilePath(self):
"""
Test that we return nothing when directories are empty
"""
dir_structure = ['one/', 'two/', 'one/a/']
self._createNestedDir(dir_structure)
files = cros_build_lib.ListFiles(self.root_dir)
self.assertEqual(files, [])
def testNoSuchDir(self):
try:
cros_build_lib.ListFiles('/me/no/existe')
except OSError, err:
self.assertEqual(err.errno, errno.ENOENT)
if __name__ == '__main__':
unittest.main()