1import os, 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 adb(args):
12    if verbose:
13        print args
14    devnull = open(os.devnull, 'w')
15    return subprocess.call([ADB] + args, stdout=devnull, stderr=subprocess.STDOUT)
16
17def pull_from_device(path):
18    tmp = tempfile.mktemp()
19    adb(['pull', path, tmp])
20    text = open(tmp, 'r').read()
21    os.unlink(tmp)
22    return text
23
24def push_to_device(path):
25    # Workaround for https://code.google.com/p/android/issues/detail?id=65857
26    dst_path = os.path.join(ANDROID_TMPDIR, os.path.basename(path))
27    tmp_path = dst_path + '.push'
28    adb(['push', path, tmp_path])
29    adb(['shell', 'cp "%s" "%s" 2>&1' % (tmp_path, dst_path)])
30