• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 attempts != 0:
26        ret = 5
27    if ret != 0:
28      print "adb command failed", args
29      print tmpname
30      out.close()
31      out = open(tmpname, 'r')
32      print out.read()
33    out.close()
34    os.unlink(tmpname)
35    return ret
36
37def pull_from_device(path):
38    tmp = tempfile.mktemp()
39    adb(['pull', path, tmp], 5, 60)
40    text = open(tmp, 'r').read()
41    os.unlink(tmp)
42    return text
43
44def push_to_device(path):
45    dst_path = host_to_device_path(path)
46    adb(['push', path, dst_path], 5, 60)
47