2
2
from devicemanager import DeviceManager, DMError
6
7
class DeviceManagerADB(DeviceManager):
8
def __init__(self, host = None, port = 20701, retrylimit = 5, packageName = "org.mozilla.fennec_unofficial"):
9
def __init__(self, host = None, port = 20701, retrylimit = 5, packageName = None):
11
12
self.retrylimit = retrylimit
16
self.packageName = None
17
if packageName == None:
19
packageName = 'org.mozilla.fennec_' + os.getenv('USER')
21
packageName = 'org.mozilla.fennec_'
14
22
self.Init(packageName)
16
24
def Init(self, packageName):
17
25
# Initialization code that may fail: Catch exceptions here to allow
18
26
# successful initialization even if, for example, adb is not installed.
20
root = self.getDeviceRoot()
21
self.verifyPackage(packageName)
22
self.tmpDir = root + "/tmp"
23
if (not self.dirExists(self.tmpDir)):
24
self.mkDir(self.tmpDir)
29
self.verifyRunAs(packageName)
26
32
self.packageName = None
29
34
# a test to see if we have root privs
30
self.checkCmd(["shell", "ls", "/sbin"])
35
files = self.listFiles("/data/data")
37
if (files[0].find("Permission denied") != -1):
38
print "NOT running as root"
39
raise Exception("not running as root")
33
42
self.checkCmd(["root"])
99
108
if (not self.dirExists(remoteDir)):
100
109
self.mkDirs(remoteDir+"/x")
101
for root, dirs, files in os.walk(localDir):
110
for root, dirs, files in os.walk(localDir, followlinks='true'):
102
111
relRoot = os.path.relpath(root, localDir)
103
112
for file in files:
104
113
localFile = os.path.join(root, file)
136
145
def fileExists(self, filepath):
137
self.checkCmd(["shell", "ls", filepath])
146
p = self.runCmd(["shell", "ls", "-a", filepath])
147
data = p.stdout.readlines()
149
if (data[0].rstrip() == filepath):
140
153
def removeFile(self, filename):
141
154
return self.runCmd(["shell", "rm", filename]).stdout.read()
381
394
return devroot + '/fennec'
382
395
elif (self.dirExists(devroot + '/firefox')):
383
396
return devroot + '/firefox'
384
elif (self.dirExists('/data/data/org.mozilla.fennec')):
385
return '/data/data/org.mozilla.fennec'
386
elif (self.dirExists('/data/data/org.mozilla.firefox')):
387
return '/data/data/org.mozilla.firefox'
388
elif (self.dirExists('/data/data/org.mozilla.fennec_unofficial')):
389
return '/data/data/org.mozilla.fennec_unofficial'
390
elif (self.dirExists('/data/data/org.mozilla.fennec_aurora')):
391
return '/data/data/org.mozilla.fennec_aurora'
392
elif (self.dirExists('/data/data/org.mozilla.firefox_beta')):
393
return '/data/data/org.mozilla.firefox_beta'
397
elif (self.packageName and self.dirExists('/data/data/' + self.packageName)):
398
return '/data/data/' + self.packageName
395
400
# Failure (either not installed or not a recognized platform)
401
print "devicemanagerADB: getAppRoot failed"
398
404
# Gets the directory location on the device for a specific test type
512
518
self.checkCmdAs(["shell", "chmod", "777", remoteDir.strip()])
513
519
print "chmod " + remoteDir.strip()
515
def verifyPackage(self, packageName):
516
# If a valid package name is specified, it will be used for certain
517
# file operations, so that pushed files and directories are created
518
# by the uid associated with the package.
519
self.packageName = None
521
data = self.runCmd(["shell", "run-as", packageName, "pwd"]).stdout.read()
522
if (not re.search('is unknown', data)):
522
# Check to see if adb itself can be executed.
524
self.runCmd(["version"])
525
except Exception as (ex):
526
print "unable to execute ADB: ensure Android SDK is installed and adb is in your $PATH"
528
def isCpAvailable(self):
529
# Some Android systems may not have a cp command installed,
530
# or it may not be executable by the user.
531
data = self.runCmd(["shell", "cp"]).stdout.read()
532
if (re.search('Usage', data)):
535
print "unable to execute 'cp' on device; consider installing busybox from Android Market"
538
def verifyRunAs(self, packageName):
539
# If a valid package name is available, and certain other
540
# conditions are met, devicemanagerADB can execute file operations
541
# via the "run-as" command, so that pushed files and directories
542
# are created by the uid associated with the package, more closely
543
# echoing conditions encountered by Fennec at run time.
544
# Check to see if run-as can be used here, by verifying a
545
# file copy via run-as.
546
self.useRunAs = False
547
devroot = self.getDeviceRoot()
548
if (packageName and self.isCpAvailable() and devroot):
549
self.tmpDir = devroot + "/tmp"
550
if (not self.dirExists(self.tmpDir)):
551
self.mkDir(self.tmpDir)
552
self.checkCmd(["shell", "run-as", packageName, "mkdir", devroot + "/sanity"])
553
self.checkCmd(["push", os.path.abspath(sys.argv[0]), self.tmpDir + "/tmpfile"])
554
self.checkCmd(["shell", "run-as", packageName, "cp", self.tmpDir + "/tmpfile", devroot + "/sanity"])
555
if (self.fileExists(devroot + "/sanity/tmpfile")):
556
print "will execute commands via run-as " + packageName
523
557
self.packageName = packageName
524
print "package set: " + self.packageName
559
self.checkCmd(["shell", "rm", devroot + "/tmp/tmpfile"])
560
self.checkCmd(["shell", "run-as", packageName, "rm", "-r", devroot + "/sanity"])