jw.pkg.cmds.distro.backend.BackendCmd: Add sudo(opts)

Prepending --login to the argument list of BackendCmd.sudo() fails if
run as root, because BackendCmd.sudo() detects that and leaves the
/usr/bin/sudo out from the exec call. Fix that by adding an
opts: list[str] parameter to sudo, defaulting to an empty list, with
options that should be passed to /usr/bin/sudo.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-02-13 08:36:45 +01:00
commit 0bc06b7c41
2 changed files with 5 additions and 4 deletions

View file

@ -10,7 +10,7 @@ class BackendCmd:
def __init__(self, parent: Cmd): def __init__(self, parent: Cmd):
self.__parent = parent self.__parent = parent
async def _sudo(self, cmd: list[str], mod_env: dict[str, str] = {}): async def _sudo(self, cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[]):
env: dict[str, str]|None = None env: dict[str, str]|None = None
cmd_input: str|None = None cmd_input: str|None = None
if mod_env: if mod_env:
@ -21,6 +21,7 @@ class BackendCmd:
cmdline.append('/usr/bin/sudo') cmdline.append('/usr/bin/sudo')
if env is not None: if env is not None:
cmdline.append('--preserve-env=' + ','.join(mod_env.keys())) cmdline.append('--preserve-env=' + ','.join(mod_env.keys()))
cmdline.extend(opts)
cmdline.extend(cmd) cmdline.extend(cmd)
if self.interactive: if self.interactive:
cmd_input = "mode:interactive" cmd_input = "mode:interactive"

View file

@ -9,9 +9,9 @@ class Base(BackendCmd):
super().__init__(parent) super().__init__(parent)
async def zypper(self, *args): async def zypper(self, *args):
# Run sudo --login, because /etc/profile may modify ZYPP_CONF cmd = ['/usr/bin/zypper']
cmd = ['--login', '/usr/bin/zypper']
if not self.interactive: if not self.interactive:
cmd.extend(['--non-interactive', '--gpg-auto-import-keys', '--no-gpg-checks']) cmd.extend(['--non-interactive', '--gpg-auto-import-keys', '--no-gpg-checks'])
cmd.extend(args) cmd.extend(args)
return await self._sudo(cmd) # Run sudo --login in case /etc/profile modifies ZYPP_CONF
return await self._sudo(cmd, opts=['--login'])