mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 09:13:37 +02:00
lib.FileContext: Support chroot
Add a bool parameter "chroot" to __init__(). If passed, all path-centric operations act as if .root was prepended to it. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
b52264dfad
commit
7547d13a6d
1 changed files with 21 additions and 13 deletions
|
|
@ -14,9 +14,10 @@ from .base import Input, InputMode, Result, StatResult
|
||||||
|
|
||||||
class FileContext(abc.ABC):
|
class FileContext(abc.ABC):
|
||||||
|
|
||||||
def __init__(self, uri: str, interactive: bool|None=None, verbose_default=False):
|
def __init__(self, uri: str, interactive: bool|None=None, verbose_default=False, chroot: bool=False):
|
||||||
self.__uri = uri
|
self.__uri = uri
|
||||||
self.__id, self.__root = self.split_uri(uri)
|
self.__id, self.__root = self.split_uri(uri)
|
||||||
|
self.__chroot = chroot
|
||||||
self.__interactive = interactive
|
self.__interactive = interactive
|
||||||
self.__verbose_default = verbose_default
|
self.__verbose_default = verbose_default
|
||||||
self.__log_name: str|None = None
|
self.__log_name: str|None = None
|
||||||
|
|
@ -28,6 +29,15 @@ class FileContext(abc.ABC):
|
||||||
async def __aexit__(self, exc_type, exc, tb):
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
await self.close()
|
await self.close()
|
||||||
|
|
||||||
|
def _chroot(self, path: str) -> str:
|
||||||
|
if not self.__chroot:
|
||||||
|
return path
|
||||||
|
if not len(path):
|
||||||
|
return self.__root
|
||||||
|
if path[-1] == '/':
|
||||||
|
return self.__root + path
|
||||||
|
return self.__root + '/' + path
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def schema_from_uri(cls, uri: str) -> str:
|
def schema_from_uri(cls, uri: str) -> str:
|
||||||
tokens = re.split(r'://', uri)
|
tokens = re.split(r'://', uri)
|
||||||
|
|
@ -98,7 +108,7 @@ class FileContext(abc.ABC):
|
||||||
group: str|None=None,
|
group: str|None=None,
|
||||||
mode: str|None=None,
|
mode: str|None=None,
|
||||||
) -> Result:
|
) -> Result:
|
||||||
return await self._get(path, wd=wd, throw=throw, verbose=verbose, title=title)
|
return await self._get(self._chroot(path), wd=wd, throw=throw, verbose=verbose, title=title)
|
||||||
|
|
||||||
async def _put(
|
async def _put(
|
||||||
self,
|
self,
|
||||||
|
|
@ -129,20 +139,20 @@ class FileContext(abc.ABC):
|
||||||
atomic: bool = False
|
atomic: bool = False
|
||||||
) -> Result:
|
) -> Result:
|
||||||
mode_str = None if mode is None else oct(mode).replace('0o', '0')
|
mode_str = None if mode is None else oct(mode).replace('0o', '0')
|
||||||
return await self._put(path, content, wd=wd, throw=throw, verbose=verbose,
|
return await self._put(self._chroot(path), content, wd=wd, throw=throw, verbose=verbose,
|
||||||
title=title, owner=owner, group=group, mode=mode_str, atomic=atomic)
|
title=title, owner=owner, group=group, mode=mode_str, atomic=atomic)
|
||||||
|
|
||||||
async def _unlink(self, path: str) -> None:
|
async def _unlink(self, path: str) -> None:
|
||||||
raise NotImplementedError(f'{self.log_name}: unlink("{path}") is not implemented')
|
raise NotImplementedError(f'{self.log_name}: unlink("{path}") is not implemented')
|
||||||
|
|
||||||
async def unlink(self, path: str) -> None:
|
async def unlink(self, path: str) -> None:
|
||||||
return await self._unlink(path)
|
return await self._unlink(self._chroot(path))
|
||||||
|
|
||||||
async def _erase(self, path: str) -> None:
|
async def _erase(self, path: str) -> None:
|
||||||
raise NotImplementedError(f'{self.log_name}: erase("{path}") is not implemented')
|
raise NotImplementedError(f'{self.log_name}: erase("{path}") is not implemented')
|
||||||
|
|
||||||
async def erase(self, path: str) -> None:
|
async def erase(self, path: str) -> None:
|
||||||
return await self._erase(path)
|
return await self._erase(self._chroot(path))
|
||||||
|
|
||||||
async def _rename(self, src: str, dst: str) -> None:
|
async def _rename(self, src: str, dst: str) -> None:
|
||||||
raise NotImplementedError(f'{self.log_name}: rename("{path}") is not implemented')
|
raise NotImplementedError(f'{self.log_name}: rename("{path}") is not implemented')
|
||||||
|
|
@ -154,7 +164,7 @@ class FileContext(abc.ABC):
|
||||||
raise NotImplementedError(f'{self.log_name}: mktemp("{path}") is not implemented')
|
raise NotImplementedError(f'{self.log_name}: mktemp("{path}") is not implemented')
|
||||||
|
|
||||||
async def mktemp(self, tmpl: str, directory: bool=False) -> None:
|
async def mktemp(self, tmpl: str, directory: bool=False) -> None:
|
||||||
return await self._mktemp(tmpl, directory)
|
return await self._mktemp(_chroot(tmpl), directory)
|
||||||
|
|
||||||
async def _chown(self, path: str, owner: str|None, group: str|None) -> None:
|
async def _chown(self, path: str, owner: str|None, group: str|None) -> None:
|
||||||
raise NotImplementedError(f'{self.log_name}: chown("{path}") is not implemented')
|
raise NotImplementedError(f'{self.log_name}: chown("{path}") is not implemented')
|
||||||
|
|
@ -162,13 +172,13 @@ class FileContext(abc.ABC):
|
||||||
async def chown(self, path: str, owner: str|None=None, group: str|None=None) -> None:
|
async def chown(self, path: str, owner: str|None=None, group: str|None=None) -> None:
|
||||||
if owner is None and group is None:
|
if owner is None and group is None:
|
||||||
raise ValueError(f'Tried to change ownership of {path} specifying neither owner nor group')
|
raise ValueError(f'Tried to change ownership of {path} specifying neither owner nor group')
|
||||||
return await self._chown(path, owner, group)
|
return await self._chown(self._chroot(path), owner, group)
|
||||||
|
|
||||||
async def _chmod(self, path: str, mode: int) -> None:
|
async def _chmod(self, path: str, mode: int) -> None:
|
||||||
raise NotImplementedError(f'{self.log_name}: chmod("{path}") is not implemented')
|
raise NotImplementedError(f'{self.log_name}: chmod("{path}") is not implemented')
|
||||||
|
|
||||||
async def chmod(self, path: str, mode: int) -> None:
|
async def chmod(self, path: str, mode: int) -> None:
|
||||||
return await self._chmod(path, mode)
|
return await self._chmod(self._chroot(path), mode)
|
||||||
|
|
||||||
async def _stat(self, path: str, follow_symlinks: bool) -> StatResult:
|
async def _stat(self, path: str, follow_symlinks: bool) -> StatResult:
|
||||||
raise NotImplementedError(f'{self.log_name}: lstat("{path}") is not implemented')
|
raise NotImplementedError(f'{self.log_name}: lstat("{path}") is not implemented')
|
||||||
|
|
@ -176,7 +186,7 @@ class FileContext(abc.ABC):
|
||||||
async def stat(self, path: str, follow_symlinks: bool=True) -> StatResult:
|
async def stat(self, path: str, follow_symlinks: bool=True) -> StatResult:
|
||||||
if not isinstance(path, str):
|
if not isinstance(path, str):
|
||||||
raise TypeError(f"path must be str, got {type(path).__name__}")
|
raise TypeError(f"path must be str, got {type(path).__name__}")
|
||||||
return await self._stat(path, follow_symlinks)
|
return await self._stat(self._chroot(path), follow_symlinks)
|
||||||
|
|
||||||
async def _file_exists(self, path: str) -> bool:
|
async def _file_exists(self, path: str) -> bool:
|
||||||
try:
|
try:
|
||||||
|
|
@ -190,7 +200,7 @@ class FileContext(abc.ABC):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def file_exists(self, path: str) -> bool:
|
async def file_exists(self, path: str) -> bool:
|
||||||
return await self._file_exists(path)
|
return await self._file_exists(self._chroot(path))
|
||||||
|
|
||||||
async def _is_dir(self, path: str) -> bool:
|
async def _is_dir(self, path: str) -> bool:
|
||||||
try:
|
try:
|
||||||
|
|
@ -207,9 +217,7 @@ class FileContext(abc.ABC):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def is_dir(self, path: str) -> bool:
|
async def is_dir(self, path: str) -> bool:
|
||||||
if not path:
|
return self._is_dir(self._chroot(path))
|
||||||
raise ValueError('Tried to investigate file system resource with empty path')
|
|
||||||
return self._is_dir(path)
|
|
||||||
|
|
||||||
async def _close(self) -> None:
|
async def _close(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue