Scan a tree for files.
Return a generator that returns ``(pathname, stat_result)``
pairs for each file and directory in the tree, in
depth-first order.
If ``ok`` is not None, it must be a function that determines
if a particular file or directory should be returned.
It gets the pathname and stat result as arguments, and
should return True or False. If it returns False on a
directory, ``scan_tree`` will not recurse into the
directory.
``dirst`` is for internal optimization, and should not
be used by the caller. ``log`` is used by unit tests and
should not be used by the caller.
Errors from calling ``listdir`` or ``lstat`` are logged,
but do not stop the scanning. Such files or directories are
not returned, however.

:
'''Scan a tree for files. Return a generator that returns ``(pathname, stat_result)`` pairs for each file and directory in the tree, in depth-first order. If ``ok`` is not None, it must be a function that determines if a particular file or directory should be returned. It gets the pathname and stat result as arguments, and should return True or False. If it returns False on a directory, ``scan_tree`` will not recurse into the directory. ``dirst`` is for internal optimization, and should not be used by the caller. ``log`` is used by unit tests and should not be used by the caller. Errors from calling ``listdir`` or ``lstat`` are logged, but do not stop the scanning. Such files or directories are not returned, however. '''try:
names = self.listdir(dirname)
except OSError, e:
log('listdir failed: %s: %s' % (e.filename, e.strerror))
names = []
queue = []
for name in names:
pathname = os.path.join(dirname, name)
try:
st = self.lstat(pathname)
except OSError, e:
log('lstat failed: %s: %s' % (e.filename, e.strerror))
else:
if ok isNoneor ok(pathname, st):
if stat.S_ISDIR(st.st_mode):
for t in self.scan_tree(pathname, ok=ok, dirst=st):
yield t
else:
queue.append((pathname, st))
for pathname, st in queue:
yield pathname, st
if dirst isNone:
try:
dirst = self.lstat(dirname)
except OSError, e:
log('lstat for dir failed: %s: %s' % (e.filename, e.strerror))
returnyield dirname, dirst