From f6b62e3b4b1dc2650e6cae68cde33efde3980b07 Mon Sep 17 00:00:00 2001 From: TortueMat Date: Wed, 25 Jan 2017 19:33:05 -0500 Subject: [PATCH 1/8] Update .gitignore and a couple of file. Manage conflict between 2.7 and 3.6 about `str` and `byte` concatenation. Also update `next()` method from `generator` to `__next__()` --- .coverage | 1 + .gitignore | 2 ++ README.md | 2 +- adb.egg-info/PKG-INFO | 31 +++++++++++++++++++++++++++++++ adb.egg-info/SOURCES.txt | 17 +++++++++++++++++ adb.egg-info/dependency_links.txt | 1 + adb.egg-info/requires.txt | 2 ++ adb.egg-info/top_level.txt | 1 + adb/adb_commands.py | 10 +++++----- adb/adb_protocol.py | 8 +++++++- adb/fastboot.py | 10 +++++----- adb/filesync_protocol.py | 4 ++-- setup.py | 2 +- test/adb_test.py | 14 +++++++------- test/common_stub.py | 6 ++++-- test/fastboot_test.py | 12 ++++++------ tox.ini | 2 +- 17 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 .coverage create mode 100644 adb.egg-info/PKG-INFO create mode 100644 adb.egg-info/SOURCES.txt create mode 100644 adb.egg-info/dependency_links.txt create mode 100644 adb.egg-info/requires.txt create mode 100644 adb.egg-info/top_level.txt diff --git a/.coverage b/.coverage new file mode 100644 index 0000000..ec7c7b1 --- /dev/null +++ b/.coverage @@ -0,0 +1 @@ +!coverage.py: This is a private format, don't read it directly!{"lines":{"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/__init__.py":[1],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_commands.py":[23,25,26,27,29,30,31,34,35,36,38,41,43,44,46,49,53,54,55,57,59,78,82,85,86,104,109,112,135,159,184,192,203,211,215,219,223,229,243,97,99,101,102,79,80,225,226,227,209,213,217,221,239,240,241,170,171,174,175,176,177,178,181,182,145,153,154,155,156,157],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_protocol.py":[18,20,21,23,27,29,32,33,34,37,38,40,47,48,51,52,55,56,59,68,69,71,75,80,81,83,89,93,107,110,125,140,150,164,166,167,60,62,61,64,65,169,171,173,180,184,191,196,205,210,211,249,250,318,319,354,355,377,378,187,189,279,280,281,174,175,176,177,178,282,207,193,194,182,208,283,213,214,215,216,217,198,199,200,203,218,219,222,223,230,231,232,233,234,237,239,241,242,247,284,316,375,398,399,336,337,338,339,340,341,342,246,343,346,349,352,84,85,86,87,400,127,128,112,113,114,116,121,122,108,90,91,123,129,132,138,401,130,131,188,95,97,98,105,235,141,142,143],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/usb_exceptions.py":[14,17,18,21,28,30,35,36,39,40,43,48,50,54,59,60,63,64,67,68,71,72,31,32],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common.py":[17,18,19,20,21,23,24,26,28,30,33,38,48,58,60,61,63,79,89,131,135,139,151,154,163,176,189,197,202,204,212,213,227,228,249,251,275,278,280,296,300,303,306,309,40,41,45],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/filesync_protocol.py":[18,20,21,22,23,25,27,28,31,33,36,37,40,41,44,45,48,49,52,53,55,66,78,88,90,125,126,129,130,132,134,146,166,191,199,203,211,81,135,138,139,142,143,144,82,157,158,160,200,201,163,164,83,193,194,168,169,204,205,209,172,213,214,215,217,218,219,173,175,177,183,187,188,189,195,84,86,196,85,103,105,106,108,109,110,112,111,114,118,119,120,121],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_m2crypto.py":[15],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot.py":[14,16,17,18,19,20,21,22,24,25,28,30,31,32,35,36,37,38,39,41,46,47,50,51,54,55,58,59,60,63,64,65,67,77,81,93,106,107,139,176,188,202,203,205,214,218,221,223,230,235,239,240,262,263,294,308,316,328,342,346,360,211,212,74,75,280,284,286,287,288,290,88,89,90,190,193,194,195,196,198,291,292,127,128,155,156,157,158,160,162,163,166,168,130,131,132,136,137,167,169,170,171,133,134,135,305,306,236,237,103,161,253,255,256,257,258,281,282,191,192,178,179,180,199,181,182,259,260,314,326,344,357,358,362,339,340,173,174,164,165],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common_cli.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_debug.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_pythonrsa.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot_debug.py":[]}} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2aa1b18..7f19aec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.pyc +.cache/ +.tox/ /adb.zip /fastboot.zip diff --git a/README.md b/README.md index 896bfe2..f35545c 100644 --- a/README.md +++ b/README.md @@ -78,4 +78,4 @@ for i in xrange(10): [coverage_link]: https://coveralls.io/github/google/python-adb?branch=master [build_img]: https://travis-ci.org/google/python-adb.svg?branch=master [build_link]: https://travis-ci.org/google/python-adb -[pycon_preso]: https://docs.google.com/a/google.com/presentation/d/e/2PACX-1vQ6DsSqOZ3oJBPM72OfxAz7FIxZ0f6tM8ZvAjp15ck9xr5cbtFBgshgOdlIAMmCoO8XmGVOc_c4iWqR/pub?start=false&loop=false&delayms=15000 +[pycon_preso]: https://docs.google.com/presentation/d/1bv8pmm8TZp4aFxoq2ohA-ms_a3BWci7D3tYvVGIm8T0/pub?start=false&loop=false&delayms=10000 diff --git a/adb.egg-info/PKG-INFO b/adb.egg-info/PKG-INFO new file mode 100644 index 0000000..e190211 --- /dev/null +++ b/adb.egg-info/PKG-INFO @@ -0,0 +1,31 @@ +Metadata-Version: 1.1 +Name: adb +Version: 1.1.0 +Summary: A pure python implementation of the Android ADB and Fastboot protocols +Home-page: https://github.com/google/python-adb +Author: Alex Lusco +Author-email: alusco@google.com +License: UNKNOWN +Description: + This repository contains a pure-python implementation of the Android + ADB and Fastboot protocols, using libusb1 for USB communications. + + This is a complete replacement and rearchitecture of the Android + project's ADB and fastboot code available at + https://github.com/android/platform_system_core/tree/master/adb + + This code is mainly targeted to users that need to communicate with + Android devices in an automated fashion, such as in automated + testing. It does not have a daemon between the client and the device, + and therefore does not support multiple simultaneous commands to the + same device. It does support any number of devices and never + communicates with a device that it wasn't intended to, unlike the + Android project's ADB. + +Keywords: android,adb,fastboot +Platform: UNKNOWN +Classifier: Development Status :: 4 - Beta +Classifier: License :: OSI Approved :: Apache Software License +Classifier: Programming Language :: Python +Classifier: Programming Language :: Python :: 3 +Classifier: Topic :: Software Development :: Testing diff --git a/adb.egg-info/SOURCES.txt b/adb.egg-info/SOURCES.txt new file mode 100644 index 0000000..d0b2a0b --- /dev/null +++ b/adb.egg-info/SOURCES.txt @@ -0,0 +1,17 @@ +adb/__init__.py +adb/adb_commands.py +adb/adb_debug.py +adb/adb_protocol.py +adb/common.py +adb/common_cli.py +adb/fastboot.py +adb/fastboot_debug.py +adb/filesync_protocol.py +adb/sign_m2crypto.py +adb/sign_pythonrsa.py +adb/usb_exceptions.py +adb.egg-info/PKG-INFO +adb.egg-info/SOURCES.txt +adb.egg-info/dependency_links.txt +adb.egg-info/requires.txt +adb.egg-info/top_level.txt \ No newline at end of file diff --git a/adb.egg-info/dependency_links.txt b/adb.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/adb.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/adb.egg-info/requires.txt b/adb.egg-info/requires.txt new file mode 100644 index 0000000..76ad123 --- /dev/null +++ b/adb.egg-info/requires.txt @@ -0,0 +1,2 @@ +libusb1>=1.0.16 +M2Crypto>=0.21.1 diff --git a/adb.egg-info/top_level.txt b/adb.egg-info/top_level.txt new file mode 100644 index 0000000..06e7af1 --- /dev/null +++ b/adb.egg-info/top_level.txt @@ -0,0 +1 @@ +adb diff --git a/adb/adb_commands.py b/adb/adb_commands.py index b37cae4..c4820f0 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -22,7 +22,7 @@ All timeouts are in milliseconds. """ -import cStringIO +import io import os import socket @@ -142,7 +142,7 @@ def Push(self, source_file, device_filename, mtime='0', timeout_ms=None): mtime: Optional, modification time to set on the file. timeout_ms: Expected timeout for any part of the push. """ - if isinstance(source_file, basestring): + if isinstance(source_file, str): if os.path.isdir(source_file): self.Shell("mkdir " + device_filename) for f in os.listdir(source_file): @@ -168,8 +168,8 @@ def Pull(self, device_filename, dest_file='', timeout_ms=None): The file data if dest_file is not set. """ if not dest_file: - dest_file = cStringIO.StringIO() - elif isinstance(dest_file, basestring): + dest_file = io.StringIO() + elif isinstance(dest_file, str): dest_file = open(dest_file, 'w') connection = self.protocol_handler.Open( self.handle, destination='sync:', @@ -178,7 +178,7 @@ def Pull(self, device_filename, dest_file='', timeout_ms=None): connection.Close() # An empty call to cStringIO.StringIO returns an instance of # cStringIO.OutputType. - if isinstance(dest_file, cStringIO.OutputType): + if isinstance(dest_file, io.StringIO): return dest_file.getvalue() def Stat(self, device_filename): diff --git a/adb/adb_protocol.py b/adb/adb_protocol.py index d118528..a3c800a 100644 --- a/adb/adb_protocol.py +++ b/adb/adb_protocol.py @@ -184,6 +184,8 @@ def checksum(self): @staticmethod def CalculateChecksum(data): # The checksum is just a sum of all the bytes. I swear. + if isinstance(data, bytes): + return sum(map(ord, data.decode('ascii'))) & 0xFFFFFFFF return sum(map(ord, data)) & 0xFFFFFFFF def Pack(self): @@ -229,7 +231,11 @@ def Read(cls, usb, expected_cmds, timeout_ms=None, total_timeout_ms=None): data = '' while data_length > 0: temp = usb.BulkRead(data_length, timeout_ms) - data += temp + if isinstance(temp, bytes): + data += temp.decode('ascii') + else: + data += temp + data_length -= len(temp) actual_checksum = cls.CalculateChecksum(data) diff --git a/adb/fastboot.py b/adb/fastboot.py index 177a916..0510fcf 100644 --- a/adb/fastboot.py +++ b/adb/fastboot.py @@ -16,7 +16,7 @@ import argparse import binascii import collections -import cStringIO +import io import logging import os import struct @@ -87,7 +87,7 @@ def SendCommand(self, command, arg=None): """ if arg is not None: command = '%s:%s' % (command, arg) - self._Write(cStringIO.StringIO(command), len(command)) + self._Write(io.StringIO(command), len(command)) def HandleSimpleResponses( self, timeout_ms=None, info_cb=DEFAULT_MESSAGE_CALLBACK): @@ -189,7 +189,7 @@ def _Write(self, data, length, progress_callback=None): """Sends the data to the device, tracking progress with the callback.""" if progress_callback: progress = self._HandleProgress(length, progress_callback) - progress.next() + progress.__next__() while length: tmp = data.read(self.chunk_kb * 1024) length -= len(tmp) @@ -277,14 +277,14 @@ def Download(self, source_file, source_len=0, Returns: Response to a download request, normally nothing. """ - if isinstance(source_file, basestring): + if isinstance(source_file, str): source_len = os.stat(source_file).st_size source_file = open(source_file) if source_len == 0: # Fall back to storing it all in memory :( data = source_file.read() - source_file = cStringIO.StringIO(data) + source_file = io.StringIO(data) source_len = len(data) self._protocol.SendCommand('download', '%08x' % source_len) diff --git a/adb/filesync_protocol.py b/adb/filesync_protocol.py index 2d4e776..49b1ab1 100644 --- a/adb/filesync_protocol.py +++ b/adb/filesync_protocol.py @@ -161,7 +161,7 @@ def Send(self, command_id, data='', size=0): self._Flush() header = struct.pack('<2I', self.id_to_wire[command_id], size) - self.send_buffer += header + data + self.send_buffer += header.decode('ascii') + data def Read(self, expected_ids, read_data=True): """Read ADB messages and return FileSync packets.""" @@ -170,7 +170,7 @@ def Read(self, expected_ids, read_data=True): # Read one filesync packet off the recv buffer. header_data = self._ReadBuffered(self.recv_header_len) - header = struct.unpack(self.recv_header_format, header_data) + header = struct.unpack(self.recv_header_format, header_data.encode('ascii')) # Header is (ID, ...). command_id = self.wire_to_id[header[0]] diff --git a/setup.py b/setup.py index 37d00c0..699d302 100644 --- a/setup.py +++ b/setup.py @@ -54,7 +54,7 @@ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python', - #'Programming Language :: Python :: 3', ## import cStringIO fails with python3 + 'Programming Language :: Python :: 3', 'Topic :: Software Development :: Testing' ] ) diff --git a/test/adb_test.py b/test/adb_test.py index 6b89782..d7a14bd 100755 --- a/test/adb_test.py +++ b/test/adb_test.py @@ -14,7 +14,7 @@ # limitations under the License. """Tests for adb.""" -import cStringIO +import io import struct import unittest @@ -159,7 +159,7 @@ def _MakeSyncHeader(cls, command, *int_parts): @classmethod def _MakeWriteSyncPacket(cls, command, data='', size=None): - return cls._MakeSyncHeader(command, size or len(data)) + data + return cls._MakeSyncHeader(command, size or len(data)) + data.encode("ascii") @classmethod def _ExpectSyncCommand(cls, write_commands, read_commands): @@ -180,7 +180,7 @@ def _ExpectSyncCommand(cls, write_commands, read_commands): return usb def testPush(self): - filedata = 'alo there, govnah' + filedata = u'alo there, govnah' mtime = 100 send = [ @@ -188,11 +188,11 @@ def testPush(self): self._MakeWriteSyncPacket('DATA', filedata), self._MakeWriteSyncPacket('DONE', size=mtime), ] - data = 'OKAY\0\0\0\0' - usb = self._ExpectSyncCommand([''.join(send)], [data]) + data = b'OKAY\0\0\0\0' + usb = self._ExpectSyncCommand([b''.join(send)], [data]) adb_commands = self._Connect(usb) - adb_commands.Push(cStringIO.StringIO(filedata), '/data', mtime=mtime) + adb_commands.Push(io.StringIO(filedata), '/data', mtime=mtime) def testPull(self): filedata = "g'ddayta, govnah" @@ -202,7 +202,7 @@ def testPull(self): self._MakeWriteSyncPacket('DATA', filedata), self._MakeWriteSyncPacket('DONE'), ] - usb = self._ExpectSyncCommand([recv], [''.join(data)]) + usb = self._ExpectSyncCommand([recv], [b''.join(data)]) adb_commands = self._Connect(usb) self.assertEqual(filedata, adb_commands.Pull('/data')) diff --git a/test/common_stub.py b/test/common_stub.py index 0e3c911..61e6703 100644 --- a/test/common_stub.py +++ b/test/common_stub.py @@ -20,9 +20,11 @@ def __init__(self): def BulkWrite(self, data, unused_timeout_ms=None): expected_data = self.written_data.pop(0) + if type(expected_data) != type(data) and isinstance(expected_data, bytes): + expected_data = expected_data.decode('ascii') if expected_data != data: - raise ValueError('Expected %s, got %s (%s)' % ( - _Dotify(expected_data), binascii.hexlify(data), _Dotify(data))) + raise ValueError('Expected %s got %s (%s)' % ( + _Dotify(expected_data), binascii.hexlify(data.encode('ascii')), _Dotify(data))) def BulkRead(self, length, timeout_ms=None): # pylint: disable=unused-argument diff --git a/test/fastboot_test.py b/test/fastboot_test.py index 983691b..a5775ed 100755 --- a/test/fastboot_test.py +++ b/test/fastboot_test.py @@ -14,7 +14,7 @@ # limitations under the License. """Tests for adb.fastboot.""" -import cStringIO +import io import os import tempfile import unittest @@ -58,7 +58,7 @@ def ExpectFlash(self, partition, succeed=True): def testDownload(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - data = cStringIO.StringIO(raw) + data = io.StringIO(raw) self.ExpectDownload([raw]) commands = fastboot.FastbootCommands(self.usb) @@ -68,14 +68,14 @@ def testDownload(self): def testDownloadFail(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - data = cStringIO.StringIO(raw) + data = io.StringIO(raw) self.ExpectDownload([raw], succeed=False) commands = fastboot.FastbootCommands(self.usb) with self.assertRaises(fastboot.FastbootRemoteFailure): commands.Download(data) - data = cStringIO.StringIO(raw) + data = io.StringIO(raw) self.ExpectDownload([raw], accept_data=False) with self.assertRaises(fastboot.FastbootTransferError): commands.Download(data) @@ -86,7 +86,7 @@ def testFlash(self): self.ExpectFlash(partition) commands = fastboot.FastbootCommands(self.usb) - output = cStringIO.StringIO() + output = io.StringIO() def InfoCb(message): if message.header == 'INFO': output.write(message.message) @@ -108,7 +108,7 @@ def testFlashFromFile(self): # More than one packet, ends somewhere into the 3rd packet. raw = 'SOMETHING' * 1086 tmp = tempfile.NamedTemporaryFile(delete=False) - tmp.write(raw) + tmp.write(raw.encode('ascii')) tmp.close() progresses = [] diff --git a/tox.ini b/tox.ini index aa9670d..d0e76fa 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,7 @@ [tox] envlist = - py27 + py36 [testenv] deps = From 74a91f8d1de1697bc216ed88fca43f031a3cc964 Mon Sep 17 00:00:00 2001 From: TortueMat Date: Wed, 25 Jan 2017 19:39:43 -0500 Subject: [PATCH 2/8] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7f19aec..46edce3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.pyc .cache/ +.coverage/ .tox/ /adb.zip /fastboot.zip From 4d4130c5de1fe238475487e7ad75507136f1ef42 Mon Sep 17 00:00:00 2001 From: TortueMat Date: Wed, 25 Jan 2017 19:44:20 -0500 Subject: [PATCH 3/8] Update .travis.yml. Change python version from 2.7 to 3.6 --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c6ac7d5..88f57cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: python sudo: false python: - - 2.7 + - 3.6 addons: apt: @@ -21,7 +21,7 @@ install: - pip install tox coveralls env: - - TOXENV=py27 + - TOXENV=py36 script: tox From 84ef5ccd2dc7c5b366211cf85c37c94074ea519c Mon Sep 17 00:00:00 2001 From: TortueMat Date: Wed, 25 Jan 2017 21:06:12 -0500 Subject: [PATCH 4/8] Update .gitignore. Add travis/tox support for python 2.7 and python 3.6. Add retrocompatibility for python 2.7 with sys_version checking --- .coverage | 2 +- .gitignore | 3 ++- .travis.yml | 1 + adb.egg-info/PKG-INFO | 31 ------------------------------- adb.egg-info/SOURCES.txt | 17 ----------------- adb.egg-info/dependency_links.txt | 1 - adb.egg-info/requires.txt | 2 -- adb.egg-info/top_level.txt | 1 - adb/fastboot.py | 22 ++++++++++++++++++---- setup.py | 1 + test/fastboot_test.py | 26 +++++++++++++++++++++----- tox.ini | 1 + 12 files changed, 45 insertions(+), 63 deletions(-) delete mode 100644 adb.egg-info/PKG-INFO delete mode 100644 adb.egg-info/SOURCES.txt delete mode 100644 adb.egg-info/dependency_links.txt delete mode 100644 adb.egg-info/requires.txt delete mode 100644 adb.egg-info/top_level.txt diff --git a/.coverage b/.coverage index ec7c7b1..1b77540 100644 --- a/.coverage +++ b/.coverage @@ -1 +1 @@ -!coverage.py: This is a private format, don't read it directly!{"lines":{"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/__init__.py":[1],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_commands.py":[23,25,26,27,29,30,31,34,35,36,38,41,43,44,46,49,53,54,55,57,59,78,82,85,86,104,109,112,135,159,184,192,203,211,215,219,223,229,243,97,99,101,102,79,80,225,226,227,209,213,217,221,239,240,241,170,171,174,175,176,177,178,181,182,145,153,154,155,156,157],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_protocol.py":[18,20,21,23,27,29,32,33,34,37,38,40,47,48,51,52,55,56,59,68,69,71,75,80,81,83,89,93,107,110,125,140,150,164,166,167,60,62,61,64,65,169,171,173,180,184,191,196,205,210,211,249,250,318,319,354,355,377,378,187,189,279,280,281,174,175,176,177,178,282,207,193,194,182,208,283,213,214,215,216,217,198,199,200,203,218,219,222,223,230,231,232,233,234,237,239,241,242,247,284,316,375,398,399,336,337,338,339,340,341,342,246,343,346,349,352,84,85,86,87,400,127,128,112,113,114,116,121,122,108,90,91,123,129,132,138,401,130,131,188,95,97,98,105,235,141,142,143],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/usb_exceptions.py":[14,17,18,21,28,30,35,36,39,40,43,48,50,54,59,60,63,64,67,68,71,72,31,32],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common.py":[17,18,19,20,21,23,24,26,28,30,33,38,48,58,60,61,63,79,89,131,135,139,151,154,163,176,189,197,202,204,212,213,227,228,249,251,275,278,280,296,300,303,306,309,40,41,45],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/filesync_protocol.py":[18,20,21,22,23,25,27,28,31,33,36,37,40,41,44,45,48,49,52,53,55,66,78,88,90,125,126,129,130,132,134,146,166,191,199,203,211,81,135,138,139,142,143,144,82,157,158,160,200,201,163,164,83,193,194,168,169,204,205,209,172,213,214,215,217,218,219,173,175,177,183,187,188,189,195,84,86,196,85,103,105,106,108,109,110,112,111,114,118,119,120,121],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_m2crypto.py":[15],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot.py":[14,16,17,18,19,20,21,22,24,25,28,30,31,32,35,36,37,38,39,41,46,47,50,51,54,55,58,59,60,63,64,65,67,77,81,93,106,107,139,176,188,202,203,205,214,218,221,223,230,235,239,240,262,263,294,308,316,328,342,346,360,211,212,74,75,280,284,286,287,288,290,88,89,90,190,193,194,195,196,198,291,292,127,128,155,156,157,158,160,162,163,166,168,130,131,132,136,137,167,169,170,171,133,134,135,305,306,236,237,103,161,253,255,256,257,258,281,282,191,192,178,179,180,199,181,182,259,260,314,326,344,357,358,362,339,340,173,174,164,165],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common_cli.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_debug.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_pythonrsa.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot_debug.py":[]}} \ No newline at end of file +!coverage.py: This is a private format, don't read it directly!{"lines":{"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_m2crypto.py":[15],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_commands.py":[171,135,175,145,174,154,23,25,26,27,156,29,30,31,240,34,35,36,38,227,41,170,43,44,46,221,176,49,178,155,53,54,55,184,57,59,181,192,182,225,177,203,78,79,80,209,82,211,85,86,215,217,219,101,159,223,97,226,99,229,102,104,109,239,112,241,243,157,153,213],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot.py":[14,16,17,18,19,21,22,25,26,27,29,30,33,35,36,37,40,41,42,43,44,46,51,52,55,56,59,60,63,64,65,68,69,70,72,79,80,82,86,93,94,95,96,101,111,114,115,135,136,138,139,140,141,142,143,144,145,147,163,164,165,166,168,169,170,171,172,173,174,175,176,177,178,179,181,182,184,186,187,188,189,190,196,198,199,200,201,204,205,206,207,209,210,213,214,216,222,223,225,229,232,234,241,246,247,248,250,251,264,266,267,268,269,270,271,273,274,291,292,293,295,297,298,299,302,304,305,306,308,319,320,322,328,330,340,342,353,354,356,358,360,371,372,374,376],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/filesync_protocol.py":[18,20,21,22,23,25,27,28,31,33,36,37,40,41,44,45,48,49,52,53,55,66,78,81,82,83,84,85,86,88,90,103,105,106,108,109,110,111,112,114,118,119,120,121,125,126,129,130,132,134,135,138,139,142,143,144,146,157,158,160,163,164,166,168,169,172,173,175,177,183,187,188,189,191,193,194,195,196,199,200,201,203,204,205,209,211,213,214,215,217,218,219],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_pythonrsa.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_protocol.py":[18,20,21,23,27,29,32,33,34,37,38,40,47,48,51,52,55,56,59,61,62,64,65,68,69,71,75,80,81,83,84,85,86,87,89,90,91,93,95,97,98,105,107,108,110,112,113,114,116,121,122,123,125,127,128,129,130,131,132,138,140,141,142,143,150,164,166,167,169,171,173,174,175,176,177,178,180,182,184,187,188,189,191,193,194,196,198,199,200,203,205,207,208,210,211,213,214,215,216,217,218,219,222,223,230,231,232,233,234,235,239,241,242,246,247,249,250,279,280,281,282,283,284,316,318,319,336,337,338,339,340,341,342,343,346,349,352,354,355,375,377,378,398,399,400,401],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common.py":[131,135,139,45,17,18,19,20,21,278,23,24,26,28,154,30,33,163,38,296,41,176,300,151,303,48,306,309,58,60,61,63,197,202,204,79,212,213,89,280,227,228,189,40,275,249,250,251],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/__init__.py":[1],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common_cli.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/usb_exceptions.py":[14,17,18,21,28,30,31,32,35,36,39,40,43,48,50,54,59,60,63,64,67,68,71,72],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot_debug.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_debug.py":[]}} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 46edce3..3f03a81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.pyc .cache/ -.coverage/ +.coverage +adb.egg-info/ .tox/ /adb.zip /fastboot.zip diff --git a/.travis.yml b/.travis.yml index 88f57cc..fadb494 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ sudo: false python: - 3.6 + - 2.7 addons: apt: diff --git a/adb.egg-info/PKG-INFO b/adb.egg-info/PKG-INFO deleted file mode 100644 index e190211..0000000 --- a/adb.egg-info/PKG-INFO +++ /dev/null @@ -1,31 +0,0 @@ -Metadata-Version: 1.1 -Name: adb -Version: 1.1.0 -Summary: A pure python implementation of the Android ADB and Fastboot protocols -Home-page: https://github.com/google/python-adb -Author: Alex Lusco -Author-email: alusco@google.com -License: UNKNOWN -Description: - This repository contains a pure-python implementation of the Android - ADB and Fastboot protocols, using libusb1 for USB communications. - - This is a complete replacement and rearchitecture of the Android - project's ADB and fastboot code available at - https://github.com/android/platform_system_core/tree/master/adb - - This code is mainly targeted to users that need to communicate with - Android devices in an automated fashion, such as in automated - testing. It does not have a daemon between the client and the device, - and therefore does not support multiple simultaneous commands to the - same device. It does support any number of devices and never - communicates with a device that it wasn't intended to, unlike the - Android project's ADB. - -Keywords: android,adb,fastboot -Platform: UNKNOWN -Classifier: Development Status :: 4 - Beta -Classifier: License :: OSI Approved :: Apache Software License -Classifier: Programming Language :: Python -Classifier: Programming Language :: Python :: 3 -Classifier: Topic :: Software Development :: Testing diff --git a/adb.egg-info/SOURCES.txt b/adb.egg-info/SOURCES.txt deleted file mode 100644 index d0b2a0b..0000000 --- a/adb.egg-info/SOURCES.txt +++ /dev/null @@ -1,17 +0,0 @@ -adb/__init__.py -adb/adb_commands.py -adb/adb_debug.py -adb/adb_protocol.py -adb/common.py -adb/common_cli.py -adb/fastboot.py -adb/fastboot_debug.py -adb/filesync_protocol.py -adb/sign_m2crypto.py -adb/sign_pythonrsa.py -adb/usb_exceptions.py -adb.egg-info/PKG-INFO -adb.egg-info/SOURCES.txt -adb.egg-info/dependency_links.txt -adb.egg-info/requires.txt -adb.egg-info/top_level.txt \ No newline at end of file diff --git a/adb.egg-info/dependency_links.txt b/adb.egg-info/dependency_links.txt deleted file mode 100644 index 8b13789..0000000 --- a/adb.egg-info/dependency_links.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/adb.egg-info/requires.txt b/adb.egg-info/requires.txt deleted file mode 100644 index 76ad123..0000000 --- a/adb.egg-info/requires.txt +++ /dev/null @@ -1,2 +0,0 @@ -libusb1>=1.0.16 -M2Crypto>=0.21.1 diff --git a/adb.egg-info/top_level.txt b/adb.egg-info/top_level.txt deleted file mode 100644 index 06e7af1..0000000 --- a/adb.egg-info/top_level.txt +++ /dev/null @@ -1 +0,0 @@ -adb diff --git a/adb/fastboot.py b/adb/fastboot.py index 0510fcf..e277ac8 100644 --- a/adb/fastboot.py +++ b/adb/fastboot.py @@ -16,7 +16,12 @@ import argparse import binascii import collections -import io +import sys + +if sys.version_info < (3, 0): + import cStringIO +else: + import io import logging import os import struct @@ -87,7 +92,10 @@ def SendCommand(self, command, arg=None): """ if arg is not None: command = '%s:%s' % (command, arg) - self._Write(io.StringIO(command), len(command)) + if sys.version_info < (3, 0): + self._Write(cStringIO.StringIO(command), len(command)) + else: + self._Write(io.StringIO(command), len(command)) def HandleSimpleResponses( self, timeout_ms=None, info_cb=DEFAULT_MESSAGE_CALLBACK): @@ -189,7 +197,10 @@ def _Write(self, data, length, progress_callback=None): """Sends the data to the device, tracking progress with the callback.""" if progress_callback: progress = self._HandleProgress(length, progress_callback) - progress.__next__() + if sys.version_info < (3,0): + progress.next() + else: + progress.__next__() while length: tmp = data.read(self.chunk_kb * 1024) length -= len(tmp) @@ -284,7 +295,10 @@ def Download(self, source_file, source_len=0, if source_len == 0: # Fall back to storing it all in memory :( data = source_file.read() - source_file = io.StringIO(data) + if sys.version_info < (3,0): + source_file = cStringIO.StringIO(data) + else: + source_file = io.StringIO(data) source_len = len(data) self._protocol.SendCommand('download', '%08x' % source_len) diff --git a/setup.py b/setup.py index 699d302..664c58d 100644 --- a/setup.py +++ b/setup.py @@ -54,6 +54,7 @@ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python', + 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 3', 'Topic :: Software Development :: Testing' ] diff --git a/test/fastboot_test.py b/test/fastboot_test.py index a5775ed..2620491 100755 --- a/test/fastboot_test.py +++ b/test/fastboot_test.py @@ -14,7 +14,11 @@ # limitations under the License. """Tests for adb.fastboot.""" -import io +import sys +if sys.version_info < (3,0): + import cStringIO +else: + import io import os import tempfile import unittest @@ -58,7 +62,10 @@ def ExpectFlash(self, partition, succeed=True): def testDownload(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - data = io.StringIO(raw) + if sys.version_info < (3,0): + data = cStringIO.StringIO(raw) + else: + data = io.StringIO(raw) self.ExpectDownload([raw]) commands = fastboot.FastbootCommands(self.usb) @@ -68,14 +75,20 @@ def testDownload(self): def testDownloadFail(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - data = io.StringIO(raw) + if sys.version_info < (3,0): + data = cStringIO.StringIO(raw) + else: + data = io.StringIO(raw) self.ExpectDownload([raw], succeed=False) commands = fastboot.FastbootCommands(self.usb) with self.assertRaises(fastboot.FastbootRemoteFailure): commands.Download(data) - data = io.StringIO(raw) + if sys.version_info < (3,0): + data = cStringIO.StringIO(raw) + else: + data = io.StringIO(raw) self.ExpectDownload([raw], accept_data=False) with self.assertRaises(fastboot.FastbootTransferError): commands.Download(data) @@ -86,7 +99,10 @@ def testFlash(self): self.ExpectFlash(partition) commands = fastboot.FastbootCommands(self.usb) - output = io.StringIO() + if sys.version_info < (3,0): + output = cStringIO.StringIO() + else: + output = io.StringIO() def InfoCb(message): if message.header == 'INFO': output.write(message.message) diff --git a/tox.ini b/tox.ini index d0e76fa..4880a55 100644 --- a/tox.ini +++ b/tox.ini @@ -6,6 +6,7 @@ [tox] envlist = py36 + py27 [testenv] deps = From 97cc86e3b55768fd87ee365acf88c5e22162ff6c Mon Sep 17 00:00:00 2001 From: TortueMat Date: Thu, 26 Jan 2017 11:10:51 -0500 Subject: [PATCH 5/8] Fix .travis.yml and update .gitignore. Delete .coverage --- .coverage | 1 - .travis.yml | 2 +- adb/fastboot.py | 24 ++++++++++-------------- test/fastboot_test.py | 26 +++++--------------------- 4 files changed, 16 insertions(+), 37 deletions(-) delete mode 100644 .coverage diff --git a/.coverage b/.coverage deleted file mode 100644 index 1b77540..0000000 --- a/.coverage +++ /dev/null @@ -1 +0,0 @@ -!coverage.py: This is a private format, don't read it directly!{"lines":{"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_m2crypto.py":[15],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_commands.py":[171,135,175,145,174,154,23,25,26,27,156,29,30,31,240,34,35,36,38,227,41,170,43,44,46,221,176,49,178,155,53,54,55,184,57,59,181,192,182,225,177,203,78,79,80,209,82,211,85,86,215,217,219,101,159,223,97,226,99,229,102,104,109,239,112,241,243,157,153,213],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot.py":[14,16,17,18,19,21,22,25,26,27,29,30,33,35,36,37,40,41,42,43,44,46,51,52,55,56,59,60,63,64,65,68,69,70,72,79,80,82,86,93,94,95,96,101,111,114,115,135,136,138,139,140,141,142,143,144,145,147,163,164,165,166,168,169,170,171,172,173,174,175,176,177,178,179,181,182,184,186,187,188,189,190,196,198,199,200,201,204,205,206,207,209,210,213,214,216,222,223,225,229,232,234,241,246,247,248,250,251,264,266,267,268,269,270,271,273,274,291,292,293,295,297,298,299,302,304,305,306,308,319,320,322,328,330,340,342,353,354,356,358,360,371,372,374,376],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/filesync_protocol.py":[18,20,21,22,23,25,27,28,31,33,36,37,40,41,44,45,48,49,52,53,55,66,78,81,82,83,84,85,86,88,90,103,105,106,108,109,110,111,112,114,118,119,120,121,125,126,129,130,132,134,135,138,139,142,143,144,146,157,158,160,163,164,166,168,169,172,173,175,177,183,187,188,189,191,193,194,195,196,199,200,201,203,204,205,209,211,213,214,215,217,218,219],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/sign_pythonrsa.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_protocol.py":[18,20,21,23,27,29,32,33,34,37,38,40,47,48,51,52,55,56,59,61,62,64,65,68,69,71,75,80,81,83,84,85,86,87,89,90,91,93,95,97,98,105,107,108,110,112,113,114,116,121,122,123,125,127,128,129,130,131,132,138,140,141,142,143,150,164,166,167,169,171,173,174,175,176,177,178,180,182,184,187,188,189,191,193,194,196,198,199,200,203,205,207,208,210,211,213,214,215,216,217,218,219,222,223,230,231,232,233,234,235,239,241,242,246,247,249,250,279,280,281,282,283,284,316,318,319,336,337,338,339,340,341,342,343,346,349,352,354,355,375,377,378,398,399,400,401],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common.py":[131,135,139,45,17,18,19,20,21,278,23,24,26,28,154,30,33,163,38,296,41,176,300,151,303,48,306,309,58,60,61,63,197,202,204,79,212,213,89,280,227,228,189,40,275,249,250,251],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/__init__.py":[1],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/common_cli.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/usb_exceptions.py":[14,17,18,21,28,30,31,32,35,36,39,40,43,48,50,54,59,60,63,64,67,68,71,72],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/fastboot_debug.py":[],"/home/mathieu/Desktop/OpenSource/Python-ADB/python-adb/adb/adb_debug.py":[]}} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index fadb494..2de6a3f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ sudo: false python: - 3.6 - - 2.7 addons: apt: @@ -23,6 +22,7 @@ install: env: - TOXENV=py36 + - TOXENV=py27 script: tox diff --git a/adb/fastboot.py b/adb/fastboot.py index e277ac8..fe4663a 100644 --- a/adb/fastboot.py +++ b/adb/fastboot.py @@ -16,15 +16,14 @@ import argparse import binascii import collections -import sys - -if sys.version_info < (3, 0): - import cStringIO -else: - import io import logging import os import struct +import io +import sys + +if sys.version_info < (3,0): + __next__ = next from adb import common from adb import usb_exceptions @@ -92,10 +91,10 @@ def SendCommand(self, command, arg=None): """ if arg is not None: command = '%s:%s' % (command, arg) - if sys.version_info < (3, 0): - self._Write(cStringIO.StringIO(command), len(command)) - else: - self._Write(io.StringIO(command), len(command)) + + if not isinstance(command, unicode): + command = unicode(command) + self._Write(io.StringIO(command), len(command)) def HandleSimpleResponses( self, timeout_ms=None, info_cb=DEFAULT_MESSAGE_CALLBACK): @@ -295,10 +294,7 @@ def Download(self, source_file, source_len=0, if source_len == 0: # Fall back to storing it all in memory :( data = source_file.read() - if sys.version_info < (3,0): - source_file = cStringIO.StringIO(data) - else: - source_file = io.StringIO(data) + source_file = io.StringIO(data) source_len = len(data) self._protocol.SendCommand('download', '%08x' % source_len) diff --git a/test/fastboot_test.py b/test/fastboot_test.py index 2620491..a5775ed 100755 --- a/test/fastboot_test.py +++ b/test/fastboot_test.py @@ -14,11 +14,7 @@ # limitations under the License. """Tests for adb.fastboot.""" -import sys -if sys.version_info < (3,0): - import cStringIO -else: - import io +import io import os import tempfile import unittest @@ -62,10 +58,7 @@ def ExpectFlash(self, partition, succeed=True): def testDownload(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - if sys.version_info < (3,0): - data = cStringIO.StringIO(raw) - else: - data = io.StringIO(raw) + data = io.StringIO(raw) self.ExpectDownload([raw]) commands = fastboot.FastbootCommands(self.usb) @@ -75,20 +68,14 @@ def testDownload(self): def testDownloadFail(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - if sys.version_info < (3,0): - data = cStringIO.StringIO(raw) - else: - data = io.StringIO(raw) + data = io.StringIO(raw) self.ExpectDownload([raw], succeed=False) commands = fastboot.FastbootCommands(self.usb) with self.assertRaises(fastboot.FastbootRemoteFailure): commands.Download(data) - if sys.version_info < (3,0): - data = cStringIO.StringIO(raw) - else: - data = io.StringIO(raw) + data = io.StringIO(raw) self.ExpectDownload([raw], accept_data=False) with self.assertRaises(fastboot.FastbootTransferError): commands.Download(data) @@ -99,10 +86,7 @@ def testFlash(self): self.ExpectFlash(partition) commands = fastboot.FastbootCommands(self.usb) - if sys.version_info < (3,0): - output = cStringIO.StringIO() - else: - output = io.StringIO() + output = io.StringIO() def InfoCb(message): if message.header == 'INFO': output.write(message.message) From 132b96282c1bb35626d025af9dd7185157df2e36 Mon Sep 17 00:00:00 2001 From: TortueMat Date: Thu, 26 Jan 2017 11:27:40 -0500 Subject: [PATCH 6/8] Fix conflict between `io` from 3.6 and `io` from 2.7 by adding a boolean PYTHON_27. --- adb/fastboot.py | 21 ++++++++++++++------- test/fastboot_test.py | 30 +++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/adb/fastboot.py b/adb/fastboot.py index fe4663a..c2fda9a 100644 --- a/adb/fastboot.py +++ b/adb/fastboot.py @@ -19,11 +19,14 @@ import logging import os import struct -import io import sys +PYTHON_27 = False if sys.version_info < (3,0): - __next__ = next + PYTHON_27 = True + import cStringIO +else: + import io from adb import common from adb import usb_exceptions @@ -92,9 +95,10 @@ def SendCommand(self, command, arg=None): if arg is not None: command = '%s:%s' % (command, arg) - if not isinstance(command, unicode): - command = unicode(command) - self._Write(io.StringIO(command), len(command)) + if PYTHON_27: + self._Write(cStringIO.StringIO(command), len(command)) + else: + self._Write(io.StringIO(command), len(command)) def HandleSimpleResponses( self, timeout_ms=None, info_cb=DEFAULT_MESSAGE_CALLBACK): @@ -196,7 +200,7 @@ def _Write(self, data, length, progress_callback=None): """Sends the data to the device, tracking progress with the callback.""" if progress_callback: progress = self._HandleProgress(length, progress_callback) - if sys.version_info < (3,0): + if PYTHON_27: progress.next() else: progress.__next__() @@ -294,7 +298,10 @@ def Download(self, source_file, source_len=0, if source_len == 0: # Fall back to storing it all in memory :( data = source_file.read() - source_file = io.StringIO(data) + if PYTHON_27: + source_file = cStringIO.StringIO(data) + else: + source_file = io.StringIO(data) source_len = len(data) self._protocol.SendCommand('download', '%08x' % source_len) diff --git a/test/fastboot_test.py b/test/fastboot_test.py index a5775ed..5aa9192 100755 --- a/test/fastboot_test.py +++ b/test/fastboot_test.py @@ -14,7 +14,15 @@ # limitations under the License. """Tests for adb.fastboot.""" -import io +import sys +PYTHON_27 = False +if sys.version_info < (3,0): + PYTHON_27 = True + +if PYTHON_27: + import cStringIO +else: + import io import os import tempfile import unittest @@ -58,7 +66,10 @@ def ExpectFlash(self, partition, succeed=True): def testDownload(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - data = io.StringIO(raw) + if PYTHON_27: + data = cStringIO.StringIO(raw) + else: + data = io.StringIO(raw) self.ExpectDownload([raw]) commands = fastboot.FastbootCommands(self.usb) @@ -68,14 +79,20 @@ def testDownload(self): def testDownloadFail(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - data = io.StringIO(raw) + if PYTHON_27: + data = cStringIO.StringIO(raw) + else: + data = io.StringIO(raw) self.ExpectDownload([raw], succeed=False) commands = fastboot.FastbootCommands(self.usb) with self.assertRaises(fastboot.FastbootRemoteFailure): commands.Download(data) - data = io.StringIO(raw) + if PYTHON_27: + data = cStringIO.StringIO(raw) + else: + data = io.StringIO(raw) self.ExpectDownload([raw], accept_data=False) with self.assertRaises(fastboot.FastbootTransferError): commands.Download(data) @@ -86,7 +103,10 @@ def testFlash(self): self.ExpectFlash(partition) commands = fastboot.FastbootCommands(self.usb) - output = io.StringIO() + if PYTHON_27: + output = cStringIO.StringIO() + else: + output = io.StringIO() def InfoCb(message): if message.header == 'INFO': output.write(message.message) From 81f7c12e03fee9f2329ea847f8c5e2803b868fb7 Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Thu, 2 Feb 2017 19:30:32 -0500 Subject: [PATCH 7/8] adb/fastboot.py: Use `next` builtin function. abd/filesync_protocol.py: Use `utf8` as encoding for sending only bytes. --- adb/fastboot.py | 26 ++++++++------------------ adb/filesync_protocol.py | 13 ++++++------- test/adb_test.py | 2 +- test/common_stub.py | 2 +- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/adb/fastboot.py b/adb/fastboot.py index c2fda9a..26958eb 100644 --- a/adb/fastboot.py +++ b/adb/fastboot.py @@ -21,12 +21,11 @@ import struct import sys -PYTHON_27 = False -if sys.version_info < (3,0): - PYTHON_27 = True - import cStringIO +PYTHON_27 = sys.version_info < (3,0) +if PYTHON_27: + from cStringIO import StringIO else: - import io + from io import StringIO from adb import common from adb import usb_exceptions @@ -94,11 +93,8 @@ def SendCommand(self, command, arg=None): """ if arg is not None: command = '%s:%s' % (command, arg) - - if PYTHON_27: - self._Write(cStringIO.StringIO(command), len(command)) - else: - self._Write(io.StringIO(command), len(command)) + + self._Write(StringIO(command), len(command)) def HandleSimpleResponses( self, timeout_ms=None, info_cb=DEFAULT_MESSAGE_CALLBACK): @@ -200,10 +196,7 @@ def _Write(self, data, length, progress_callback=None): """Sends the data to the device, tracking progress with the callback.""" if progress_callback: progress = self._HandleProgress(length, progress_callback) - if PYTHON_27: - progress.next() - else: - progress.__next__() + next(progress) while length: tmp = data.read(self.chunk_kb * 1024) length -= len(tmp) @@ -298,10 +291,7 @@ def Download(self, source_file, source_len=0, if source_len == 0: # Fall back to storing it all in memory :( data = source_file.read() - if PYTHON_27: - source_file = cStringIO.StringIO(data) - else: - source_file = io.StringIO(data) + source_file = StringIO(data) source_len = len(data) self._protocol.SendCommand('download', '%08x' % source_len) diff --git a/adb/filesync_protocol.py b/adb/filesync_protocol.py index 49b1ab1..d0db429 100644 --- a/adb/filesync_protocol.py +++ b/adb/filesync_protocol.py @@ -135,12 +135,12 @@ def __init__(self, adb_connection, recv_header_format): self.adb = adb_connection # Sending - self.send_buffer = '' + self.send_buffer = b'' self.send_header_len = struct.calcsize('<2I') # Receiving self.recv_buffer = '' - self.recv_header_format = recv_header_format + self.recv_header_format = recv_header_format.encode('utf8') self.recv_header_len = struct.calcsize(recv_header_format) def Send(self, command_id, data='', size=0): @@ -158,10 +158,9 @@ def Send(self, command_id, data='', size=0): size = len(data) if not self._CanAddToSendBuffer(len(data)): - self._Flush() - + self._Flush() header = struct.pack('<2I', self.id_to_wire[command_id], size) - self.send_buffer += header.decode('ascii') + data + self.send_buffer += header + data.encode('utf8') def Read(self, expected_ids, read_data=True): """Read ADB messages and return FileSync packets.""" @@ -169,8 +168,8 @@ def Read(self, expected_ids, read_data=True): self._Flush() # Read one filesync packet off the recv buffer. - header_data = self._ReadBuffered(self.recv_header_len) - header = struct.unpack(self.recv_header_format, header_data.encode('ascii')) + header_data = self._ReadBuffered(self.recv_header_len).encode('utf8') + header = struct.unpack(self.recv_header_format, header_data) # Header is (ID, ...). command_id = self.wire_to_id[header[0]] diff --git a/test/adb_test.py b/test/adb_test.py index d7a14bd..26fbf62 100755 --- a/test/adb_test.py +++ b/test/adb_test.py @@ -188,7 +188,7 @@ def testPush(self): self._MakeWriteSyncPacket('DATA', filedata), self._MakeWriteSyncPacket('DONE', size=mtime), ] - data = b'OKAY\0\0\0\0' + data = 'OKAY\0\0\0\0' usb = self._ExpectSyncCommand([b''.join(send)], [data]) adb_commands = self._Connect(usb) diff --git a/test/common_stub.py b/test/common_stub.py index 61e6703..64b0317 100644 --- a/test/common_stub.py +++ b/test/common_stub.py @@ -24,7 +24,7 @@ def BulkWrite(self, data, unused_timeout_ms=None): expected_data = expected_data.decode('ascii') if expected_data != data: raise ValueError('Expected %s got %s (%s)' % ( - _Dotify(expected_data), binascii.hexlify(data.encode('ascii')), _Dotify(data))) + _Dotify(expected_data), binascii.hexlify(data), _Dotify(data))) def BulkRead(self, length, timeout_ms=None): # pylint: disable=unused-argument From 50333effa49caf5aadecf16cb7dd5a68a8bda85e Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Thu, 2 Feb 2017 20:38:17 -0500 Subject: [PATCH 8/8] test/fasboot_test.py: Fix an indentation space test/common_stub.py: Fix cString import --- test/common_stub.py | 4 ++-- test/fastboot_test.py | 28 +++++++--------------------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/test/common_stub.py b/test/common_stub.py index 64b0317..a248a89 100644 --- a/test/common_stub.py +++ b/test/common_stub.py @@ -21,9 +21,9 @@ def __init__(self): def BulkWrite(self, data, unused_timeout_ms=None): expected_data = self.written_data.pop(0) if type(expected_data) != type(data) and isinstance(expected_data, bytes): - expected_data = expected_data.decode('ascii') + expected_data = expected_data.decode('utf8') if expected_data != data: - raise ValueError('Expected %s got %s (%s)' % ( + raise ValueError('Expected %s got %s (%s)' % ( _Dotify(expected_data), binascii.hexlify(data), _Dotify(data))) def BulkRead(self, length, diff --git a/test/fastboot_test.py b/test/fastboot_test.py index 5aa9192..42f269c 100755 --- a/test/fastboot_test.py +++ b/test/fastboot_test.py @@ -15,14 +15,12 @@ """Tests for adb.fastboot.""" import sys -PYTHON_27 = False -if sys.version_info < (3,0): - PYTHON_27 = True +PYTHON_27 = sys.version_info < (3,0) if PYTHON_27: - import cStringIO + from cStringIO import StringIO else: - import io + from io import StringIO import os import tempfile import unittest @@ -66,10 +64,7 @@ def ExpectFlash(self, partition, succeed=True): def testDownload(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - if PYTHON_27: - data = cStringIO.StringIO(raw) - else: - data = io.StringIO(raw) + data = StringIO(raw) self.ExpectDownload([raw]) commands = fastboot.FastbootCommands(self.usb) @@ -79,20 +74,14 @@ def testDownload(self): def testDownloadFail(self): raw = 'aoeuidhtnsqjkxbmwpyfgcrl' - if PYTHON_27: - data = cStringIO.StringIO(raw) - else: - data = io.StringIO(raw) + data = StringIO(raw) self.ExpectDownload([raw], succeed=False) commands = fastboot.FastbootCommands(self.usb) with self.assertRaises(fastboot.FastbootRemoteFailure): commands.Download(data) - if PYTHON_27: - data = cStringIO.StringIO(raw) - else: - data = io.StringIO(raw) + data = StringIO(raw) self.ExpectDownload([raw], accept_data=False) with self.assertRaises(fastboot.FastbootTransferError): commands.Download(data) @@ -103,10 +92,7 @@ def testFlash(self): self.ExpectFlash(partition) commands = fastboot.FastbootCommands(self.usb) - if PYTHON_27: - output = cStringIO.StringIO() - else: - output = io.StringIO() + output = StringIO() def InfoCb(message): if message.header == 'INFO': output.write(message.message)