2016-09-19 19:04:56 +00:00
|
|
|
# Copyright 2016 Google Inc.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2016-09-22 12:10:02 +00:00
|
|
|
import re
|
2016-09-19 19:04:56 +00:00
|
|
|
import subprocess
|
2016-10-18 16:19:50 +00:00
|
|
|
import sys
|
2016-09-19 19:04:56 +00:00
|
|
|
|
2016-09-22 12:10:02 +00:00
|
|
|
class Adb:
|
|
|
|
def __init__(self, device_serial=None):
|
|
|
|
self.__invocation = ['adb']
|
|
|
|
if device_serial:
|
|
|
|
self.__invocation.extend(['-s', device_serial])
|
|
|
|
|
|
|
|
def shell(self, cmd):
|
2016-10-18 16:19:50 +00:00
|
|
|
subprocess.call(self.__invocation + ['shell', cmd], stdout=sys.stderr)
|
2016-09-22 12:10:02 +00:00
|
|
|
|
|
|
|
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):
|
2016-10-18 16:19:50 +00:00
|
|
|
return self.check('whoami') == 'root'
|
2016-09-22 12:10:02 +00:00
|
|
|
|
|
|
|
def attempt_root(self):
|
|
|
|
if self.is_root():
|
|
|
|
return True
|
2016-10-18 16:19:50 +00:00
|
|
|
subprocess.call(self.__invocation + ['root'], stdout=sys.stderr)
|
2016-09-22 12:10:02 +00:00
|
|
|
return self.is_root()
|
|
|
|
|
|
|
|
def remount(self):
|
2016-10-18 16:19:50 +00:00
|
|
|
subprocess.call(self.__invocation + ['remount'], stdout=sys.stderr)
|