def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False):  # NOQA
    # pylint: disable=too-many-branches, too-many-locals, too-many-statements
    _check_env()
    if as_root:
        command = 'echo \'{}\' | su'.format(escape_single_quotes(command))
    device_string = '-s {}'.format(device) if device else ''
    full_command = 'adb {} shell "{}"'.format(device_string, escape_double_quotes(command))
    logger.debug(full_command)
    if check_exit_code:
        actual_command = "adb {} shell '({}); echo; echo $?'".format(device_string, escape_single_quotes(command))
        try:
            raw_output, error = check_output(actual_command, timeout, shell=True)
        except CalledProcessErrorWithStderr as e:
            raw_output = e.output
            error = e.error
            exit_code = e.returncode
            if exit_code == 1:
                logger.debug("Exit code 1 could be either the return code of the command or mean ADB failed")

        if raw_output:
            if raw_output.endswith('\r\n'):
                newline = '\r\n'
            elif raw_output.endswith('\n'):
                newline = '\n'
            else:
                raise WAError("Unknown new line separator in: {}".format(raw_output))

            try:
                output, exit_code, _ = raw_output.rsplit(newline, 2)
            except __HOLE__:
                exit_code, _ = raw_output.rsplit(newline, 1)
                output = ''
        else:  # raw_output is empty
            exit_code = '969696'  # just because
            output = ''

        exit_code = exit_code.strip()
        if exit_code.isdigit():
            if int(exit_code):
                message = 'Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}'.format(exit_code, full_command,
                                                                                      output, error)
                raise DeviceError(message)
            elif am_start_error.findall(output):
                message = 'Could not start activity; got the following:'
                message += '\n{}'.format(am_start_error.findall(output)[0])
                raise DeviceError(message)
        else:  # not all digits
            if am_start_error.findall(output):
                message = 'Could not start activity; got the following:'
                message += '\n{}'.format(am_start_error.findall(output)[0])
                raise DeviceError(message)
            else:
                raise DeviceError('adb has returned early; did not get an exit code. Was kill-server invoked?')
    else:  # do not check exit code
        try:
            output, _ = check_output(full_command, timeout, shell=True)
        except CalledProcessErrorWithStderr as e:
            output = e.output
            error = e.error
            exit_code = e.returncode
            if e.returncode == 1:
                logger.debug("Got Exit code 1, could be either the return code of the command or mean ADB failed")
    return output
dataset/ETHPy150Open ARM-software/workload-automation/wlauto/utils/android.py/adb_shell