blob: 3bf61bc86b8a370dfae13b4309f270a7488374bd [file] [log] [blame]
csmartdalton0262b5c2016-09-19 12:04:56 -07001# Copyright 2016 Google Inc.
2#
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
csmartdaltone4fd0782016-11-09 08:41:23 -08006from __future__ import print_function
csmartdaltond7a9db62016-09-22 05:10:02 -07007import re
Chris Dalton34d90552017-10-20 09:58:32 -06008import time
csmartdalton0262b5c2016-09-19 12:04:56 -07009import subprocess
csmartdalton310d72c2016-10-18 09:19:50 -070010import sys
csmartdalton0262b5c2016-09-19 12:04:56 -070011
csmartdaltond7a9db62016-09-22 05:10:02 -070012class Adb:
Kevin Lubickcccaef12017-10-13 08:15:09 -040013 def __init__(self, device_serial=None, adb_binary=None, echo=False):
14 self.__invocation = [adb_binary]
csmartdaltond7a9db62016-09-22 05:10:02 -070015 if device_serial:
16 self.__invocation.extend(['-s', device_serial])
csmartdaltona1151292016-11-09 15:37:48 -050017 self.__echo = echo
csmartdaltone4fd0782016-11-09 08:41:23 -080018 self.__is_root = None
csmartdalton0262b5c2016-09-19 12:04:56 -070019
csmartdaltond7a9db62016-09-22 05:10:02 -070020 def shell(self, cmd):
csmartdaltona1151292016-11-09 15:37:48 -050021 if self.__echo:
22 self.__echo_shell_cmd(cmd)
23 self.__invoke('shell', cmd)
csmartdaltond7a9db62016-09-22 05:10:02 -070024
25 def check(self, cmd):
csmartdaltona1151292016-11-09 15:37:48 -050026 if self.__echo:
27 self.__echo_shell_cmd(cmd)
csmartdaltond7a9db62016-09-22 05:10:02 -070028 result = subprocess.check_output(self.__invocation + ['shell', cmd])
csmartdaltona1151292016-11-09 15:37:48 -050029 if self.__echo:
30 print(result, file=sys.stderr)
csmartdaltone4fd0782016-11-09 08:41:23 -080031 return result
csmartdaltond7a9db62016-09-22 05:10:02 -070032
csmartdaltone4fd0782016-11-09 08:41:23 -080033 def root(self):
34 if not self.is_root():
csmartdaltona1151292016-11-09 15:37:48 -050035 self.__invoke('root')
36 self.__invoke('wait-for-device')
csmartdaltone4fd0782016-11-09 08:41:23 -080037 self.__is_root = None
38 return self.is_root()
csmartdaltond7a9db62016-09-22 05:10:02 -070039
40 def is_root(self):
csmartdaltone4fd0782016-11-09 08:41:23 -080041 if self.__is_root is None:
42 self.__is_root = ('root' == self.check('whoami').strip())
43 return self.__is_root
csmartdaltond7a9db62016-09-22 05:10:02 -070044
45 def remount(self):
csmartdaltona1151292016-11-09 15:37:48 -050046 self.__invoke('remount')
csmartdaltone4fd0782016-11-09 08:41:23 -080047
Chris Dalton34d90552017-10-20 09:58:32 -060048 def reboot(self):
49 self.__is_root = None
50 self.shell('reboot')
51 self.__invoke('wait-for-device')
52 while '1' != self.check('getprop sys.boot_completed').strip():
53 time.sleep(1)
54
csmartdaltona1151292016-11-09 15:37:48 -050055 def __echo_shell_cmd(self, cmd):
csmartdaltone4fd0782016-11-09 08:41:23 -080056 escaped = [re.sub(r'([^a-zA-Z0-9])', r'\\\1', x)
57 for x in cmd.strip().splitlines()]
csmartdaltona1151292016-11-09 15:37:48 -050058 self.__invoke('shell', 'echo', '$(whoami)@$(getprop ro.serialno)$',
59 " '\n>' ".join(escaped))
60
61 def __invoke(self, *args):
62 subprocess.call(self.__invocation + list(args), stdout=sys.stderr)