forked from google/python-adb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
adb_commands.py
246 lines (214 loc) · 7.95 KB
/
adb_commands.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import io
import os
import socket
import posixpath
from adb import adb_protocol
from adb import common
from adb import filesync_protocol
try:
file_types = (file, io.IOBase)
except NameError:
file_types = (io.IOBase,)
CLASS = 0xFF
SUBCLASS = 0x42
PROTOCOL = 0x01
DeviceIsAvailable = common.InterfaceMatcher(CLASS, SUBCLASS, PROTOCOL)
class AdbCommands(object):
protocol_handler = adb_protocol.AdbMessage
filesync_handler = filesync_protocol.FilesyncProtocol
def __init__(self):
self.build_props = None
self._device_state = None
self._handle = None
self._service_connections = {}
def __reset(self):
self.__init__()
def _get_service_connection(
self, service, service_command=None, create=True, timeout_ms=None
):
connection = self._service_connections.get(service, None)
if connection:
return connection
if not connection and not create:
return None
if service_command:
destination_str = b"%s:%s" % (service, service_command)
else:
destination_str = service
connection = self.protocol_handler.Open(
self._handle, destination=destination_str, timeout_ms=timeout_ms
)
self._service_connections.update({service: connection})
return connection
def ConnectDevice(
self, port_path=None, serial=None, default_timeout_ms=None, **kwargs
):
if "handle" in kwargs:
self._handle = kwargs.pop("handle")
else:
if isinstance(serial, (bytes, bytearray)):
serial = serial.decode("utf-8")
if serial and ":" in serial:
self._handle = common.TcpHandle(serial, timeout_ms=default_timeout_ms)
else:
self._handle = common.UsbHandle.FindAndOpen(
DeviceIsAvailable,
port_path=port_path,
serial=serial,
timeout_ms=default_timeout_ms,
)
self._Connect(**kwargs)
return self
def Close(self):
for conn in list(self._service_connections.values()):
if conn:
try:
conn.Close()
except:
pass
if self._handle:
self._handle.Close()
self.__reset()
def _Connect(self, banner=None, **kwargs):
if not banner:
banner = socket.gethostname().encode()
conn_str = self.protocol_handler.Connect(self._handle, banner=banner, **kwargs)
parts = conn_str.split(b"::")
self._device_state = parts[0]
self.build_props = str(parts[1].split(b";"))
return True
@classmethod
def Devices(cls):
return common.UsbHandle.FindDevices(DeviceIsAvailable)
def GetState(self):
return self._device_state
def Install(
self,
apk_path,
destination_dir="",
replace_existing=True,
grant_permissions=False,
timeout_ms=None,
transfer_progress_callback=None,
):
if not destination_dir:
destination_dir = "/data/local/tmp/"
basename = os.path.basename(apk_path)
destination_path = posixpath.join(destination_dir, basename)
self.Push(
apk_path,
destination_path,
timeout_ms=timeout_ms,
progress_callback=transfer_progress_callback,
)
cmd = ["pm install"]
if grant_permissions:
cmd.append("-g")
if replace_existing:
cmd.append("-r")
cmd.append('"{}"'.format(destination_path))
ret = self.Shell(" ".join(cmd), timeout_ms=timeout_ms)
rm_cmd = ["rm", destination_path]
self.Shell(" ".join(rm_cmd), timeout_ms=timeout_ms)
return ret
def Uninstall(self, package_name, keep_data=False, timeout_ms=None):
cmd = ["pm uninstall"]
if keep_data:
cmd.append("-k")
cmd.append('"%s"' % package_name)
return self.Shell(" ".join(cmd), timeout_ms=timeout_ms)
def Push(
self,
source_file,
device_filename,
mtime="0",
timeout_ms=None,
progress_callback=None,
st_mode=None,
):
if isinstance(source_file, str):
if os.path.isdir(source_file):
self.Shell("mkdir " + device_filename)
for f in os.listdir(source_file):
self.Push(
os.path.join(source_file, f),
device_filename + "/" + f,
progress_callback=progress_callback,
)
return
source_file = open(source_file, "rb")
with source_file:
connection = self.protocol_handler.Open(
self._handle, destination=b"sync:", timeout_ms=timeout_ms
)
kwargs = {}
if st_mode is not None:
kwargs["st_mode"] = st_mode
self.filesync_handler.Push(
connection,
source_file,
device_filename,
mtime=int(mtime),
progress_callback=progress_callback,
**kwargs
)
connection.Close()
def Pull(
self, device_filename, dest_file=None, timeout_ms=None, progress_callback=None
):
if not dest_file:
dest_file = io.BytesIO()
elif isinstance(dest_file, str):
dest_file = open(dest_file, "wb")
elif isinstance(dest_file, file_types):
pass
else:
raise ValueError("dest_file is of unknown type")
conn = self.protocol_handler.Open(
self._handle, destination=b"sync:", timeout_ms=timeout_ms
)
self.filesync_handler.Pull(conn, device_filename, dest_file, progress_callback)
conn.Close()
if isinstance(dest_file, io.BytesIO):
return dest_file.getvalue()
dest_file.close()
if hasattr(dest_file, "name"):
return os.path.exists(dest_file.name)
return True
def Stat(self, device_filename):
connection = self.protocol_handler.Open(self._handle, destination=b"sync:")
mode, size, mtime = self.filesync_handler.Stat(connection, device_filename)
connection.Close()
return mode, size, mtime
def List(self, device_path):
connection = self.protocol_handler.Open(self._handle, destination=b"sync:")
listing = self.filesync_handler.List(connection, device_path)
connection.Close()
return listing
def Reboot(self, destination=b""):
self.protocol_handler.Open(self._handle, b"reboot:%s" % destination)
def RebootBootloader(self):
self.Reboot(b"bootloader")
def Remount(self):
return self.protocol_handler.Command(self._handle, service=b"remount")
def Root(self):
return self.protocol_handler.Command(self._handle, service=b"root")
def EnableVerity(self):
return self.protocol_handler.Command(self._handle, service=b"enable-verity")
def DisableVerity(self):
return self.protocol_handler.Command(self._handle, service=b"disable-verity")
def Shell(self, command, timeout_ms=None):
return self.protocol_handler.Command(
self._handle, service=b"shell", command=command, timeout_ms=timeout_ms
)
def StreamingShell(self, command, timeout_ms=None):
return self.protocol_handler.StreamingCommand(
self._handle, service=b"shell", command=command, timeout_ms=timeout_ms
)
def Logcat(self, options, timeout_ms=None):
return self.StreamingShell("logcat %s" % options, timeout_ms)
def InteractiveShell(self, cmd=None, strip_cmd=True, delim=None, strip_delim=True):
conn = self._get_service_connection(b"shell:")
return self.protocol_handler.InteractiveShellCommand(
conn, cmd=cmd, strip_cmd=strip_cmd, delim=delim, strip_delim=strip_delim
)