Fix the pylint issues that have built up...

Change-Id: I73b14c468a27dffb5c3524b2141682f9e1cafe35
This commit is contained in:
Dan Albert
2016-02-24 11:38:40 -08:00
parent 16801f2d4a
commit 5ca14aa777

View File

@@ -40,7 +40,7 @@ class NoUniqueDeviceError(FindDeviceError):
class ShellError(RuntimeError): class ShellError(RuntimeError):
def __init__(self, cmd, stdout, stderr, exit_code): def __init__(self, cmd, stdout, stderr, exit_code):
super(ShellError, self).__init__( super(ShellError, self).__init__(
'`{0}` exited with code {1}'.format(cmd, exit_code)) '`{0}` exited with code {1}'.format(cmd, exit_code))
self.cmd = cmd self.cmd = cmd
self.stdout = stdout self.stdout = stdout
self.stderr = stderr self.stderr = stderr
@@ -117,7 +117,8 @@ def _get_device_by_type(flag, adb_path):
subprocess.check_call([adb_path, 'start-server'], stdout=devnull, subprocess.check_call([adb_path, 'start-server'], stdout=devnull,
stderr=devnull) stderr=devnull)
try: try:
serial = subprocess.check_output([adb_path, flag, 'get-serialno']).strip() serial = subprocess.check_output(
[adb_path, flag, 'get-serialno']).strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
raise RuntimeError('adb unexpectedly returned nonzero') raise RuntimeError('adb unexpectedly returned nonzero')
if serial == 'unknown': if serial == 'unknown':
@@ -151,20 +152,22 @@ def get_emulator_device(adb_path='adb'):
return _get_device_by_type('-e', adb_path=adb_path) return _get_device_by_type('-e', adb_path=adb_path)
# If necessary, modifies subprocess.check_output() or subprocess.Popen() args to run the subprocess # If necessary, modifies subprocess.check_output() or subprocess.Popen() args
# via Windows PowerShell to work-around an issue in Python 2's subprocess class on Windows where it # to run the subprocess via Windows PowerShell to work-around an issue in
# doesn't support Unicode. # Python 2's subprocess class on Windows where it doesn't support Unicode.
def _get_subprocess_args(args): def _get_subprocess_args(args):
# Only do this slow work-around if Unicode is in the cmd line on Windows. PowerShell takes # Only do this slow work-around if Unicode is in the cmd line on Windows.
# 600-700ms to startup on a 2013-2014 machine, which is very slow. # PowerShell takes 600-700ms to startup on a 2013-2014 machine, which is
if (os.name != 'nt' or all(not isinstance(arg, unicode) for arg in args[0])): # very slow.
if os.name != 'nt' or all(not isinstance(arg, unicode) for arg in args[0]):
return args return args
def escape_arg(arg): def escape_arg(arg):
# Escape for the parsing that the C Runtime does in Windows apps. In particular, this will # Escape for the parsing that the C Runtime does in Windows apps. In
# take care of double-quotes. # particular, this will take care of double-quotes.
arg = subprocess.list2cmdline([arg]) arg = subprocess.list2cmdline([arg])
# Escape single-quote with another single-quote because we're about to... # Escape single-quote with another single-quote because we're about
# to...
arg = arg.replace(u"'", u"''") arg = arg.replace(u"'", u"''")
# ...put the arg in a single-quoted string for PowerShell to parse. # ...put the arg in a single-quoted string for PowerShell to parse.
arg = u"'" + arg + u"'" arg = u"'" + arg + u"'"
@@ -172,22 +175,26 @@ def _get_subprocess_args(args):
# Escape command line args. # Escape command line args.
argv = map(escape_arg, args[0]) argv = map(escape_arg, args[0])
# Cause script errors (such as adb not found) to stop script immediately with an error. # Cause script errors (such as adb not found) to stop script immediately
ps_code = u'$ErrorActionPreference = "Stop"\r\n'; # with an error.
# Add current directory to the PATH var, to match cmd.exe/CreateProcess() behavior. ps_code = u'$ErrorActionPreference = "Stop"\r\n'
ps_code += u'$env:Path = ".;" + $env:Path\r\n'; # Add current directory to the PATH var, to match cmd.exe/CreateProcess()
# behavior.
ps_code += u'$env:Path = ".;" + $env:Path\r\n'
# Precede by &, the PowerShell call operator, and separate args by space. # Precede by &, the PowerShell call operator, and separate args by space.
ps_code += u'& ' + u' '.join(argv) ps_code += u'& ' + u' '.join(argv)
# Make the PowerShell exit code the exit code of the subprocess. # Make the PowerShell exit code the exit code of the subprocess.
ps_code += u'\r\nExit $LastExitCode' ps_code += u'\r\nExit $LastExitCode'
# Encode as UTF-16LE (without Byte-Order-Mark) which Windows natively understands. # Encode as UTF-16LE (without Byte-Order-Mark) which Windows natively
# understands.
ps_code = ps_code.encode('utf-16le') ps_code = ps_code.encode('utf-16le')
# Encode the PowerShell command as base64 and use the special -EncodedCommand option that base64 # Encode the PowerShell command as base64 and use the special
# decodes. Base64 is just plain ASCII, so it should have no problem passing through Win32 # -EncodedCommand option that base64 decodes. Base64 is just plain ASCII,
# CreateProcessA() (which python erroneously calls instead of CreateProcessW()). # so it should have no problem passing through Win32 CreateProcessA()
return (['powershell.exe', '-NoProfile', '-NonInteractive', '-EncodedCommand', # (which python erroneously calls instead of CreateProcessW()).
base64.b64encode(ps_code)],) + args[1:] return (['powershell.exe', '-NoProfile', '-NonInteractive',
'-EncodedCommand', base64.b64encode(ps_code)],) + args[1:]
# Call this instead of subprocess.check_output() to work-around issue in Python # Call this instead of subprocess.check_output() to work-around issue in Python
@@ -219,7 +226,8 @@ class AndroidDevice(object):
# Maximum search distance from the output end to find the delimiter. # Maximum search distance from the output end to find the delimiter.
# adb on Windows returns \r\n even if adbd returns \n. # adb on Windows returns \r\n even if adbd returns \n.
_RETURN_CODE_SEARCH_LENGTH = len('{0}255\r\n'.format(_RETURN_CODE_DELIMITER)) _RETURN_CODE_SEARCH_LENGTH = len(
'{0}255\r\n'.format(_RETURN_CODE_DELIMITER))
# Feature name strings. # Feature name strings.
SHELL_PROTOCOL_FEATURE = 'shell_v2' SHELL_PROTOCOL_FEATURE = 'shell_v2'
@@ -281,8 +289,8 @@ class AndroidDevice(object):
if partition[1] == '': if partition[1] == '':
raise RuntimeError('Could not find exit status in shell output.') raise RuntimeError('Could not find exit status in shell output.')
result = int(partition[2]) result = int(partition[2])
# partition[0] won't contain the full text if search_text was truncated, # partition[0] won't contain the full text if search_text was
# pull from the original string instead. # truncated, pull from the original string instead.
out = out[:-len(partition[1]) - len(partition[2])] out = out[:-len(partition[1]) - len(partition[2])]
return result, out return result, out