#NEWLINE# def adb_shell( device , command , timeout = None , check_exit_code = False , as_root = False ) : #NEWLINE# #INDENT# _check_env ( ) #NEWLINE# if as_root : #NEWLINE# #INDENT# command = "echo '{}' | su" . format ( escape_single_quotes ( command ) ) #NEWLINE# #UNINDENT# device_string = ( '-s {}' . format ( device ) if device else '' ) #NEWLINE# full_command = 'adb {} shell "{}"' . format ( device_string , escape_double_quotes ( command ) ) #NEWLINE# logger . debug ( full_command ) #NEWLINE# if check_exit_code : #NEWLINE# #INDENT# actual_command = "adb {} shell '({}); echo; echo $?'" . format ( device_string , escape_single_quotes ( command ) ) #NEWLINE# try : #NEWLINE# #INDENT# ( raw_output , error ) = check_output ( actual_command , timeout , shell = True ) #NEWLINE# #UNINDENT# except CalledProcessErrorWithStderr as e : #NEWLINE# #INDENT# raw_output = e . output #NEWLINE# error = e . error #NEWLINE# exit_code = e . returncode #NEWLINE# if ( newline == 1 ) : #NEWLINE# #INDENT# logger . debug ( 'Exit code 1 could be either the return code of the command or mean ADB failed' ) #NEWLINE# #UNINDENT# #UNINDENT# if raw_output : #NEWLINE# #INDENT# if raw_output . endswith ( '\r\n' ) : #NEWLINE# #INDENT# newline = '\r\n' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if raw_output . endswith ( '\n' ) : #NEWLINE# #INDENT# newline = '\n' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise WAError ( 'Unknown new line separator in: {}' . format ( raw_output ) ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# ( output , exit_code , _ ) = raw_output . rsplit ( newline , 2 ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# ( exit_code , _ ) = raw_output . rsplit ( newline , 1 ) #NEWLINE# output = '' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# exit_code = '969696' #NEWLINE# output = '' #NEWLINE# #UNINDENT# exit_code = exit_code . strip ( ) #NEWLINE# if exit_code . isdigit ( ) : #NEWLINE# #INDENT# if int ( exit_code ) : #NEWLINE# #INDENT# message = 'Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}' . format ( exit_code , full_command , output , error ) #NEWLINE# raise DeviceError ( message ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if am_start_error . findall ( output ) : #NEWLINE# #INDENT# message = 'Could not start activity; got the following:' #NEWLINE# message += '\n{}' . format ( am_start_error . findall ( output ) [ 0 ] ) #NEWLINE# raise DeviceError ( message ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if am_start_error . findall ( output ) : #NEWLINE# #INDENT# message = 'Could not start activity; got the following:' #NEWLINE# message += '\n{}' . format ( am_start_error . findall ( output ) [ 0 ] ) #NEWLINE# raise DeviceError ( message ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise DeviceError ( 'adb has returned early; did not get an exit code. Was kill-server invoked?' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( output , _ ) = check_output ( full_command , timeout , shell = True ) #NEWLINE# #UNINDENT# except CalledProcessErrorWithStderr as e : #NEWLINE# #INDENT# output = e . output #NEWLINE# error = e . error #NEWLINE# exit_code = e . returncode #NEWLINE# if ( e . returncode == 1 ) : #NEWLINE# #INDENT# logger . debug ( 'Got Exit code 1, could be either the return code of the command or mean ADB failed' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return output