Bug 741994 - add ability to pass device serial number to DeviceManagerADB, r=jmaher

This commit is contained in:
Jonathan Griffin 2012-04-05 11:58:12 -07:00
parent 223d54613a
commit ba5cbafc28

View File

@ -8,7 +8,7 @@ import tempfile
class DeviceManagerADB(DeviceManager):
def __init__(self, host=None, port=20701, retrylimit=5, packageName=None,
adbPath='adb'):
adbPath='adb', deviceSerial=None):
self.host = host
self.port = port
self.retrylimit = retrylimit
@ -24,6 +24,10 @@ class DeviceManagerADB(DeviceManager):
# the path to adb, or 'adb' to assume that it's on the PATH
self.adbPath = adbPath
# The serial number of the device to use with adb, used in cases
# where multiple devices are being managed by the same adb instance.
self.deviceSerial = deviceSerial
if packageName:
self.packageName = packageName
else:
@ -100,7 +104,11 @@ class DeviceManagerADB(DeviceManager):
cmdline = envstr + "; " + cmdline
# all output should be in stdout
proc = subprocess.Popen([self.adbPath, "shell", cmdline],
args=[self.adbPath]
if self.deviceSerial:
args.extend(['-s', self.deviceSerial])
args.extend(["shell", cmdline])
proc = subprocess.Popen(args,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
outputfile.write(stdout.rstrip('\n'))
@ -675,11 +683,14 @@ class DeviceManagerADB(DeviceManager):
def runCmd(self, args):
# If we are not root but have run-as, and we're trying to execute
# a shell command then using run-as is the best we can do
finalArgs = [self.adbPath]
if self.deviceSerial:
finalArgs.extend(['-s', self.deviceSerial])
if (not self.haveRoot and self.useRunAs and args[0] == "shell" and args[1] != "run-as"):
args.insert(1, "run-as")
args.insert(2, self.packageName)
args.insert(0, self.adbPath)
return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
finalArgs.extend(args)
return subprocess.Popen(finalArgs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def runCmdAs(self, args):
if self.useRunAs:
@ -690,11 +701,14 @@ class DeviceManagerADB(DeviceManager):
def checkCmd(self, args):
# If we are not root but have run-as, and we're trying to execute
# a shell command then using run-as is the best we can do
finalArgs = [self.adbPath]
if self.deviceSerial:
finalArgs.extend(['-s', self.deviceSerial])
if (not self.haveRoot and self.useRunAs and args[0] == "shell" and args[1] != "run-as"):
args.insert(1, "run-as")
args.insert(2, self.packageName)
args.insert(0, self.adbPath)
return subprocess.check_call(args)
finalArgs.extend(args)
return subprocess.check_call(finalArgs)
def checkCmdAs(self, args):
if (self.useRunAs):
@ -731,6 +745,23 @@ class DeviceManagerADB(DeviceManager):
raise DMError("unable to execute ADB: ensure Android SDK is installed and adb is in your $PATH")
def verifyDevice(self):
# If there is a device serial number, see if adb is connected to it
if self.deviceSerial:
deviceStatus = None
proc = subprocess.Popen([self.adbPath, "devices"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in proc.stdout:
m = re.match('(.+)?\s+(.+)$', line)
if m:
if self.deviceSerial == m.group(1):
deviceStatus = m.group(2)
if deviceStatus == None:
raise DMError("device not found: %s" % self.deviceSerial)
elif deviceStatus != "device":
raise DMError("bad status for device %s: %s" % (self.deviceSerial,
deviceStatus))
# Check to see if we can connect to device and run a simple command
try:
self.checkCmd(["shell", "echo"])