1import os, sys, subprocess, tempfile 2import time 3 4ANDROID_TMPDIR = '/data/local/tmp/Output' 5ADB = os.environ.get('ADB', 'adb') 6 7verbose = False 8if os.environ.get('ANDROID_RUN_VERBOSE') == '1': 9 verbose = True 10 11def host_to_device_path(path): 12 rel = os.path.relpath(path, "/") 13 dev = os.path.join(ANDROID_TMPDIR, rel) 14 return dev 15 16def adb(args, attempts = 1, timeout_sec = 600): 17 if verbose: 18 print(args) 19 tmpname = tempfile.mktemp() 20 out = open(tmpname, 'w') 21 ret = 255 22 while attempts > 0 and ret != 0: 23 attempts -= 1 24 ret = subprocess.call(['timeout', str(timeout_sec), ADB] + args, stdout=out, stderr=subprocess.STDOUT) 25 if ret != 0: 26 print("adb command failed", args) 27 print(tmpname) 28 out.close() 29 out = open(tmpname, 'r') 30 print(out.read()) 31 out.close() 32 os.unlink(tmpname) 33 return ret 34 35def pull_from_device(path): 36 tmp = tempfile.mktemp() 37 adb(['pull', path, tmp], 5, 60) 38 text = open(tmp, 'r').read() 39 os.unlink(tmp) 40 return text 41 42def push_to_device(path): 43 dst_path = host_to_device_path(path) 44 adb(['push', path, dst_path], 5, 60) 45