diff --git a/src/python/jw/pkg/lib/ExecContext.py b/src/python/jw/pkg/lib/ExecContext.py index 0a29205b..73c06c32 100644 --- a/src/python/jw/pkg/lib/ExecContext.py +++ b/src/python/jw/pkg/lib/ExecContext.py @@ -240,9 +240,13 @@ class ExecContext(Base): super().__init__(uri=uri, interactive=interactive, verbose_default=verbose_default) @abc.abstractmethod - async def _run(self, *args, **kwargs) -> Result: + def _username(self) -> str: pass + @property + def username(self) -> str: + return self._username() + async def run( self, cmd: list[str], diff --git a/src/python/jw/pkg/lib/ec/Local.py b/src/python/jw/pkg/lib/ec/Local.py index f0b449c3..e4193d84 100644 --- a/src/python/jw/pkg/lib/ec/Local.py +++ b/src/python/jw/pkg/lib/ec/Local.py @@ -2,6 +2,8 @@ import os, sys, subprocess, asyncio, pwd, grp, stat +from functools import cache + from ..ExecContext import ExecContext as Base from ..base import Result, StatResult @@ -13,6 +15,10 @@ class Local(Base): def __init__(self, uri='local', *args, **kwargs) -> None: super().__init__(uri, *args, **kwargs) + @cache + def _username(self) -> str: + return pwd.getpwuid(os.getuid()).pw_name, + async def _run( self, cmd: list[str], diff --git a/src/python/jw/pkg/lib/ec/SSHClient.py b/src/python/jw/pkg/lib/ec/SSHClient.py index 0f61bb84..aca3648b 100644 --- a/src/python/jw/pkg/lib/ec/SSHClient.py +++ b/src/python/jw/pkg/lib/ec/SSHClient.py @@ -2,7 +2,7 @@ from typing import Any -import os, abc, sys +import os, abc, sys, pwd from enum import Flag, auto from ..util import pretty_cmd @@ -128,8 +128,9 @@ class SSHClient(ExecContext): def set_username(self, username: str) -> None: self.__username = username - @property - def username(self) -> str|None: + def _username(self) -> str: + if self.__username is None: + return pwd.getpwuid(os.getuid()).pw_name return self.__username def ssh_client(*args, type: str|list[str]|None=None, **kwargs) -> SSHClient: # export