aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skpbench/_adb.py
blob: 402e132a2b9fd7afff02099a081347e1ea491975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Copyright 2016 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import re
import subprocess
import sys

class Adb:
  def __init__(self, device_serial=None):
    self.__invocation = ['adb']
    if device_serial:
      self.__invocation.extend(['-s', device_serial])

  def shell(self, cmd):
    subprocess.call(self.__invocation + ['shell', cmd], stdout=sys.stderr)

  def check(self, cmd):
    result = subprocess.check_output(self.__invocation + ['shell', cmd])
    return result.rstrip()

  def check_lines(self, cmd):
    result = self.check(cmd)
    return re.split('[\r\n]+', result)

  def get_device_model(self):
    result = self.check('getprop | grep ro.product.model')
    result = re.match(r'\[ro.product.model\]:\s*\[(.*)\]', result)
    return result.group(1) if result else 'unknown_product'

  def is_root(self):
    return self.check('whoami') == 'root'

  def attempt_root(self):
    if self.is_root():
      return True
    subprocess.call(self.__invocation + ['root'], stdout=sys.stderr)
    return self.is_root()

  def remount(self):
    subprocess.call(self.__invocation + ['remount'], stdout=sys.stderr)