From c758a60220f603323b01f33a603ef3c71c70935d Mon Sep 17 00:00:00 2001 From: Rusty Howell Date: Tue, 12 Sep 2017 13:03:04 -0600 Subject: [PATCH 1/4] Fix some python3-ism with byte v. str Also make the example in the README a bit more universal. --- README.md | 10 ++++++++-- adb/adb_protocol.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 67d4750..9ba1312 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,14 @@ signer = sign_m2crypto.M2CryptoSigner( device = adb_commands.AdbCommands.ConnectDevice( rsa_keys=[signer]) # Now we can use Shell, Pull, Push, etc! -for i in xrange(10): - print device.Shell('echo %d' % i) +for i in range(10): + print(device.Shell('echo %d' % i)) + +# Want to check the kernel version? Do this. +print(device.Shell('uname -a')) + +# Want to check some properties? Do this. +print(device.Shell('getprop | grep dalvik')) ``` ### Pros diff --git a/adb/adb_protocol.py b/adb/adb_protocol.py index af2514c..b4a6441 100644 --- a/adb/adb_protocol.py +++ b/adb/adb_protocol.py @@ -279,7 +279,7 @@ def Connect(cls, usb, banner=b'notadb', rsa_keys=None, auth_timeout_ms=100): """ msg = cls( command=b'CNXN', arg0=VERSION, arg1=MAX_ADB_DATA, - data=b'host::%s\0' % banner) + data=bytearray('host::%s\0' % banner, 'utf-8')) msg.Send(usb) cmd, arg0, arg1, banner = cls.Read(usb, [b'CNXN', b'AUTH']) if cmd == b'AUTH': From dd9bffc6a82782218f96d630d2595e053b83652e Mon Sep 17 00:00:00 2001 From: Rusty Howell Date: Tue, 27 Feb 2018 10:10:41 -0700 Subject: [PATCH 2/4] Convert optional str arg to bytes The default value is a bytearray. So if the value is a str, just convert it to a bytearray before continuing. In py3, this converts unicode str to bytes. In py2, this converts bytes to bytearray, which is then just joined back into bytes. --- adb/adb_protocol.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adb/adb_protocol.py b/adb/adb_protocol.py index b4a6441..4581e5c 100644 --- a/adb/adb_protocol.py +++ b/adb/adb_protocol.py @@ -277,9 +277,12 @@ def Connect(cls, usb, banner=b'notadb', rsa_keys=None, auth_timeout_ms=100): InvalidResponseError: When the device does authentication in an unexpected way. """ + # In py3, convert unicode/str to bytes. It's joined into a byte string. + if isinstance(banner, str): + banner = bytearray(banner, 'utf-8') msg = cls( command=b'CNXN', arg0=VERSION, arg1=MAX_ADB_DATA, - data=bytearray('host::%s\0' % banner, 'utf-8')) + data=b'host::%s\0' % banner, 'utf-8') msg.Send(usb) cmd, arg0, arg1, banner = cls.Read(usb, [b'CNXN', b'AUTH']) if cmd == b'AUTH': From 25015a89ec09c3bf3b582696521fdae9e4328da1 Mon Sep 17 00:00:00 2001 From: Rusty Howell Date: Tue, 27 Feb 2018 10:25:02 -0700 Subject: [PATCH 3/4] Add better comment --- adb/adb_protocol.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adb/adb_protocol.py b/adb/adb_protocol.py index 4581e5c..3a729e4 100644 --- a/adb/adb_protocol.py +++ b/adb/adb_protocol.py @@ -277,12 +277,13 @@ def Connect(cls, usb, banner=b'notadb', rsa_keys=None, auth_timeout_ms=100): InvalidResponseError: When the device does authentication in an unexpected way. """ - # In py3, convert unicode/str to bytes. It's joined into a byte string. + # In py3/py2, convert unicode/str to bytes. It's joined into a byte string. + # In py2, this ends up kind of being a no-op. if isinstance(banner, str): banner = bytearray(banner, 'utf-8') msg = cls( command=b'CNXN', arg0=VERSION, arg1=MAX_ADB_DATA, - data=b'host::%s\0' % banner, 'utf-8') + data=b'host::%s\0' % banner) msg.Send(usb) cmd, arg0, arg1, banner = cls.Read(usb, [b'CNXN', b'AUTH']) if cmd == b'AUTH': From 5de792874433952b023d4b67cb31eff991cca4aa Mon Sep 17 00:00:00 2001 From: Rusty Howell Date: Tue, 27 Feb 2018 10:38:14 -0700 Subject: [PATCH 4/4] Clarify code comment --- adb/adb_protocol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adb/adb_protocol.py b/adb/adb_protocol.py index 3a729e4..7d95510 100644 --- a/adb/adb_protocol.py +++ b/adb/adb_protocol.py @@ -277,8 +277,8 @@ def Connect(cls, usb, banner=b'notadb', rsa_keys=None, auth_timeout_ms=100): InvalidResponseError: When the device does authentication in an unexpected way. """ - # In py3/py2, convert unicode/str to bytes. It's joined into a byte string. - # In py2, this ends up kind of being a no-op. + # In py3, convert unicode to bytes. In py2, convert str to bytes. + # It's later joined into a byte string, so in py2, this ends up kind of being a no-op. if isinstance(banner, str): banner = bytearray(banner, 'utf-8') msg = cls(