-
Notifications
You must be signed in to change notification settings - Fork 35
/
adb.py
79 lines (48 loc) · 1.94 KB
/
adb.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
__author__ = 'Chariton Karamitas <[email protected]>'
import subprocess
import logger
class ADB(object):
def __init__(self, serial=None):
super(ADB, self).__init__()
self._logger = logger.get_logger(self.__class__.__name__)
self.serial = serial
def __str__(self):
return '<ADB serial=%s>' % self.serial
def _run(self, args, check=True):
if self.serial is not None:
args = ['adb', '-s', self.serial] + args
else:
args = ['adb'] + args
proc = subprocess.run(args, check=check, capture_output=True, text=True)
return proc.stdout.rstrip()
def shell(self, args, check=True):
return self._run(['shell'] + args, check=check)
def push(self, src_path, dst_path):
return self._run(['push', src_path, dst_path])
def pull(self, src_path, dst_path):
return self._run(['pull', src_path, dst_path])
def forward(self, protocol, host_endpoint, dev_endpoint):
return self._run(['forward', '%s:%s' % (protocol, host_endpoint),
'%s:%s' % (protocol, dev_endpoint)])
def reverse(self, protocol, dev_endpoint, host_endpoint):
return self._run(['reverse', '%s:%s' % (protocol, dev_endpoint),
'%s:%s' % (protocol, host_endpoint)])
class ADBAbstractions(ADB):
def __init__(self, serial=None):
super(ADBAbstractions, self).__init__(serial)
def __str__(self):
return '<ADBAbstractions serial=%s>' % self.serial
def su(self, args, check=True):
return self.shell(['su', '-c'] + args, check=check)
def is_rooted(self):
out = self.su(['id', '-u'])
try:
rooted = int(out) == 0
except ValueError:
rooted = False
return rooted
def getprop(self, name):
return self.shell(['getprop', name])
def setprop(self, name, value):
return self.shell(['setprop', name, value])