jw.pkg.util: Add sudo()

Move the body of BackendCmd.sudo() into a function. The rationale
behind that is that its functionality is independent of the calling
object for the most part, so having it in a function instead of a
method is the more modular pattern.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-02-17 10:19:57 +01:00
commit b1d4e20295
2 changed files with 20 additions and 18 deletions

View file

@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
import os
from ....lib.util import run_cmd
from ....lib.util import run_cmd, run_sudo
from ..Cmd import Cmd
class BackendCmd:
@ -11,21 +9,7 @@ class BackendCmd:
self.__parent = parent
async def _sudo(self, cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[]):
env: dict[str, str]|None = None
cmd_input: str|None = None
if mod_env:
env = os.environ.copy()
env.update(mod_env)
cmdline = []
if os.getuid() != 0:
cmdline.append('/usr/bin/sudo')
if env is not None:
cmdline.append('--preserve-env=' + ','.join(mod_env.keys()))
cmdline.extend(opts)
cmdline.extend(cmd)
if self.interactive:
cmd_input = "mode:interactive"
stdout, stderr = await run_cmd(*cmdline, throw=True, verbose=True, env=env, cmd_input=cmd_input)
return await run_sudo(cmd, mod_env=mod_env, opts=opts, interactive=self.interactive)
@property
def parent(self):