Skip to content

Commit

Permalink
Merge c41da7a into 4245d0b
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Mar 8, 2019
2 parents 4245d0b + c41da7a commit dbc380b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
16 changes: 14 additions & 2 deletions parsec/core/mountpoint/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import warnings
import logging
from functools import partial
from pathlib import PurePath

from async_generator import asynccontextmanager

from parsec.utils import start_task
from parsec.core.types import FsPath
from parsec.core.mountpoint.exceptions import (
MountpointConfigurationError,
MountpointAlreadyMounted,
Expand Down Expand Up @@ -54,13 +56,23 @@ async def disabled_runner(*args, **kwargs):


class MountpointManager:
def __init__(self, base_mountpoint, fs, runner, nursery):
def __init__(self, base_mountpoint: PurePath, fs, runner, nursery):
self._base_mountpoint = base_mountpoint
self._fs = fs
self._runner = runner
self._nursery = nursery
self._mountpoint_tasks = {}

def get_path_in_mountpoint(self, path: FsPath) -> PurePath:
workspace = path.workspace
sub_path = path.relative_to(f"/{workspace}")
if workspace not in self._mountpoint_tasks:
raise MountpointNotMounted(f"Workspace `{workspace}` is not mounted")
return self._get_mountpoint_path(workspace) / sub_path

def _get_mountpoint_path(self, workspace: str) -> PurePath:
return self._base_mountpoint / f"{self._fs.device.user_id}-{workspace}"

async def mount_workspace(self, workspace: str):
if workspace in self._mountpoint_tasks:
raise MountpointAlreadyMounted(f"Workspace `{workspace}` already mounted.")
Expand All @@ -70,7 +82,7 @@ async def mount_workspace(self, workspace: str):
except FileNotFoundError as exc:
raise MountpointConfigurationError(f"Workspace `{workspace}` doesn't exist") from exc

mountpoint = self._base_mountpoint / f"{self._fs.device.user_id}-{workspace}"
mountpoint = self._get_mountpoint_path(workspace)
runner_task = await start_task(self._nursery, self._runner, workspace, mountpoint)
self._mountpoint_tasks[workspace] = runner_task

Expand Down
4 changes: 4 additions & 0 deletions parsec/core/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def is_root(self):
def is_workspace(self):
return not self.is_root() and self.parent.is_root()

@property
def workspace(self):
return self.parts[1]

def walk_from_path(self):
parent = None
curr = self
Expand Down
27 changes: 26 additions & 1 deletion tests/core/mountpoint/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import trio
import pytest
from pathlib import Path
from pathlib import Path, PurePath
from unittest.mock import patch

from parsec.core.mountpoint import (
Expand Down Expand Up @@ -243,3 +243,28 @@ async def _bootstrap(fs, mountpoint_manager):
assert str(exc.value).startswith("[WinError 1231] The network location cannot be reached.")
else:
assert exc.value.args == (100, "Network is down")


@pytest.mark.trio
@pytest.mark.mountpoint
async def test_get_path_in_mountpoint(base_mountpoint, alice, alice_fs, event_bus):
# Populate a bit the fs first...
await alice_fs.workspace_create("/mounted_wksp")
await alice_fs.workspace_create("/not_mounted_wksp")
await alice_fs.file_create("/mounted_wksp/bar.txt")
await alice_fs.file_create("/not_mounted_wksp/foo.txt")

# Now we can start fuse
async with mountpoint_manager_factory(
alice_fs, event_bus, base_mountpoint
) as mountpoint_manager:
await mountpoint_manager.mount_workspace("mounted_wksp")

bar_path = mountpoint_manager.get_path_in_mountpoint(FsPath("/mounted_wksp/bar.txt"))

assert isinstance(bar_path, PurePath)
assert str(bar_path) == f"{base_mountpoint.absolute()}/{alice.user_id}-mounted_wksp/bar.txt"
assert await trio.Path(bar_path).exists()

with pytest.raises(MountpointNotMounted):
mountpoint_manager.get_path_in_mountpoint(FsPath("/not_mounted_wksp/foo.txt"))

0 comments on commit dbc380b

Please sign in to comment.