Esempio n. 1
0
 def temporary_directory(self):
     # TODO: Maybe this isn't good enough.
     directory = Path(uuid4().hex)
     self.create_directory(path=directory)
     return directory
Esempio n. 2
0
 def test_named_virtualenvs_are_children(self):
     locator = Locator(root=Path.root())
     self.assertEqual(
         locator.for_name("one"),
         VirtualEnv(path=locator.root.descendant("one")),
     )
Esempio n. 3
0
class _State(object):
    """
    The state of a memory filesystem.

    Includes its file contents and tree state.

    """

    _tree = pmap([(Path.root(), pmap())])
    _links = pmap()

    def create_file(self, path):
        if self.exists(path=path) or self.is_link(path=path):
            raise exceptions.FileExists(path)

        path = self.realpath(path=path)
        parent = path.parent()
        contents = self._tree.get(parent)
        if contents is None:
            raise exceptions.FileNotFound(path)
        file = _BytesIOIsTerrible()
        self._tree = self._tree.set(parent, contents.set(path, file))
        return file

    def open_file(self, path, mode):
        path = self.realpath(path=path)

        parent = path.parent()
        contents = self._tree.get(parent)
        if contents is None:
            raise exceptions.FileNotFound(path)

        if mode == "w" or mode == "wb":
            file = _BytesIOIsTerrible()
            self._tree = self._tree.set(parent, contents.set(path, file))
            return file

        file = contents.get(path)

        if mode == "a" or mode == "ab":
            if file is None:
                combined = _BytesIOIsTerrible()
            else:
                combined = _BytesIOIsTerrible()
                combined.write(file._hereismyvalue)
            self._tree = self._tree.set(parent, contents.set(path, combined))
            return combined
        elif file is None:
            raise exceptions.FileNotFound(path)
        else:
            return BytesIO(file._hereismyvalue)

    def remove_file(self, path):
        parent = path.parent()
        contents = self._tree.get(parent)
        if contents is None:
            raise exceptions.FileNotFound(path)

        if path not in contents:
            raise exceptions.FileNotFound(path)

        self._tree = self._tree.set(parent, contents.remove(path))

    def create_directory(self, path):
        if self.exists(path=path):
            raise exceptions.FileExists(path)

        parent = path.parent()
        if not self.exists(path=parent):
            raise exceptions.FileNotFound(parent)

        self._tree = self._tree.set(path, pmap())

    def list_directory(self, path):
        if self.is_file(path=path):
            raise exceptions.NotADirectory(path)
        elif path not in self._tree:
            raise exceptions.FileNotFound(path)

        # FIXME: Inefficient
        return pset(child.basename() for child in self._tree[path]) | pset(
            subdirectory.basename() for subdirectory in self._tree
            if subdirectory.parent() == path and subdirectory != path)

    def remove_empty_directory(self, path):
        if self.list_directory(path=path):
            raise exceptions.DirectoryNotEmpty(path)
        self.remove(path=path)

    def temporary_directory(self):
        # TODO: Maybe this isn't good enough.
        directory = Path(uuid4().hex)
        self.create_directory(path=directory)
        return directory

    def remove(self, path):
        if self.is_link(path=path):
            self._links = self._links.remove(path)
        elif path not in self._tree:
            raise exceptions.FileNotFound(path)
        else:
            self._tree = self._tree.remove(path)

    def link(self, source, to):
        if self.exists(path=to) or self.is_link(path=to):
            raise exceptions.FileExists(to)
        self._tree = self._tree_with(path=to)
        self._links = self._links.set(to, source)

    def realpath(self, path):
        real = Path.root()
        for segment in path.segments:
            seen = current, = {real.descendant(segment)}
            while self.is_link(path=current):
                current = self._links[current].relative_to(current.parent())
                if current in seen:
                    raise exceptions.SymbolicLoop(current)
                seen.add(current)
            real = current
        return real

    def exists(self, path):
        return self.is_file(path=path) or self.is_dir(path=path)

    def is_dir(self, path):
        return self.realpath(path=path) in self._tree

    def is_file(self, path):
        real = self.realpath(path=path)
        return real in self._tree.get(real.parent(), pmap())

    def is_link(self, path):
        return path in self._links

    def _tree_with(self, path):
        parent = path.parent()
        if not self.exists(path=parent):
            raise exceptions.FileNotFound(parent)
        elif not self.is_dir(path=parent):
            raise exceptions.NotADirectory(parent)
        return self._tree.set(parent, self._tree[parent].set(path, None))
Esempio n. 4
0
 def test_children_of_nonempty_root(self):
     fs = self.FS()
     fs.touch(Path("file"))
     self.assertEqual(fs.children(Path.root()), s(Path("file")))
Esempio n. 5
0
 def test_instances_are_independent(self):
     fs = self.FS()
     fs.touch(Path("file"))
     self.assertTrue(fs.exists(Path("file")))
     self.assertFalse(self.FS().exists(Path("file")))
Esempio n. 6
0
 def test_children_of_root(self):
     fs = self.FS()
     self.assertFalse(fs.children(Path.root()))
Esempio n. 7
0
from appdirs import user_config_dir
from filesystems import Path, native
import toml

_PATH = Path.from_string(user_config_dir(__package__)).descendant(
    "config.toml", )


def load(path=_PATH, fs=native.FS()):
    return toml.loads(fs.get_contents(path))
Esempio n. 8
0
            current = os.path.join(
                os.path.dirname(current), os.readlink(current),
            )
            if current in seen:
                raise exceptions.SymbolicLoop(current)
            seen.add(current)
        real = Path.from_string(current)
    return real


FS = common.create(
    name="NativeFS",

    create_file=_create_file,
    open_file=_open_file,
    remove_file=_remove_file,

    create_directory=_create_directory,
    list_directory=_list_directory,
    remove_empty_directory=_remove_empty_directory,
    temporary_directory=lambda fs: Path.from_string(tempfile.mkdtemp()),

    link=_link,
    realpath=_realpath,

    exists=lambda fs, path: os.path.exists(str(path)),
    is_dir=lambda fs, path: os.path.isdir(str(path)),
    is_file=lambda fs, path: os.path.isfile(str(path)),
    is_link=lambda fs, path: os.path.islink(str(path)),
)
Esempio n. 9
0
 def test_realpath_root(self):
     fs = self.FS()
     self.assertEqual(fs.realpath(Path.root()), Path.root())
Esempio n. 10
0
 def test_root_always_exists(self):
     fs = self.FS()
     self.assertTrue(fs.exists(Path.root()))