2012-05-01 12:50:17 -07:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
# You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2011-05-06 15:17:55 -07:00
|
|
|
import subprocess
|
2012-02-23 07:32:28 -08:00
|
|
|
from devicemanager import DeviceManager, DMError, _pop_last_line
|
2011-05-06 15:17:55 -07:00
|
|
|
import re
|
2011-07-17 09:04:40 -07:00
|
|
|
import os
|
2011-08-23 09:45:10 -07:00
|
|
|
import sys
|
2011-12-05 04:19:31 -08:00
|
|
|
import tempfile
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
class DeviceManagerADB(DeviceManager):
|
|
|
|
|
2012-04-05 12:06:41 -07:00
|
|
|
def __init__(self, host=None, port=20701, retrylimit=5, packageName='fennec',
|
2012-04-05 11:58:12 -07:00
|
|
|
adbPath='adb', deviceSerial=None):
|
2011-05-06 15:17:55 -07:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.retrylimit = retrylimit
|
|
|
|
self.retries = 0
|
|
|
|
self._sock = None
|
2011-08-23 09:45:10 -07:00
|
|
|
self.useRunAs = False
|
2012-02-01 08:43:15 -08:00
|
|
|
self.haveRoot = False
|
2012-03-19 08:09:39 -07:00
|
|
|
self.useDDCopy = False
|
2011-12-05 04:19:31 -08:00
|
|
|
self.useZip = False
|
2011-08-26 16:23:02 -07:00
|
|
|
self.packageName = None
|
2012-02-01 08:43:15 -08:00
|
|
|
self.tempDir = None
|
2012-05-08 07:23:19 -07:00
|
|
|
self.deviceRoot = None
|
2012-03-16 15:36:28 -07:00
|
|
|
|
2012-04-04 10:27:59 -07:00
|
|
|
# the path to adb, or 'adb' to assume that it's on the PATH
|
|
|
|
self.adbPath = adbPath
|
|
|
|
|
2012-04-05 11:58:12 -07:00
|
|
|
# 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
|
|
|
|
|
2012-04-05 12:06:41 -07:00
|
|
|
if packageName == 'fennec':
|
2011-08-22 01:00:50 -07:00
|
|
|
if os.getenv('USER'):
|
2012-03-16 15:36:28 -07:00
|
|
|
self.packageName = 'org.mozilla.fennec_' + os.getenv('USER')
|
2011-08-22 01:00:50 -07:00
|
|
|
else:
|
2012-03-16 15:36:28 -07:00
|
|
|
self.packageName = 'org.mozilla.fennec_'
|
2012-04-05 12:06:41 -07:00
|
|
|
elif packageName:
|
|
|
|
self.packageName = packageName
|
2011-07-17 09:04:40 -07:00
|
|
|
|
2012-03-16 15:36:28 -07:00
|
|
|
# verify that we can run the adb command. can't continue otherwise
|
|
|
|
self.verifyADB()
|
|
|
|
|
|
|
|
# try to connect to the device over tcp/ip if we have a hostname
|
2012-02-28 11:15:37 -08:00
|
|
|
if self.host:
|
2012-03-16 15:36:28 -07:00
|
|
|
self.connectRemoteADB()
|
2012-02-28 11:15:37 -08:00
|
|
|
|
2012-03-16 15:36:28 -07:00
|
|
|
# verify that we can connect to the device. can't continue
|
|
|
|
self.verifyDevice()
|
2012-02-01 08:43:15 -08:00
|
|
|
|
2012-05-08 07:23:19 -07:00
|
|
|
# set up device root
|
|
|
|
self.setupDeviceRoot()
|
|
|
|
|
2012-03-16 15:36:28 -07:00
|
|
|
# Can we use run-as? (currently not required)
|
|
|
|
try:
|
|
|
|
self.verifyRunAs()
|
|
|
|
except DMError:
|
|
|
|
pass
|
2012-02-01 08:43:15 -08:00
|
|
|
|
2012-03-16 15:36:28 -07:00
|
|
|
# Can we run things as root? (currently not required)
|
2012-04-20 14:15:59 -07:00
|
|
|
useRunAsTmp = self.useRunAs
|
|
|
|
self.useRunAs = False
|
2012-02-01 08:43:15 -08:00
|
|
|
try:
|
2012-03-16 15:36:28 -07:00
|
|
|
self.verifyRoot()
|
|
|
|
except DMError, e:
|
2011-05-06 15:17:55 -07:00
|
|
|
try:
|
|
|
|
self.checkCmd(["root"])
|
2012-02-01 08:43:15 -08:00
|
|
|
# The root command does not fail even if ADB cannot get
|
|
|
|
# root rights (e.g. due to production builds), so we have
|
|
|
|
# to check again ourselves that we have root now.
|
2012-03-16 15:36:28 -07:00
|
|
|
self.verifyRoot()
|
|
|
|
except DMError:
|
2012-04-20 14:15:59 -07:00
|
|
|
if useRunAsTmp:
|
2012-02-01 08:43:15 -08:00
|
|
|
print "restarting as root failed, but run-as available"
|
|
|
|
else:
|
|
|
|
print "restarting as root failed"
|
2012-04-20 14:15:59 -07:00
|
|
|
self.useRunAs = useRunAsTmp
|
2011-05-06 15:17:55 -07:00
|
|
|
|
2012-03-16 15:36:28 -07:00
|
|
|
# can we use zip to speed up some file operations? (currently not
|
|
|
|
# required)
|
|
|
|
try:
|
|
|
|
self.verifyZip()
|
|
|
|
except DMError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if self.host:
|
|
|
|
self.disconnectRemoteADB()
|
|
|
|
|
2012-02-23 07:32:28 -08:00
|
|
|
# external function: executes shell command on device
|
|
|
|
# returns:
|
|
|
|
# success: <return code>
|
|
|
|
# failure: None
|
|
|
|
def shell(self, cmd, outputfile, env=None, cwd=None):
|
2012-05-08 08:04:58 -07:00
|
|
|
# need to quote and escape special characters here
|
2012-02-23 07:32:28 -08:00
|
|
|
for (index, arg) in enumerate(cmd):
|
2012-05-08 08:04:58 -07:00
|
|
|
arg.replace('&', '\&')
|
|
|
|
|
|
|
|
needsQuoting = False
|
|
|
|
for char in [ ' ', '(', ')', '"', '&' ]:
|
|
|
|
if arg.find(char):
|
|
|
|
needsQuoting = True
|
|
|
|
break
|
|
|
|
if needsQuoting:
|
2012-02-23 07:32:28 -08:00
|
|
|
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. :(
|
2012-02-28 09:20:56 -08:00
|
|
|
cmdline = " ".join(cmd) + "; echo $?"
|
2012-02-23 07:32:28 -08:00
|
|
|
|
|
|
|
# prepend cwd and env to command if necessary
|
|
|
|
if cwd:
|
|
|
|
cmdline = "cd %s; %s" % (cwd, cmdline)
|
|
|
|
if env:
|
|
|
|
envstr = '; '.join(map(lambda x: 'export %s=%s' % (x[0], x[1]), env.iteritems()))
|
|
|
|
cmdline = envstr + "; " + cmdline
|
|
|
|
|
|
|
|
# all output should be in stdout
|
2012-04-05 11:58:12 -07:00
|
|
|
args=[self.adbPath]
|
|
|
|
if self.deviceSerial:
|
|
|
|
args.extend(['-s', self.deviceSerial])
|
|
|
|
args.extend(["shell", cmdline])
|
|
|
|
proc = subprocess.Popen(args,
|
2012-02-23 07:32:28 -08:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = proc.communicate()
|
|
|
|
outputfile.write(stdout.rstrip('\n'))
|
|
|
|
|
|
|
|
lastline = _pop_last_line(outputfile)
|
|
|
|
if lastline:
|
|
|
|
m = re.search('([0-9]+)', lastline)
|
|
|
|
if m:
|
|
|
|
return_code = m.group(1)
|
|
|
|
outputfile.seek(-2, 2)
|
|
|
|
outputfile.truncate() # truncate off the return code
|
2012-05-23 07:26:07 -07:00
|
|
|
return int(return_code)
|
2012-02-23 07:32:28 -08:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2012-02-28 11:15:37 -08:00
|
|
|
def connectRemoteADB(self):
|
|
|
|
self.checkCmd(["connect", self.host + ":" + str(self.port)])
|
|
|
|
|
|
|
|
def disconnectRemoteADB(self):
|
|
|
|
self.checkCmd(["disconnect", self.host + ":" + str(self.port)])
|
|
|
|
|
2011-05-06 15:17:55 -07:00
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: True
|
|
|
|
# failure: False
|
|
|
|
def pushFile(self, localname, destname):
|
|
|
|
try:
|
2011-07-17 09:04:40 -07:00
|
|
|
if (os.name == "nt"):
|
|
|
|
destname = destname.replace('\\', '/')
|
2011-08-23 09:45:10 -07:00
|
|
|
if (self.useRunAs):
|
2012-02-01 08:43:15 -08:00
|
|
|
remoteTmpFile = self.getTempDir() + "/" + os.path.basename(localname)
|
2011-07-17 09:04:40 -07:00
|
|
|
self.checkCmd(["push", os.path.realpath(localname), remoteTmpFile])
|
2012-02-01 08:43:15 -08:00
|
|
|
if self.useDDCopy:
|
|
|
|
self.checkCmdAs(["shell", "dd", "if=" + remoteTmpFile, "of=" + destname])
|
|
|
|
else:
|
|
|
|
self.checkCmdAs(["shell", "cp", remoteTmpFile, destname])
|
2011-07-17 09:04:40 -07:00
|
|
|
self.checkCmd(["shell", "rm", remoteTmpFile])
|
|
|
|
else:
|
|
|
|
self.checkCmd(["push", os.path.realpath(localname), destname])
|
|
|
|
if (self.isDir(destname)):
|
|
|
|
destname = destname + "/" + os.path.basename(localname)
|
2011-05-06 15:17:55 -07:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: directory name
|
|
|
|
# failure: None
|
|
|
|
def mkDir(self, name):
|
|
|
|
try:
|
2012-04-05 12:06:41 -07:00
|
|
|
result = self.runCmdAs(["shell", "mkdir", name]).stdout.read()
|
|
|
|
if 'read-only file system' in result.lower():
|
|
|
|
return None
|
2012-05-08 07:23:19 -07:00
|
|
|
if 'file exists' in result.lower():
|
|
|
|
return name
|
2011-05-06 15:17:55 -07:00
|
|
|
return name
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# make directory structure on the device
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: directory structure that we created
|
|
|
|
# failure: None
|
|
|
|
def mkDirs(self, filename):
|
2011-07-17 09:04:40 -07:00
|
|
|
parts = filename.split('/')
|
|
|
|
name = ""
|
|
|
|
for part in parts:
|
|
|
|
if (part == parts[-1]): break
|
|
|
|
if (part != ""):
|
|
|
|
name += '/' + part
|
|
|
|
if (not self.dirExists(name)):
|
|
|
|
if (self.mkDir(name) == None):
|
|
|
|
print "failed making directory: " + str(name)
|
|
|
|
return None
|
|
|
|
return name
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
# push localDir from host to remoteDir on the device
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: remoteDir
|
|
|
|
# failure: None
|
|
|
|
def pushDir(self, localDir, remoteDir):
|
2011-07-17 09:04:40 -07:00
|
|
|
# adb "push" accepts a directory as an argument, but if the directory
|
|
|
|
# contains symbolic links, the links are pushed, rather than the linked
|
2011-12-05 04:19:31 -08:00
|
|
|
# files; we either zip/unzip or push file-by-file to get around this
|
|
|
|
# limitation
|
2011-05-06 15:17:55 -07:00
|
|
|
try:
|
2012-01-05 07:20:21 -08:00
|
|
|
if (not self.dirExists(remoteDir)):
|
|
|
|
self.mkDirs(remoteDir+"/x")
|
2011-12-05 04:19:31 -08:00
|
|
|
if (self.useZip):
|
2012-01-25 01:49:30 -08:00
|
|
|
try:
|
|
|
|
localZip = tempfile.mktemp()+".zip"
|
|
|
|
remoteZip = remoteDir + "/adbdmtmp.zip"
|
|
|
|
subprocess.check_output(["zip", "-r", localZip, '.'], cwd=localDir)
|
|
|
|
self.pushFile(localZip, remoteZip)
|
|
|
|
os.remove(localZip)
|
|
|
|
data = self.runCmdAs(["shell", "unzip", "-o", remoteZip, "-d", remoteDir]).stdout.read()
|
|
|
|
self.checkCmdAs(["shell", "rm", remoteZip])
|
|
|
|
if (re.search("unzip: exiting", data) or re.search("Operation not permitted", data)):
|
|
|
|
raise Exception("unzip failed, or permissions error")
|
|
|
|
except:
|
2012-01-05 07:20:21 -08:00
|
|
|
print "zip/unzip failure: falling back to normal push"
|
|
|
|
self.useZip = False
|
|
|
|
self.pushDir(localDir, remoteDir)
|
2011-12-05 04:19:31 -08:00
|
|
|
else:
|
2012-04-26 16:21:38 -07:00
|
|
|
for root, dirs, files in os.walk(localDir, followlinks=True):
|
2011-12-05 04:19:31 -08:00
|
|
|
relRoot = os.path.relpath(root, localDir)
|
|
|
|
for file in files:
|
|
|
|
localFile = os.path.join(root, file)
|
|
|
|
remoteFile = remoteDir + "/"
|
|
|
|
if (relRoot!="."):
|
|
|
|
remoteFile = remoteFile + relRoot + "/"
|
|
|
|
remoteFile = remoteFile + file
|
|
|
|
self.pushFile(localFile, remoteFile)
|
|
|
|
for dir in dirs:
|
|
|
|
targetDir = remoteDir + "/"
|
|
|
|
if (relRoot!="."):
|
|
|
|
targetDir = targetDir + relRoot + "/"
|
|
|
|
targetDir = targetDir + dir
|
|
|
|
if (not self.dirExists(targetDir)):
|
|
|
|
self.mkDir(targetDir)
|
2011-12-05 04:19:31 -08:00
|
|
|
return remoteDir
|
2011-05-06 15:17:55 -07:00
|
|
|
except:
|
|
|
|
print "pushing " + localDir + " to " + remoteDir + " failed"
|
2011-12-05 04:19:31 -08:00
|
|
|
return None
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: True
|
|
|
|
# failure: False
|
|
|
|
def dirExists(self, dirname):
|
2011-07-17 09:04:40 -07:00
|
|
|
return self.isDir(dirname)
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
# Because we always have / style paths we make this a lot easier with some
|
|
|
|
# assumptions
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: True
|
|
|
|
# failure: False
|
|
|
|
def fileExists(self, filepath):
|
2011-08-22 01:00:50 -07:00
|
|
|
p = self.runCmd(["shell", "ls", "-a", filepath])
|
|
|
|
data = p.stdout.readlines()
|
|
|
|
if (len(data) == 1):
|
|
|
|
if (data[0].rstrip() == filepath):
|
|
|
|
return True
|
|
|
|
return False
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
def removeFile(self, filename):
|
|
|
|
return self.runCmd(["shell", "rm", filename]).stdout.read()
|
|
|
|
|
|
|
|
# does a recursive delete of directory on the device: rm -Rf remoteDir
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: output of telnet, i.e. "removing file: /mnt/sdcard/tests/test.txt"
|
|
|
|
# failure: None
|
|
|
|
def removeSingleDir(self, remoteDir):
|
|
|
|
return self.runCmd(["shell", "rmdir", remoteDir]).stdout.read()
|
|
|
|
|
|
|
|
# does a recursive delete of directory on the device: rm -Rf remoteDir
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: output of telnet, i.e. "removing file: /mnt/sdcard/tests/test.txt"
|
|
|
|
# failure: None
|
|
|
|
def removeDir(self, remoteDir):
|
|
|
|
out = ""
|
|
|
|
if (self.isDir(remoteDir)):
|
|
|
|
files = self.listFiles(remoteDir.strip())
|
|
|
|
for f in files:
|
|
|
|
if (self.isDir(remoteDir.strip() + "/" + f.strip())):
|
|
|
|
out += self.removeDir(remoteDir.strip() + "/" + f.strip())
|
|
|
|
else:
|
2011-07-17 09:04:40 -07:00
|
|
|
out += self.removeFile(remoteDir.strip() + "/" + f.strip())
|
|
|
|
out += self.removeSingleDir(remoteDir.strip())
|
2011-05-06 15:17:55 -07:00
|
|
|
else:
|
|
|
|
out += self.removeFile(remoteDir.strip())
|
|
|
|
return out
|
|
|
|
|
|
|
|
def isDir(self, remotePath):
|
2011-07-17 09:04:40 -07:00
|
|
|
p = self.runCmd(["shell", "ls", "-a", remotePath])
|
2011-05-06 15:17:55 -07:00
|
|
|
data = p.stdout.readlines()
|
|
|
|
if (len(data) == 0):
|
|
|
|
return True
|
|
|
|
if (len(data) == 1):
|
2011-07-17 09:04:40 -07:00
|
|
|
if (data[0].rstrip() == remotePath):
|
2011-05-06 15:17:55 -07:00
|
|
|
return False
|
|
|
|
if (data[0].find("No such file or directory") != -1):
|
|
|
|
return False
|
|
|
|
if (data[0].find("Not a directory") != -1):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def listFiles(self, rootdir):
|
2011-07-17 09:04:40 -07:00
|
|
|
p = self.runCmd(["shell", "ls", "-a", rootdir])
|
2011-05-06 15:17:55 -07:00
|
|
|
data = p.stdout.readlines()
|
2012-01-30 06:57:08 -08:00
|
|
|
data[:] = [item.rstrip('\r\n') for item in data]
|
2011-05-06 15:17:55 -07:00
|
|
|
if (len(data) == 1):
|
|
|
|
if (data[0] == rootdir):
|
|
|
|
return []
|
|
|
|
if (data[0].find("No such file or directory") != -1):
|
|
|
|
return []
|
|
|
|
if (data[0].find("Not a directory") != -1):
|
|
|
|
return []
|
2012-04-20 14:15:59 -07:00
|
|
|
if (data[0].find("Permission denied") != -1):
|
|
|
|
return []
|
|
|
|
if (data[0].find("opendir failed") != -1):
|
|
|
|
return []
|
2011-05-06 15:17:55 -07:00
|
|
|
return data
|
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: array of process tuples
|
|
|
|
# failure: []
|
|
|
|
def getProcessList(self):
|
|
|
|
p = self.runCmd(["shell", "ps"])
|
|
|
|
# first line is the headers
|
|
|
|
p.stdout.readline()
|
|
|
|
proc = p.stdout.readline()
|
|
|
|
ret = []
|
|
|
|
while (proc):
|
|
|
|
els = proc.split()
|
|
|
|
ret.append(list([els[1], els[len(els) - 1], els[0]]))
|
|
|
|
proc = p.stdout.readline()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# external function
|
2012-02-23 07:32:28 -08:00
|
|
|
# DEPRECATED: Use shell() or launchApplication() for new code
|
2011-05-06 15:17:55 -07:00
|
|
|
# returns:
|
|
|
|
# success: pid
|
|
|
|
# failure: None
|
|
|
|
def fireProcess(self, appname, failIfRunning=False):
|
2011-07-17 09:04:40 -07:00
|
|
|
#strip out env vars
|
|
|
|
parts = appname.split('"');
|
|
|
|
if (len(parts) > 2):
|
|
|
|
parts = parts[2:]
|
|
|
|
return self.launchProcess(parts, failIfRunning)
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
# external function
|
2012-02-23 07:32:28 -08:00
|
|
|
# DEPRECATED: Use shell() or launchApplication() for new code
|
2011-05-06 15:17:55 -07:00
|
|
|
# returns:
|
|
|
|
# success: output filename
|
|
|
|
# failure: None
|
|
|
|
def launchProcess(self, cmd, outputFile = "process.txt", cwd = '', env = '', failIfRunning=False):
|
2012-01-07 15:41:08 -08:00
|
|
|
if cmd[0] == "am":
|
|
|
|
self.checkCmd(["shell"] + cmd)
|
|
|
|
return outputFile
|
|
|
|
|
2012-02-03 15:54:02 -08:00
|
|
|
acmd = ["shell", "am", "start", "-W"]
|
2011-07-17 09:04:40 -07:00
|
|
|
cmd = ' '.join(cmd).strip()
|
2011-05-06 15:17:55 -07:00
|
|
|
i = cmd.find(" ")
|
2011-12-05 04:19:31 -08:00
|
|
|
# SUT identifies the URL by looking for :\\ -- another strategy to consider
|
|
|
|
re_url = re.compile('^[http|file|chrome|about].*')
|
|
|
|
last = cmd.rfind(" ")
|
|
|
|
uri = ""
|
|
|
|
args = ""
|
|
|
|
if re_url.match(cmd[last:].strip()):
|
|
|
|
args = cmd[i:last].strip()
|
|
|
|
uri = cmd[last:].strip()
|
|
|
|
else:
|
|
|
|
args = cmd[i:].strip()
|
2011-05-06 15:17:55 -07:00
|
|
|
acmd.append("-n")
|
|
|
|
acmd.append(cmd[0:i] + "/.App")
|
2011-12-05 04:19:31 -08:00
|
|
|
if args != "":
|
2012-02-03 15:54:02 -08:00
|
|
|
acmd.append("--es")
|
2011-12-05 04:19:31 -08:00
|
|
|
acmd.append("args")
|
|
|
|
acmd.append(args)
|
2012-02-03 15:54:02 -08:00
|
|
|
if env != '' and env != None:
|
|
|
|
envCnt = 0
|
|
|
|
# env is expected to be a dict of environment variables
|
|
|
|
for envkey, envval in env.iteritems():
|
|
|
|
acmd.append("--es")
|
|
|
|
acmd.append("env" + str(envCnt))
|
|
|
|
acmd.append(envkey + "=" + envval);
|
|
|
|
envCnt += 1
|
2011-12-05 04:19:31 -08:00
|
|
|
if uri != "":
|
|
|
|
acmd.append("-d")
|
|
|
|
acmd.append(''.join(['\'',uri, '\'']));
|
2011-05-06 15:17:55 -07:00
|
|
|
print acmd
|
|
|
|
self.checkCmd(acmd)
|
2012-01-07 15:41:08 -08:00
|
|
|
return outputFile
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
2012-03-23 14:00:04 -07:00
|
|
|
# success: True
|
|
|
|
# failure: False
|
|
|
|
def killProcess(self, appname, forceKill=False):
|
2011-05-06 15:17:55 -07:00
|
|
|
procs = self.getProcessList()
|
2012-03-23 14:00:04 -07:00
|
|
|
didKillProcess = False
|
2011-11-09 09:08:04 -08:00
|
|
|
for (pid, name, user) in procs:
|
|
|
|
if name == appname:
|
2012-03-23 14:00:04 -07:00
|
|
|
args = ["shell", "kill"]
|
|
|
|
if forceKill:
|
|
|
|
args.append("-9")
|
|
|
|
args.append(pid)
|
|
|
|
p = self.runCmdAs(args)
|
|
|
|
didKillProcess = True
|
|
|
|
|
|
|
|
return didKillProcess
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: filecontents
|
|
|
|
# failure: None
|
|
|
|
def catFile(self, remoteFile):
|
|
|
|
#p = self.runCmd(["shell", "cat", remoteFile])
|
|
|
|
#return p.stdout.read()
|
|
|
|
return self.getFile(remoteFile)
|
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: output of pullfile, string
|
|
|
|
# failure: None
|
|
|
|
def pullFile(self, remoteFile):
|
|
|
|
#return self.catFile(remoteFile)
|
|
|
|
return self.getFile(remoteFile)
|
|
|
|
|
|
|
|
# copy file from device (remoteFile) to host (localFile)
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: output of pullfile, string
|
|
|
|
# failure: None
|
|
|
|
def getFile(self, remoteFile, localFile = 'tmpfile_dm_adb'):
|
2011-10-11 07:31:05 -07:00
|
|
|
# TODO: add debug flags and allow for printing stdout
|
|
|
|
# self.runCmd(["pull", remoteFile, localFile])
|
2011-05-06 15:17:55 -07:00
|
|
|
try:
|
2012-02-01 08:43:15 -08:00
|
|
|
|
|
|
|
# First attempt to pull file regularly
|
|
|
|
outerr = self.runCmd(["pull", remoteFile, localFile]).communicate()
|
|
|
|
|
|
|
|
# Now check stderr for errors
|
2012-03-22 07:45:20 -07:00
|
|
|
if outerr[1]:
|
|
|
|
errl = outerr[1].splitlines()
|
|
|
|
if (len(errl) == 1):
|
|
|
|
if (((errl[0].find("Permission denied") != -1)
|
|
|
|
or (errl[0].find("does not exist") != -1))
|
|
|
|
and self.useRunAs):
|
|
|
|
# If we lack permissions to read but have run-as, then we should try
|
|
|
|
# to copy the file to a world-readable location first before attempting
|
|
|
|
# to pull it again.
|
|
|
|
remoteTmpFile = self.getTempDir() + "/" + os.path.basename(remoteFile)
|
|
|
|
self.checkCmdAs(["shell", "dd", "if=" + remoteFile, "of=" + remoteTmpFile])
|
|
|
|
self.checkCmdAs(["shell", "chmod", "777", remoteTmpFile])
|
|
|
|
self.runCmd(["pull", remoteTmpFile, localFile]).stdout.read()
|
|
|
|
# Clean up temporary file
|
|
|
|
self.checkCmdAs(["shell", "rm", remoteTmpFile])
|
2012-02-01 08:43:15 -08:00
|
|
|
|
2011-05-06 15:17:55 -07:00
|
|
|
f = open(localFile)
|
|
|
|
ret = f.read()
|
|
|
|
f.close()
|
2012-03-22 07:45:20 -07:00
|
|
|
return ret
|
2011-05-06 15:17:55 -07:00
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# copy directory structure from device (remoteDir) to host (localDir)
|
|
|
|
# external function
|
|
|
|
# checkDir exists so that we don't create local directories if the
|
|
|
|
# remote directory doesn't exist but also so that we don't call isDir
|
|
|
|
# twice when recursing.
|
|
|
|
# returns:
|
|
|
|
# success: list of files, string
|
|
|
|
# failure: None
|
|
|
|
def getDirectory(self, remoteDir, localDir, checkDir=True):
|
|
|
|
ret = []
|
|
|
|
p = self.runCmd(["pull", remoteDir, localDir])
|
2012-02-29 16:40:58 -08:00
|
|
|
p.stdout.readline()
|
|
|
|
line = p.stdout.readline()
|
2011-05-06 15:17:55 -07:00
|
|
|
while (line):
|
|
|
|
els = line.split()
|
|
|
|
f = els[len(els) - 1]
|
|
|
|
i = f.find(localDir)
|
|
|
|
if (i != -1):
|
|
|
|
if (localDir[len(localDir) - 1] != '/'):
|
|
|
|
i = i + 1
|
|
|
|
f = f[i + len(localDir):]
|
|
|
|
i = f.find("/")
|
|
|
|
if (i > 0):
|
|
|
|
f = f[0:i]
|
|
|
|
ret.append(f)
|
2012-02-29 16:40:58 -08:00
|
|
|
line = p.stdout.readline()
|
2011-05-06 15:17:55 -07:00
|
|
|
#the last line is a summary
|
2011-10-14 04:52:01 -07:00
|
|
|
if (len(ret) > 0):
|
|
|
|
ret.pop()
|
2011-05-06 15:17:55 -07:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# true/false check if the two files have the same md5 sum
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: True
|
|
|
|
# failure: False
|
|
|
|
def validateFile(self, remoteFile, localFile):
|
|
|
|
return self.getRemoteHash(remoteFile) == self.getLocalHash(localFile)
|
|
|
|
|
|
|
|
# return the md5 sum of a remote file
|
|
|
|
# internal function
|
|
|
|
# returns:
|
|
|
|
# success: MD5 hash for given filename
|
|
|
|
# failure: None
|
|
|
|
def getRemoteHash(self, filename):
|
|
|
|
data = p = self.runCmd(["shell", "ls", "-l", filename]).stdout.read()
|
|
|
|
return data.split()[3]
|
|
|
|
|
|
|
|
def getLocalHash(self, filename):
|
|
|
|
data = p = subprocess.Popen(["ls", "-l", filename], stdout=subprocess.PIPE).stdout.read()
|
|
|
|
return data.split()[4]
|
|
|
|
|
2012-05-08 07:23:19 -07:00
|
|
|
# Internal method to setup the device root and cache its value
|
|
|
|
def setupDeviceRoot(self):
|
|
|
|
# /mnt/sdcard/tests is preferred to /data/local/tests, but this can be
|
|
|
|
# over-ridden by creating /data/local/tests
|
|
|
|
testRoot = "/data/local/tests"
|
|
|
|
if (self.dirExists(testRoot)):
|
|
|
|
self.deviceRoot = testRoot
|
|
|
|
return
|
|
|
|
|
|
|
|
for (basePath, subPath) in [('/mnt/sdcard', 'tests'),
|
|
|
|
('/data/local', 'tests')]:
|
|
|
|
if self.dirExists(basePath):
|
|
|
|
testRoot = os.path.join(basePath, subPath)
|
|
|
|
if self.mkDir(testRoot):
|
|
|
|
self.deviceRoot = testRoot
|
|
|
|
return
|
|
|
|
|
|
|
|
raise DMError("Unable to set up device root as /mnt/sdcard/tests "
|
|
|
|
"or /data/local/tests")
|
|
|
|
|
2011-05-06 15:17:55 -07:00
|
|
|
# Gets the device root for the testing area on the device
|
|
|
|
# For all devices we will use / type slashes and depend on the device-agent
|
|
|
|
# to sort those out. The agent will return us the device location where we
|
|
|
|
# should store things, we will then create our /tests structure relative to
|
|
|
|
# that returned path.
|
|
|
|
# Structure on the device is as follows:
|
|
|
|
# /tests
|
|
|
|
# /<fennec>|<firefox> --> approot
|
|
|
|
# /profile
|
|
|
|
# /xpcshell
|
|
|
|
# /reftest
|
|
|
|
# /mochitest
|
|
|
|
#
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: path for device root
|
|
|
|
# failure: None
|
|
|
|
def getDeviceRoot(self):
|
2012-05-08 07:23:19 -07:00
|
|
|
return self.deviceRoot
|
2011-05-06 15:17:55 -07:00
|
|
|
|
2012-02-01 08:43:15 -08:00
|
|
|
# Gets the temporary directory we are using on this device
|
|
|
|
# base on our device root, ensuring also that it exists.
|
|
|
|
#
|
|
|
|
# internal function
|
|
|
|
# returns:
|
|
|
|
# success: path for temporary directory
|
|
|
|
# failure: None
|
|
|
|
def getTempDir(self):
|
|
|
|
# Cache result to speed up operations depending
|
|
|
|
# on the temporary directory.
|
|
|
|
if self.tempDir == None:
|
|
|
|
self.tempDir = self.getDeviceRoot() + "/tmp"
|
|
|
|
if (not self.dirExists(self.tempDir)):
|
|
|
|
return self.mkDir(self.tempDir)
|
|
|
|
|
|
|
|
return self.tempDir
|
|
|
|
|
2011-05-06 15:17:55 -07:00
|
|
|
# Either we will have /tests/fennec or /tests/firefox but we will never have
|
|
|
|
# both. Return the one that exists
|
|
|
|
# TODO: ensure we can support org.mozilla.firefox
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: path for app root
|
|
|
|
# failure: None
|
2012-01-07 15:41:08 -08:00
|
|
|
def getAppRoot(self, packageName):
|
2011-05-06 15:17:55 -07:00
|
|
|
devroot = self.getDeviceRoot()
|
|
|
|
if (devroot == None):
|
|
|
|
return None
|
|
|
|
|
2012-01-07 15:41:08 -08:00
|
|
|
if (packageName and self.dirExists('/data/data/' + packageName)):
|
|
|
|
self.packageName = packageName
|
|
|
|
return '/data/data/' + packageName
|
2011-08-22 01:00:50 -07:00
|
|
|
elif (self.packageName and self.dirExists('/data/data/' + self.packageName)):
|
|
|
|
return '/data/data/' + self.packageName
|
2011-05-06 15:17:55 -07:00
|
|
|
|
|
|
|
# Failure (either not installed or not a recognized platform)
|
2011-08-22 01:00:50 -07:00
|
|
|
print "devicemanagerADB: getAppRoot failed"
|
2011-05-06 15:17:55 -07:00
|
|
|
return None
|
|
|
|
|
|
|
|
# Gets the directory location on the device for a specific test type
|
|
|
|
# Type is one of: xpcshell|reftest|mochitest
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: path for test root
|
|
|
|
# failure: None
|
|
|
|
def getTestRoot(self, type):
|
|
|
|
devroot = self.getDeviceRoot()
|
|
|
|
if (devroot == None):
|
|
|
|
return None
|
|
|
|
|
|
|
|
if (re.search('xpcshell', type, re.I)):
|
|
|
|
self.testRoot = devroot + '/xpcshell'
|
|
|
|
elif (re.search('?(i)reftest', type)):
|
|
|
|
self.testRoot = devroot + '/reftest'
|
|
|
|
elif (re.search('?(i)mochitest', type)):
|
|
|
|
self.testRoot = devroot + '/mochitest'
|
|
|
|
return self.testRoot
|
|
|
|
|
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: status from test agent
|
|
|
|
# failure: None
|
|
|
|
def reboot(self, wait = False):
|
|
|
|
ret = self.runCmd(["reboot"]).stdout.read()
|
|
|
|
if (not wait):
|
|
|
|
return "Success"
|
|
|
|
countdown = 40
|
|
|
|
while (countdown > 0):
|
|
|
|
countdown
|
|
|
|
try:
|
|
|
|
self.checkCmd(["wait-for-device", "shell", "ls", "/sbin"])
|
|
|
|
return ret
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
self.checkCmd(["root"])
|
|
|
|
except:
|
|
|
|
time.sleep(1)
|
|
|
|
print "couldn't get root"
|
|
|
|
return "Success"
|
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: text status from command or callback server
|
|
|
|
# failure: None
|
|
|
|
def updateApp(self, appBundlePath, processName=None, destPath=None, ipAddr=None, port=30000):
|
|
|
|
return self.runCmd(["install", "-r", appBundlePath]).stdout.read()
|
2011-09-30 11:54:11 -07:00
|
|
|
|
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: time in ms
|
|
|
|
# failure: None
|
|
|
|
def getCurrentTime(self):
|
|
|
|
timestr = self.runCmd(["shell", "date", "+%s"]).stdout.read().strip()
|
|
|
|
if (not timestr or not timestr.isdigit()):
|
|
|
|
return None
|
|
|
|
return str(int(timestr)*1000)
|
|
|
|
|
2011-05-06 15:17:55 -07:00
|
|
|
# Returns information about the device:
|
|
|
|
# Directive indicates the information you want to get, your choices are:
|
|
|
|
# os - name of the os
|
|
|
|
# id - unique id of the device
|
|
|
|
# uptime - uptime of the device
|
|
|
|
# systime - system time of the device
|
|
|
|
# screen - screen resolution
|
|
|
|
# memory - memory stats
|
|
|
|
# process - list of running processes (same as ps)
|
|
|
|
# disk - total, free, available bytes on disk
|
|
|
|
# power - power status (charge, battery temp)
|
|
|
|
# all - all of them - or call it with no parameters to get all the information
|
|
|
|
# returns:
|
|
|
|
# success: dict of info strings by directive name
|
|
|
|
# failure: {}
|
|
|
|
def getInfo(self, directive="all"):
|
|
|
|
ret = {}
|
|
|
|
if (directive == "id" or directive == "all"):
|
|
|
|
ret["id"] = self.runCmd(["get-serialno"]).stdout.read()
|
|
|
|
if (directive == "os" or directive == "all"):
|
|
|
|
ret["os"] = self.runCmd(["shell", "getprop", "ro.build.display.id"]).stdout.read()
|
|
|
|
if (directive == "uptime" or directive == "all"):
|
|
|
|
utime = self.runCmd(["shell", "uptime"]).stdout.read()
|
|
|
|
if (not utime):
|
|
|
|
raise DMError("error getting uptime")
|
|
|
|
utime = utime[9:]
|
|
|
|
hours = utime[0:utime.find(":")]
|
|
|
|
utime = utime[utime[1:].find(":") + 2:]
|
|
|
|
minutes = utime[0:utime.find(":")]
|
|
|
|
utime = utime[utime[1:].find(":") + 2:]
|
|
|
|
seconds = utime[0:utime.find(",")]
|
|
|
|
ret["uptime"] = ["0 days " + hours + " hours " + minutes + " minutes " + seconds + " seconds"]
|
|
|
|
if (directive == "process" or directive == "all"):
|
|
|
|
ret["process"] = self.runCmd(["shell", "ps"]).stdout.read()
|
|
|
|
if (directive == "systime" or directive == "all"):
|
|
|
|
ret["systime"] = self.runCmd(["shell", "date"]).stdout.read()
|
|
|
|
print ret
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def runCmd(self, args):
|
2012-02-01 08:43:15 -08:00
|
|
|
# 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
|
2012-04-05 11:58:12 -07:00
|
|
|
finalArgs = [self.adbPath]
|
|
|
|
if self.deviceSerial:
|
|
|
|
finalArgs.extend(['-s', self.deviceSerial])
|
2012-02-01 08:43:15 -08:00
|
|
|
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)
|
2012-04-05 11:58:12 -07:00
|
|
|
finalArgs.extend(args)
|
|
|
|
return subprocess.Popen(finalArgs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2011-05-06 15:17:55 -07:00
|
|
|
|
2011-11-09 09:08:04 -08:00
|
|
|
def runCmdAs(self, args):
|
|
|
|
if self.useRunAs:
|
|
|
|
args.insert(1, "run-as")
|
|
|
|
args.insert(2, self.packageName)
|
|
|
|
return self.runCmd(args)
|
|
|
|
|
2011-05-06 15:17:55 -07:00
|
|
|
def checkCmd(self, args):
|
2012-02-01 08:43:15 -08:00
|
|
|
# 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
|
2012-04-05 11:58:12 -07:00
|
|
|
finalArgs = [self.adbPath]
|
|
|
|
if self.deviceSerial:
|
|
|
|
finalArgs.extend(['-s', self.deviceSerial])
|
2012-02-01 08:43:15 -08:00
|
|
|
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)
|
2012-04-05 11:58:12 -07:00
|
|
|
finalArgs.extend(args)
|
|
|
|
return subprocess.check_call(finalArgs)
|
2011-05-06 15:17:55 -07:00
|
|
|
|
2011-07-17 09:04:40 -07:00
|
|
|
def checkCmdAs(self, args):
|
2011-08-23 09:45:10 -07:00
|
|
|
if (self.useRunAs):
|
2011-07-17 09:04:40 -07:00
|
|
|
args.insert(1, "run-as")
|
|
|
|
args.insert(2, self.packageName)
|
|
|
|
return self.checkCmd(args)
|
|
|
|
|
2012-06-19 09:14:41 -07:00
|
|
|
# external function
|
|
|
|
# returns:
|
|
|
|
# success: True
|
|
|
|
# failure: False
|
2011-05-06 15:17:55 -07:00
|
|
|
def chmodDir(self, remoteDir):
|
|
|
|
if (self.isDir(remoteDir)):
|
|
|
|
files = self.listFiles(remoteDir.strip())
|
|
|
|
for f in files:
|
2012-06-19 09:14:41 -07:00
|
|
|
remoteEntry = remoteDir.strip() + "/" + f.strip()
|
|
|
|
if (self.isDir(remoteEntry)):
|
|
|
|
self.chmodDir(remoteEntry)
|
2011-05-06 15:17:55 -07:00
|
|
|
else:
|
2012-06-19 09:14:41 -07:00
|
|
|
self.checkCmdAs(["shell", "chmod", "777", remoteEntry])
|
|
|
|
print "chmod " + remoteEntry
|
2011-07-17 09:04:40 -07:00
|
|
|
self.checkCmdAs(["shell", "chmod", "777", remoteDir])
|
2011-05-06 15:17:55 -07:00
|
|
|
print "chmod " + remoteDir
|
|
|
|
else:
|
2011-07-17 09:04:40 -07:00
|
|
|
self.checkCmdAs(["shell", "chmod", "777", remoteDir.strip()])
|
|
|
|
print "chmod " + remoteDir.strip()
|
2012-06-19 09:14:41 -07:00
|
|
|
return True
|
2011-07-17 09:04:40 -07:00
|
|
|
|
2011-08-23 09:45:10 -07:00
|
|
|
def verifyADB(self):
|
|
|
|
# Check to see if adb itself can be executed.
|
2012-04-04 10:27:59 -07:00
|
|
|
if self.adbPath != 'adb':
|
|
|
|
if not os.access(self.adbPath, os.X_OK):
|
|
|
|
raise DMError("invalid adb path, or adb not executable: %s", self.adbPath)
|
|
|
|
|
2011-08-23 09:45:10 -07:00
|
|
|
try:
|
2012-03-16 15:36:28 -07:00
|
|
|
self.checkCmd(["version"])
|
|
|
|
except os.error, err:
|
|
|
|
raise DMError("unable to execute ADB (%s): ensure Android SDK is installed and adb is in your $PATH" % err)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise DMError("unable to execute ADB: ensure Android SDK is installed and adb is in your $PATH")
|
|
|
|
|
|
|
|
def verifyDevice(self):
|
2012-04-05 11:58:12 -07:00
|
|
|
# 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))
|
|
|
|
|
2012-03-16 15:36:28 -07:00
|
|
|
# Check to see if we can connect to device and run a simple command
|
|
|
|
try:
|
|
|
|
self.checkCmd(["shell", "echo"])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise DMError("unable to connect to device: is it plugged in?")
|
|
|
|
|
|
|
|
def verifyRoot(self):
|
|
|
|
# a test to see if we have root privs
|
|
|
|
files = self.listFiles("/data/data")
|
2012-04-20 14:15:59 -07:00
|
|
|
if (len(files) == 0):
|
|
|
|
print "NOT running as root"
|
|
|
|
raise DMError("not running as root")
|
2012-03-16 15:36:28 -07:00
|
|
|
|
|
|
|
self.haveRoot = True
|
|
|
|
|
2011-08-23 09:45:10 -07:00
|
|
|
def isCpAvailable(self):
|
|
|
|
# Some Android systems may not have a cp command installed,
|
2012-03-16 15:36:28 -07:00
|
|
|
# or it may not be executable by the user.
|
2011-08-23 09:45:10 -07:00
|
|
|
data = self.runCmd(["shell", "cp"]).stdout.read()
|
|
|
|
if (re.search('Usage', data)):
|
|
|
|
return True
|
|
|
|
else:
|
2012-02-01 08:43:15 -08:00
|
|
|
data = self.runCmd(["shell", "dd", "-"]).stdout.read()
|
|
|
|
if (re.search('unknown operand', data)):
|
|
|
|
print "'cp' not found, but 'dd' was found as a replacement"
|
|
|
|
self.useDDCopy = True
|
|
|
|
return True
|
2011-08-23 09:45:10 -07:00
|
|
|
print "unable to execute 'cp' on device; consider installing busybox from Android Market"
|
|
|
|
return False
|
|
|
|
|
2012-03-16 15:36:28 -07:00
|
|
|
def verifyRunAs(self):
|
2011-08-23 09:45:10 -07:00
|
|
|
# If a valid package name is available, and certain other
|
|
|
|
# conditions are met, devicemanagerADB can execute file operations
|
|
|
|
# via the "run-as" command, so that pushed files and directories
|
|
|
|
# are created by the uid associated with the package, more closely
|
|
|
|
# echoing conditions encountered by Fennec at run time.
|
|
|
|
# Check to see if run-as can be used here, by verifying a
|
|
|
|
# file copy via run-as.
|
|
|
|
self.useRunAs = False
|
|
|
|
devroot = self.getDeviceRoot()
|
2012-03-16 15:36:28 -07:00
|
|
|
if (self.packageName and self.isCpAvailable() and devroot):
|
2012-02-01 08:43:15 -08:00
|
|
|
tmpDir = self.getTempDir()
|
|
|
|
|
2012-02-21 08:13:54 -08:00
|
|
|
# The problem here is that run-as doesn't cause a non-zero exit code
|
|
|
|
# when failing because of a non-existent or non-debuggable package :(
|
2012-03-16 15:36:28 -07:00
|
|
|
runAsOut = self.runCmd(["shell", "run-as", self.packageName, "mkdir", devroot + "/sanity"]).communicate()[0]
|
|
|
|
if runAsOut.startswith("run-as:") and ("not debuggable" in runAsOut or
|
|
|
|
"is unknown" in runAsOut):
|
2012-02-21 08:13:54 -08:00
|
|
|
raise DMError("run-as failed sanity check")
|
|
|
|
|
2012-03-23 18:50:35 -07:00
|
|
|
tmpfile = tempfile.NamedTemporaryFile()
|
|
|
|
self.checkCmd(["push", tmpfile.name, tmpDir + "/tmpfile"])
|
2012-02-01 08:43:15 -08:00
|
|
|
if self.useDDCopy:
|
2012-03-16 15:36:28 -07:00
|
|
|
self.checkCmd(["shell", "run-as", self.packageName, "dd", "if=" + tmpDir + "/tmpfile", "of=" + devroot + "/sanity/tmpfile"])
|
2012-02-01 08:43:15 -08:00
|
|
|
else:
|
2012-03-16 15:36:28 -07:00
|
|
|
self.checkCmd(["shell", "run-as", self.packageName, "cp", tmpDir + "/tmpfile", devroot + "/sanity"])
|
2011-08-23 09:45:10 -07:00
|
|
|
if (self.fileExists(devroot + "/sanity/tmpfile")):
|
2012-03-16 15:36:28 -07:00
|
|
|
print "will execute commands via run-as " + self.packageName
|
2011-08-23 09:45:10 -07:00
|
|
|
self.useRunAs = True
|
|
|
|
self.checkCmd(["shell", "rm", devroot + "/tmp/tmpfile"])
|
2012-03-16 15:36:28 -07:00
|
|
|
self.checkCmd(["shell", "run-as", self.packageName, "rm", "-r", devroot + "/sanity"])
|
|
|
|
|
2011-12-05 04:19:31 -08:00
|
|
|
def isUnzipAvailable(self):
|
2012-01-05 07:20:21 -08:00
|
|
|
data = self.runCmdAs(["shell", "unzip"]).stdout.read()
|
2011-12-05 04:19:31 -08:00
|
|
|
if (re.search('Usage', data)):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def isLocalZipAvailable(self):
|
|
|
|
try:
|
|
|
|
subprocess.check_call(["zip", "-?"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def verifyZip(self):
|
|
|
|
# If "zip" can be run locally, and "unzip" can be run remotely, then pushDir
|
|
|
|
# can use these to push just one file per directory -- a significant
|
|
|
|
# optimization for large directories.
|
|
|
|
self.useZip = False
|
|
|
|
if (self.isUnzipAvailable() and self.isLocalZipAvailable()):
|
|
|
|
print "will use zip to push directories"
|
|
|
|
self.useZip = True
|
2012-03-16 15:36:28 -07:00
|
|
|
else:
|
|
|
|
raise DMError("zip not available")
|