1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 10:10:46 +00:00

utils.adb_shell: Improve stability (Py3)

Move from pipes.quote (private) to shlex.quote (Py3.3+ standard).

Make tests of inputs against None (their default value) instead of based
on their truthiness.

Improve logging through quoted commands (runnable as-is, less confusing).

Make the command-building process straightforward for readability and
maintainability.
This commit is contained in:
Pierre-Clément Tosi 2019-05-14 12:12:20 +01:00 committed by Marc Bonnici
parent 1325e59b1a
commit 0d798f1c4f

View File

@ -31,6 +31,9 @@ import pexpect
import xml.etree.ElementTree import xml.etree.ElementTree
import zipfile import zipfile
try:
from shlex import quote
except ImportError:
from pipes import quote from pipes import quote
from devlib.exception import TargetTransientError, TargetStableError, HostError from devlib.exception import TargetTransientError, TargetStableError, HostError
@ -422,23 +425,23 @@ def _ping(device):
def adb_shell(device, command, timeout=None, check_exit_code=False, def adb_shell(device, command, timeout=None, check_exit_code=False,
as_root=False, adb_server=None): # NOQA as_root=False, adb_server=None): # NOQA
_check_env() _check_env()
if as_root: parts = ['adb']
command = 'echo {} | su'.format(quote(command)) if adb_server is not None:
device_part = [] parts += ['-H', adb_server]
if adb_server: if device is not None:
device_part = ['-H', adb_server] parts += ['-s', device]
device_part += ['-s', device] if device else [] parts += ['shell',
command if not as_root else 'echo {} | su'.format(quote(command))]
logger.debug(' '.join(quote(part) for part in parts))
# On older combinations of ADB/Android versions, the adb host command always # On older combinations of ADB/Android versions, the adb host command always
# exits with 0 if it was able to run the command on the target, even if the # exits with 0 if it was able to run the command on the target, even if the
# command failed (https://code.google.com/p/android/issues/detail?id=3254). # command failed (https://code.google.com/p/android/issues/detail?id=3254).
# Homogenise this behaviour by running the command then echoing the exit # Homogenise this behaviour by running the command then echoing the exit
# code. # code.
adb_shell_command = '({}); echo \"\n$?\"'.format(command) parts[-1] += ' ; echo "\n$?"'
actual_command = ['adb'] + device_part + ['shell', adb_shell_command]
logger.debug('adb {} shell {}'.format(' '.join(device_part), command))
try: try:
raw_output, _ = check_output(actual_command, timeout, shell=False, combined_output=True) raw_output, _ = check_output(parts, timeout, shell=False, combined_output=True)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise TargetStableError(str(e)) raise TargetStableError(str(e))