Logo Search packages:      
Sourcecode: obnam version File versions  Download package

def obnamlib::vfs::VirtualFileSystem::scan_tree (   self,
  dirname,
  ok = None,
  dirst = None,
  log = logging.error 
)
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.

Definition at line 181 of file vfs.py.

                                                                        :
        '''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 is None or 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 is None:
            try:
                dirst = self.lstat(dirname)
            except OSError, e:
                log('lstat for dir failed: %s: %s' % (e.filename, e.strerror))
                return

        yield dirname, dirst

        

Generated by  Doxygen 1.6.0   Back to index