OLD | NEW |
1 # Copyright 2016 Google Inc. | 1 # Copyright 2016 Google Inc. |
2 # | 2 # |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
| 6 import re |
6 import subprocess | 7 import subprocess |
7 | 8 |
8 def shell(cmd, device_serial=None): | 9 class Adb: |
9 if device_serial is None: | 10 def __init__(self, device_serial=None): |
10 subprocess.call(['adb', 'shell', cmd]) | 11 self.__invocation = ['adb'] |
11 else: | 12 if device_serial: |
12 subprocess.call(['adb', '-s', device_serial, 'shell', cmd]) | 13 self.__invocation.extend(['-s', device_serial]) |
13 | 14 |
14 def check(cmd, device_serial=None): | 15 def shell(self, cmd): |
15 if device_serial is None: | 16 subprocess.call(self.__invocation + ['shell', cmd]) |
16 out = subprocess.check_output(['adb', 'shell', cmd]) | 17 |
17 else: | 18 def check(self, cmd): |
18 out = subprocess.check_output(['adb', '-s', device_serial, 'shell', cmd]) | 19 result = subprocess.check_output(self.__invocation + ['shell', cmd]) |
19 return out.rstrip() | 20 return result.rstrip() |
| 21 |
| 22 def check_lines(self, cmd): |
| 23 result = self.check(cmd) |
| 24 return re.split('[\r\n]+', result) |
| 25 |
| 26 def get_device_model(self): |
| 27 result = self.check('getprop | grep ro.product.model') |
| 28 result = re.match(r'\[ro.product.model\]:\s*\[(.*)\]', result) |
| 29 return result.group(1) if result else 'unknown_product' |
| 30 |
| 31 def is_root(self): |
| 32 return self.check('echo $USER') == 'root' |
| 33 |
| 34 def attempt_root(self): |
| 35 if self.is_root(): |
| 36 return True |
| 37 subprocess.call(self.__invocation + ['root']) |
| 38 return self.is_root() |
| 39 |
| 40 def remount(self): |
| 41 subprocess.call(self.__invocation + ['remount']) |
OLD | NEW |