Bug 772595 - Better escape commands sent to devicemanager shell;r=jmaher

This commit is contained in:
William Lachance 2012-07-12 12:12:59 -04:00
parent fd7f652fe8
commit 2da8fac0b6
3 changed files with 28 additions and 18 deletions

View File

@ -509,7 +509,28 @@ class DeviceManager:
success: True
failure: False
"""
@staticmethod
def _escapedCommandLine(cmd):
""" Utility function to return escaped and quoted version of command line """
quotedCmd = []
for arg in cmd:
arg.replace('&', '\&')
needsQuoting = False
for char in [ ' ', '(', ')', '"', '&' ]:
if arg.find(char) >= 0:
needsQuoting = True
break
if needsQuoting:
arg = '\'%s\'' % arg
quotedCmd.append(arg)
return " ".join(quotedCmd)
class NetworkTools:
def __init__(self):
pass

View File

@ -95,24 +95,13 @@ class DeviceManagerADB(DeviceManager):
# success: <return code>
# failure: None
def shell(self, cmd, outputfile, env=None, cwd=None):
# need to quote and escape special characters here
for (index, arg) in enumerate(cmd):
arg.replace('&', '\&')
needsQuoting = False
for char in [ ' ', '(', ')', '"', '&' ]:
if arg.find(char):
needsQuoting = True
break
if needsQuoting:
cmd[index] = '\'%s\'' % arg
# This is more complex than you'd think because adb doesn't actually
# return the return code from a process, so we have to capture the output
# to get it
# FIXME: this function buffers all output of the command into memory,
# always. :(
cmdline = " ".join(cmd) + "; echo $?"
# Getting the return code is more complex than you'd think because adb
# doesn't actually return the return code from a process, so we have to
# capture the output to get it
cmdline = "%s; echo $?" % self._escapedCommandLine(cmd)
# prepend cwd and env to command if necessary
if cwd:

View File

@ -260,7 +260,7 @@ class DeviceManagerSUT(DeviceManager):
# success: <return code>
# failure: None
def shell(self, cmd, outputfile, env=None, cwd=None):
cmdline = subprocess.list2cmdline(cmd)
cmdline = self._escapedCommandLine(cmd)
if env:
cmdline = '%s %s' % (self.formatEnvString(env), cmdline)