lib.ExecContext.username: Add property

Add the property .username, backed by the protected _username()
callback. It should return the user run()'s cmd parameter is executed
as.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-19 14:03:31 +02:00
commit 9f756222fe
3 changed files with 15 additions and 4 deletions

View file

@ -240,9 +240,13 @@ class ExecContext(Base):
super().__init__(uri=uri, interactive=interactive, verbose_default=verbose_default) super().__init__(uri=uri, interactive=interactive, verbose_default=verbose_default)
@abc.abstractmethod @abc.abstractmethod
async def _run(self, *args, **kwargs) -> Result: def _username(self) -> str:
pass pass
@property
def username(self) -> str:
return self._username()
async def run( async def run(
self, self,
cmd: list[str], cmd: list[str],

View file

@ -2,6 +2,8 @@
import os, sys, subprocess, asyncio, pwd, grp, stat import os, sys, subprocess, asyncio, pwd, grp, stat
from functools import cache
from ..ExecContext import ExecContext as Base from ..ExecContext import ExecContext as Base
from ..base import Result, StatResult from ..base import Result, StatResult
@ -13,6 +15,10 @@ class Local(Base):
def __init__(self, uri='local', *args, **kwargs) -> None: def __init__(self, uri='local', *args, **kwargs) -> None:
super().__init__(uri, *args, **kwargs) super().__init__(uri, *args, **kwargs)
@cache
def _username(self) -> str:
return pwd.getpwuid(os.getuid()).pw_name,
async def _run( async def _run(
self, self,
cmd: list[str], cmd: list[str],

View file

@ -2,7 +2,7 @@
from typing import Any from typing import Any
import os, abc, sys import os, abc, sys, pwd
from enum import Flag, auto from enum import Flag, auto
from ..util import pretty_cmd from ..util import pretty_cmd
@ -128,8 +128,9 @@ class SSHClient(ExecContext):
def set_username(self, username: str) -> None: def set_username(self, username: str) -> None:
self.__username = username self.__username = username
@property def _username(self) -> str:
def username(self) -> str|None: if self.__username is None:
return pwd.getpwuid(os.getuid()).pw_name
return self.__username return self.__username
def ssh_client(*args, type: str|list[str]|None=None, **kwargs) -> SSHClient: # export def ssh_client(*args, type: str|list[str]|None=None, **kwargs) -> SSHClient: # export