mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-28 16:15:24 +02:00
lib.FileContext: Add file methods
Add the following methods, meant to do the obvious: unlink(self, path: str) -> None erase(self, path: str) -> None rename(self, src: str, dst: str) -> None mktemp(self, tmpl: str, directory: bool=False) -> None chown(self, path: str, owner: str|None=None, group: str|None=None) -> None chmod(self, path: str, mode: int) -> None stat(self, path: str, follow_symlinks: bool=True) -> StatResult file_exists(self, path: str) -> bool is_dir(self, path: str) -> bool All methods are async and call their protected counterpart, which is designed to be overridden. If possible, default implementations do something meaningful, if not, they just raise plain NotImplementedError. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
bc3ed1737f
commit
3cf5b2264e
5 changed files with 295 additions and 10 deletions
|
|
@ -1,9 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os, sys, subprocess, asyncio
|
||||
import os, sys, subprocess, asyncio, pwd, grp, stat
|
||||
|
||||
from ..ExecContext import ExecContext as Base
|
||||
from ..base import Result
|
||||
from ..base import Result, StatResult
|
||||
|
||||
from ..log import *
|
||||
from ..util import pretty_cmd
|
||||
|
|
@ -151,3 +151,32 @@ class Local(Base):
|
|||
cmdline.extend(opts)
|
||||
cmdline.extend(cmd)
|
||||
return await self._run(cmdline, *args, **kwargs)
|
||||
|
||||
async def _unlink(self, path: str) -> None:
|
||||
os.unlink(path)
|
||||
|
||||
async def _erase(self, path: str) -> None:
|
||||
if os.isdir(path):
|
||||
shutil.rmtree(path)
|
||||
return
|
||||
os.unlink(path)
|
||||
|
||||
async def _rename(self, src: str, dst: str) -> None:
|
||||
os.rename(src, dst)
|
||||
|
||||
async def _stat(self, path: str, follow_symlinks: bool) -> StatResult:
|
||||
return StatResult.from_os(os.stat(path, follow_symlinks=follow_symlinks))
|
||||
|
||||
async def _file_exists(self, path: str) -> bool:
|
||||
return os.path.exists(path)
|
||||
|
||||
async def _chown(self, path: str, owner: str|None, group: str|None) -> None:
|
||||
uid = pwd.getpwnam(owner).pw_uid if owner else -1
|
||||
gid = grp.getgrnam(group).gr_gid if group else -1
|
||||
os.chown(path, uid, gid)
|
||||
|
||||
async def _chmod(self, path: str, mode: int) -> None:
|
||||
os.chmod(path, mode)
|
||||
|
||||
async def _is_dir(self, path: str) -> bool:
|
||||
return os.path.isdir(path)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue