2017-10-06 04:00:19 +08:00
|
|
|
import os, sys, subprocess, tempfile
|
2014-02-14 17:22:10 +08:00
|
|
|
import time
|
|
|
|
|
|
|
|
ANDROID_TMPDIR = '/data/local/tmp/Output'
|
|
|
|
ADB = os.environ.get('ADB', 'adb')
|
|
|
|
|
|
|
|
verbose = False
|
|
|
|
if os.environ.get('ANDROID_RUN_VERBOSE') == '1':
|
|
|
|
verbose = True
|
|
|
|
|
2017-10-06 04:00:19 +08:00
|
|
|
def host_to_device_path(path):
|
|
|
|
rel = os.path.relpath(path, "/")
|
|
|
|
dev = os.path.join(ANDROID_TMPDIR, rel)
|
|
|
|
return dev
|
|
|
|
|
2016-03-01 09:16:03 +08:00
|
|
|
def adb(args, attempts = 1):
|
2014-02-14 17:22:10 +08:00
|
|
|
if verbose:
|
|
|
|
print args
|
2016-02-11 08:16:28 +08:00
|
|
|
tmpname = tempfile.mktemp()
|
|
|
|
out = open(tmpname, 'w')
|
2016-03-01 09:16:03 +08:00
|
|
|
ret = 255
|
|
|
|
while attempts > 0 and ret != 0:
|
|
|
|
attempts -= 1
|
|
|
|
ret = subprocess.call([ADB] + args, stdout=out, stderr=subprocess.STDOUT)
|
|
|
|
if attempts != 0:
|
|
|
|
ret = 5
|
2016-02-11 08:16:28 +08:00
|
|
|
if ret != 0:
|
2016-03-01 09:16:03 +08:00
|
|
|
print "adb command failed", args
|
|
|
|
print tmpname
|
|
|
|
out.close()
|
|
|
|
out = open(tmpname, 'r')
|
|
|
|
print out.read()
|
2016-02-11 08:16:28 +08:00
|
|
|
out.close()
|
|
|
|
os.unlink(tmpname)
|
|
|
|
return ret
|
2014-02-14 17:22:10 +08:00
|
|
|
|
|
|
|
def pull_from_device(path):
|
|
|
|
tmp = tempfile.mktemp()
|
2016-03-01 09:16:03 +08:00
|
|
|
adb(['pull', path, tmp], 5)
|
2014-02-14 17:22:10 +08:00
|
|
|
text = open(tmp, 'r').read()
|
|
|
|
os.unlink(tmp)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def push_to_device(path):
|
2017-10-06 04:00:19 +08:00
|
|
|
dst_path = host_to_device_path(path)
|
2017-07-01 12:23:47 +08:00
|
|
|
adb(['push', path, dst_path], 5)
|