mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 17:23:36 +02:00
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:
parent
cb3ccaa338
commit
b1d4e20295
2 changed files with 20 additions and 18 deletions
|
|
@ -1,8 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
from ....lib.util import run_cmd, run_sudo
|
||||||
|
|
||||||
from ....lib.util import run_cmd
|
|
||||||
from ..Cmd import Cmd
|
from ..Cmd import Cmd
|
||||||
|
|
||||||
class BackendCmd:
|
class BackendCmd:
|
||||||
|
|
@ -11,21 +9,7 @@ class BackendCmd:
|
||||||
self.__parent = parent
|
self.__parent = parent
|
||||||
|
|
||||||
async def _sudo(self, cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[]):
|
async def _sudo(self, cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[]):
|
||||||
env: dict[str, str]|None = None
|
return await run_sudo(cmd, mod_env=mod_env, opts=opts, interactive=self.interactive)
|
||||||
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)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent(self):
|
def parent(self):
|
||||||
|
|
|
||||||
|
|
@ -213,6 +213,24 @@ async def run_askpass(askpass_env: list[str], key: AskpassKey, host: str|None=No
|
||||||
return ret
|
return ret
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
async def run_sudo(cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[], interactive: bool=True):
|
||||||
|
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 interactive:
|
||||||
|
cmd_input = "mode:interactive"
|
||||||
|
stdout, stderr = await run_cmd(*cmdline, throw=True, verbose=True, env=env, cmd_input=cmd_input)
|
||||||
|
return stdout, stderr
|
||||||
|
|
||||||
async def get_username(args: Namespace|None=None, url: str|None=None, askpass_env: list[str]=[]) -> str: # export
|
async def get_username(args: Namespace|None=None, url: str|None=None, askpass_env: list[str]=[]) -> str: # export
|
||||||
url_user = None if url is None else urlparse(url).username
|
url_user = None if url is None else urlparse(url).username
|
||||||
if args is not None:
|
if args is not None:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue