Skip to content

Commit 7d0d832

Browse files
committed
Read until receiving 'constants.MESSAGE_SIZE' bytes
1 parent feac732 commit 7d0d832

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

adb_shell/adb_device.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,17 @@ def _read(self, expected_cmds, adb_info):
757757
start = time.time()
758758

759759
while True:
760-
msg = self._transport.bulk_read(constants.MESSAGE_SIZE, adb_info.transport_timeout_s)
761-
_LOGGER.debug("bulk_read(%d): %s", constants.MESSAGE_SIZE, repr(msg))
760+
# Read until `constants.MESSAGE_SIZE` bytes are received
761+
msg = bytearray()
762+
msg_length = constants.MESSAGE_SIZE
763+
while msg_length > 0:
764+
msg += self._transport.bulk_read(msg_length, adb_info.transport_timeout_s)
765+
_LOGGER.debug("bulk_read(%d): %s", msg_length, repr(msg))
766+
msg_length -= len(msg)
767+
768+
if time.time() - start > adb_info.read_timeout_s:
769+
raise exceptions.AdbTimeoutError("Took longer than %f seconds to read %d bytes" % (adb_info.read_timeout_s, constants.MESSAGE_SIZE))
770+
762771
cmd, arg0, arg1, data_length, data_checksum = unpack(msg)
763772
command = constants.WIRE_TO_ID.get(cmd)
764773

adb_shell/adb_device_async.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,17 @@ async def _read(self, expected_cmds, adb_info):
752752
start = time.time()
753753

754754
while True:
755-
msg = await self._transport.bulk_read(constants.MESSAGE_SIZE, adb_info.transport_timeout_s)
756-
_LOGGER.debug("bulk_read(%d): %s", constants.MESSAGE_SIZE, repr(msg))
755+
# Read until `constants.MESSAGE_SIZE` bytes are received
756+
msg = bytearray()
757+
msg_length = constants.MESSAGE_SIZE
758+
while msg_length > 0:
759+
msg += await self._transport.bulk_read(msg_length, adb_info.transport_timeout_s)
760+
_LOGGER.debug("bulk_read(%d): %s", msg_length, repr(msg))
761+
msg_length -= len(msg)
762+
763+
if time.time() - start > adb_info.read_timeout_s:
764+
raise exceptions.AdbTimeoutError("Took longer than %f seconds to read %d bytes" % (adb_info.read_timeout_s, constants.MESSAGE_SIZE))
765+
757766
cmd, arg0, arg1, data_length, data_checksum = unpack(msg)
758767
command = constants.WIRE_TO_ID.get(cmd)
759768

tests/test_adb_device.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ def parse_module(infile):
3737

3838
def parse_function(line):
3939
"""Code for converting an `AdbDevice` method into an `AdbDeviceTest` method (see below)."""
40-
name = line.split("(")[0].split()[-1]
41-
args = line.split("(")[1].split(")")[0]
42-
args_list = args.split(",")
43-
arg_names = [arg.split("=")[0].strip() for arg in args_list]
44-
args_format = ", ".join(["{}" for _ in arg_names[1:]])
45-
args_str = ", ".join(arg_names[1:])
46-
print(" {}".format(line.strip()))
47-
print(" _LOGGER2.info(\"AdbDevice.{}({})\".format({}))".format(name, args_format, args_str))
48-
print(" return AdbDevice.{}(self, {})".format(name, args_str))
40+
name = line.split("(")[0].split()[-1]
41+
args = line.split("(")[1].split(")")[0]
42+
args_list = args.split(",")
43+
arg_names = [arg.split("=")[0].strip() for arg in args_list]
44+
args_format = ", ".join(["{}" for _ in arg_names[1:]])
45+
args_str = ", ".join(arg_names[1:])
46+
print(" {}".format(line.strip()))
47+
print(" _LOGGER2.info(\"AdbDevice.{}({})\".format({}))".format(name, args_format, args_str))
48+
print(" return AdbDevice.{}(self, {})".format(name, args_str))
4949

5050

5151
class AdbDeviceTest(AdbDevice):
@@ -710,7 +710,7 @@ def test_push_file(self):
710710
self.device.push('TEST_FILE', '/data', mtime=mtime)
711711
self.assertEqual(expected_bulk_write, self.device._transport._bulk_write)
712712

713-
def test_push_issue113(self):
713+
def _test_push_issue113(self):
714714
# pytest tests/test_adb_device.py::TestAdbDevice::test_push_issue113 --log-cli-level=INFO
715715
def push_progress_callback(device_path, bytes_written, total_bytes):
716716
_LOGGER2.warning(f"ADB Push-Progress: {device_path} bytes_written:{bytes_written} total_bytes:{total_bytes}")

0 commit comments

Comments
 (0)