adb.filesync_protocol module¶
ADB protocol implementation.
Implements the ADB protocol as seen in android’s adb/adbd binaries, but only the host side.
Contents
-
adb.filesync_protocol.
DEFAULT_PUSH_MODE
= 33272¶ Default mode for pushed files.
-
class
adb.filesync_protocol.
DeviceFile
(filename, mode, size, mtime)¶ Bases:
tuple
-
_fields_defaults
= {}¶
-
property
filename
¶ Alias for field number 0
-
property
mode
¶ Alias for field number 1
-
property
mtime
¶ Alias for field number 3
-
property
size
¶ Alias for field number 2
-
-
class
adb.filesync_protocol.
FileSyncConnection
(adb_connection, recv_header_format)[source]¶ Bases:
object
Encapsulate a FileSync service connection.
- Parameters
adb_connection (adb.adb_protocol._AdbConnection) – ADB connection
recv_header_format (bytes) – TODO
-
adb
¶ ADB connection
-
send_buffer
¶ bytearray(adb_protocol.MAX_ADB_DATA)
(seeadb.adb_protocol.MAX_ADB_DATA
)- Type
byte_array
-
send_idx
¶ TODO
- Type
int
-
send_header_len
¶ struct.calcsize(b'<2I')
- Type
int
-
recv_buffer
¶ TODO
- Type
bytearray
-
recv_header_format
¶ TODO
- Type
bytes
-
recv_header_len
¶ struct.calcsize(recv_header_format)
- Type
int
-
Read
(expected_ids, read_data=True)[source]¶ Read ADB messages and return FileSync packets.
- Parameters
expected_ids (tuple[bytes]) – If the received header ID is not in
expected_ids
, an exception will be raisedread_data (bool) – Whether to read the received data
- Returns
command_id (bytes) – The received header ID
tuple – TODO
data (bytearray) – The received data
- Raises
adb.usb_exceptions.AdbCommandFailureException – Command failed
adb.adb_protocol.InvalidResponseError – Received response was not in
expected_ids
-
ReadUntil
(expected_ids, *finish_ids)[source]¶ Useful wrapper around
FileSyncConnection.Read()
.- Parameters
expected_ids (tuple[bytes]) – If the received header ID is not in
expected_ids
, an exception will be raisedfinish_ids (tuple[bytes]) – We will read until we find a header ID that is in
finish_ids
- Yields
cmd_id (bytes) – The received header ID
header (tuple) – TODO
data (bytearray) – The received data
-
Send
(command_id, data=b'', size=0)[source]¶ Send/buffer FileSync packets.
Packets are buffered and only flushed when this connection is read from. All messages have a response from the device, so this will always get flushed.
- Parameters
command_id (bytes) – Command to send.
data (str, bytes) – Optional data to send, must set data or size.
size (int) – Optionally override size from len(data).
-
_CanAddToSendBuffer
(data_len)[source]¶ Determine whether
data_len
bytes of data can be added to the send buffer without exceedingadb.adb_protocol.MAX_ADB_DATA
.- Parameters
data_len (int) – The length of the data to be potentially added to the send buffer (not including the length of its header)
- Returns
Whether
data_len
bytes of data can be added to the send buffer without exceedingadb.adb_protocol.MAX_ADB_DATA
- Return type
bool
-
_Flush
()[source]¶ TODO
- Raises
adb.usb_exceptions.WriteFailedError – Could not send data
-
_ReadBuffered
(size)[source]¶ Read
size
bytes of data fromself.recv_buffer
.- Parameters
size (int) – The amount of data to read
- Returns
result – The read data
- Return type
bytearray
-
id_to_wire
= {b'DATA': 1096040772, b'DENT': 1414415684, b'DONE': 1162760004, b'FAIL': 1279869254, b'LIST': 1414744396, b'OKAY': 1497451343, b'QUIT': 1414092113, b'RECV': 1447249234, b'SEND': 1145980243, b'STAT': 1413567571}¶
-
ids
= [b'STAT', b'LIST', b'SEND', b'RECV', b'DENT', b'DONE', b'DATA', b'OKAY', b'FAIL', b'QUIT']¶
-
wire_to_id
= {1096040772: b'DATA', 1145980243: b'SEND', 1162760004: b'DONE', 1279869254: b'FAIL', 1413567571: b'STAT', 1414092113: b'QUIT', 1414415684: b'DENT', 1414744396: b'LIST', 1447249234: b'RECV', 1497451343: b'OKAY'}¶
-
class
adb.filesync_protocol.
FilesyncProtocol
[source]¶ Bases:
object
Implements the FileSync protocol as described in sync.txt.
-
classmethod
List
(connection, path)[source]¶ Get a list of the files in
path
.- Parameters
connection (adb.adb_protocol._AdbConnection) – ADB connection
path (str, bytes) – The path for which we are getting a list of files
- Returns
files – Information about the files in
path
- Return type
list[DeviceFile]
-
classmethod
Pull
(connection, filename, dest_file, progress_callback)[source]¶ Pull a file from the device into the file-like
dest_file
.- Parameters
connection (adb.adb_protocol._AdbConnection) – ADB connection
filename (str) – The file to be pulled
dest_file (_io.BytesIO) – File-like object for writing to
progress_callback (function, None) – Callback method that accepts
filename
,bytes_written
, andtotal_bytes
- Raises
PullFailedError – Unable to pull file
-
classmethod
Push
(connection, datafile, filename, st_mode=33272, mtime=0, progress_callback=None)[source]¶ Push a file-like object to the device.
- Parameters
connection (adb.adb_protocol._AdbConnection) – ADB connection
datafile (_io.BytesIO) – File-like object for reading from
filename (str) – Filename to push to
st_mode (int) – Stat mode for filename
mtime (int) – Modification time
progress_callback (function, None) – Callback method that accepts
filename
,bytes_written
, andtotal_bytes
- Raises
PushFailedError – Raised on push failure.
-
static
Stat
(connection, filename)[source]¶ Get file status (mode, size, and mtime).
- Parameters
connection (adb.adb_protocol._AdbConnection) – ADB connection
filename (str, bytes) – The file for which we are getting info
- Returns
mode (int) – The mode of the file
size (int) – The size of the file
mtime (int) – The time of last modification for the file
- Raises
adb.adb_protocol.InvalidResponseError – Expected STAT response to STAT, got something else
-
classmethod
-
exception
adb.filesync_protocol.
InterleavedDataError
[source]¶ Bases:
Exception
We only support command sent serially.
-
exception
adb.filesync_protocol.
InvalidChecksumError
[source]¶ Bases:
Exception
Checksum of data didn’t match expected checksum.
-
adb.filesync_protocol.
MAX_PUSH_DATA
= 2048¶ Maximum size of a filesync DATA packet.