Skip to content

Commit d42fea3

Browse files
committed
Read until receiving 'constants.MESSAGE_SIZE' bytes
1 parent 2bd30f0 commit d42fea3

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

adb_shell/adb_device.py

Lines changed: 15 additions & 7 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

@@ -769,7 +778,9 @@ def _read(self, expected_cmds, adb_info):
769778
break
770779

771780
if time.time() - start > adb_info.read_timeout_s:
772-
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
781+
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
782+
783+
data = bytearray()
773784

774785
if data_length > 0:
775786
data = bytearray()
@@ -784,9 +795,6 @@ def _read(self, expected_cmds, adb_info):
784795
if actual_checksum != data_checksum:
785796
raise exceptions.InvalidChecksumError('Received checksum {0} != {1}'.format(actual_checksum, data_checksum))
786797

787-
else:
788-
data = bytearray()
789-
790798
return command, arg0, arg1, bytes(data)
791799

792800
def _read_until(self, expected_cmds, adb_info):
@@ -833,7 +841,7 @@ def _read_until(self, expected_cmds, adb_info):
833841
break
834842

835843
if time.time() - start > adb_info.read_timeout_s:
836-
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
844+
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
837845

838846
# Ignore CLSE responses to previous commands
839847
# https://github.com/JeffLIrion/adb_shell/pull/14

adb_shell/adb_device_async.py

Lines changed: 13 additions & 4 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

@@ -764,7 +773,7 @@ async def _read(self, expected_cmds, adb_info):
764773
break
765774

766775
if time.time() - start > adb_info.read_timeout_s:
767-
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
776+
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
768777

769778
if data_length > 0:
770779
data = bytearray()
@@ -828,7 +837,7 @@ async def _read_until(self, expected_cmds, adb_info):
828837
break
829838

830839
if time.time() - start > adb_info.read_timeout_s:
831-
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
840+
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
832841

833842
# Ignore CLSE responses to previous commands
834843
# https://github.com/JeffLIrion/adb_shell/pull/14

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)