Convert a real path under `session_root` to a vpath. Raises PermissionError if `real_path` is outside `session_root`.
(
session_root: Union[Path, str], real_path: Union[Path, str], *, prefix: bool = False
)
| 20 | |
| 21 | |
| 22 | def _to_vpath( |
| 23 | session_root: Union[Path, str], real_path: Union[Path, str], *, prefix: bool = False |
| 24 | ) -> str: |
| 25 | """ |
| 26 | Convert a real path under `session_root` to a vpath. |
| 27 | |
| 28 | Raises PermissionError if `real_path` is outside `session_root`. |
| 29 | """ |
| 30 | rp = Path(real_path).resolve() |
| 31 | base = Path(session_root).resolve() |
| 32 | if not str(rp).startswith(str(base)): |
| 33 | raise PermissionError("real path not under session root") |
| 34 | rel = rp.relative_to(base) |
| 35 | v = "/" + str(rel) |
| 36 | return ("sandbox:" + v) if prefix else v |
| 37 | |
| 38 | |
| 39 | def _from_vpath(session_root: Union[Path, str], vpath: str) -> Path: |
no test coverage detected