Beispiel #1
0
 def make_path(self, *args):
     """Create a path with the given component(s). A base path is prepended
     to the path which represents a temporary directory in the real FS,
     and a fixed path in the fake filesystem.
     Always use to compose absolute paths for tests also running in the
     real FS.
     """
     if isinstance(args[0], (list, tuple)):
         path = self.base_path
         for arg in args[0]:
             path = self.os.path.join(path, to_string(arg))
         return path
     args = [to_string(arg) for arg in args]
     return self.os.path.join(self.base_path, *args)
Beispiel #2
0
 def __init__(self, filesystem, path):
     self.filesystem = filesystem
     if isinstance(path, int):
         if not use_scandir_package and (sys.version_info < (3, 7)
                                         or self.filesystem.is_windows_fs):
             raise NotImplementedError(
                 'scandir does not support file descriptor '
                 'path argument')
         self.abspath = self.filesystem.absnormpath(
             self.filesystem.get_open_file(path).get_object().path)
         self.path = ''
     else:
         self.abspath = self.filesystem.absnormpath(path)
         self.path = to_string(path)
     contents = self.filesystem.confirmdir(self.abspath).contents
     self.contents_iter = iter(contents)
Beispiel #3
0
def walk(filesystem, top, topdown=True, onerror=None, followlinks=False):
    """Perform an os.walk operation over the fake filesystem.

    Args:
        filesystem: The fake filesystem used for implementation
        top: The root directory from which to begin walk.
        topdown: Determines whether to return the tuples with the root as
            the first entry (`True`) or as the last, after all the child
            directory tuples (`False`).
      onerror: If not `None`, function which will be called to handle the
            `os.error` instance provided when `os.listdir()` fails.
      followlinks: If `True`, symbolic links are followed.

    Yields:
        (path, directories, nondirectories) for top and each of its
        subdirectories.  See the documentation for the builtin os module
        for further details.
    """
    def do_walk(top_dir, top_most=False):
        if not top_most and not followlinks and filesystem.islink(top_dir):
            return
        try:
            top_contents = _classify_directory_contents(filesystem, top_dir)
        except OSError as exc:
            top_contents = None
            if onerror is not None:
                onerror(exc)

        if top_contents is not None:
            if topdown:
                yield top_contents

            for directory in top_contents[1]:
                if not followlinks and filesystem.islink(directory):
                    continue
                for contents in do_walk(
                        filesystem.joinpaths(top_dir, directory)):
                    yield contents

            if not topdown:
                yield top_contents

    return do_walk(to_string(top), top_most=True)