1
0
mirror of https://github.com/ARM-software/devlib.git synced 2024-10-05 18:30:50 +01:00

host: Remove sudo prompt from stderr in execute()

Remove the leading space introduced on stderr by: sudo -S -p ' '
background() still gets the space, since we cannot easily apply
processing to its stderr.

Note: -p '' does not work on recent sudo, so we unfortunately cannot
just completely remove it for the time being.
This commit is contained in:
douglas-raillard-arm 2020-11-18 17:13:17 +00:00 committed by Marc Bonnici
parent a948982700
commit 5b37dfc50b

View File

@ -97,7 +97,8 @@ class LocalConnection(ConnectionBase):
def execute(self, command, timeout=None, check_exit_code=True,
as_root=False, strip_colors=True, will_succeed=False):
self.logger.debug(command)
if as_root and not self.connected_as_root:
use_sudo = as_root and not self.connected_as_root
if use_sudo:
if self.unrooted:
raise TargetStableError('unrooted')
password = self._get_password()
@ -112,6 +113,11 @@ class LocalConnection(ConnectionBase):
raise TargetTransientError(message)
else:
raise TargetStableError(message)
# Remove the one-character prompt of sudo -S -p
if use_sudo and stderr:
stderr = stderr[1:]
return stdout + stderr
def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False):
@ -119,6 +125,8 @@ class LocalConnection(ConnectionBase):
if self.unrooted:
raise TargetStableError('unrooted')
password = self._get_password()
# The sudo prompt will add a space on stderr, but we cannot filter
# it out here
command = "echo {} | sudo -p ' ' -S ".format(quote(password)) + command
# Make sure to get a new PGID so PopenBackgroundCommand() can kill