Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit a9ea7b4

Browse files
tormath1fahhem
authored andcommitted
Fix#37 : Migrate from 2.7 to 3.6 (#38)
1 parent fb09de9 commit a9ea7b4

File tree

11 files changed

+60
-33
lines changed

11 files changed

+60
-33
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
*.pyc
2+
.cache/
3+
.coverage
4+
adb.egg-info/
5+
.tox/
26
/adb.zip
37
/fastboot.zip

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: python
33
sudo: false
44

55
python:
6-
- 2.7
6+
- 3.6
77

88
addons:
99
apt:
@@ -21,6 +21,7 @@ install:
2121
- pip install tox coveralls
2222

2323
env:
24+
- TOXENV=py36
2425
- TOXENV=py27
2526

2627
script: tox

adb/adb_commands.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
All timeouts are in milliseconds.
2323
"""
2424

25-
import cStringIO
25+
import io
2626
import os
2727
import socket
2828

@@ -142,7 +142,7 @@ def Push(self, source_file, device_filename, mtime='0', timeout_ms=None):
142142
mtime: Optional, modification time to set on the file.
143143
timeout_ms: Expected timeout for any part of the push.
144144
"""
145-
if isinstance(source_file, basestring):
145+
if isinstance(source_file, str):
146146
if os.path.isdir(source_file):
147147
self.Shell("mkdir " + device_filename)
148148
for f in os.listdir(source_file):
@@ -168,8 +168,8 @@ def Pull(self, device_filename, dest_file='', timeout_ms=None):
168168
The file data if dest_file is not set.
169169
"""
170170
if not dest_file:
171-
dest_file = cStringIO.StringIO()
172-
elif isinstance(dest_file, basestring):
171+
dest_file = io.StringIO()
172+
elif isinstance(dest_file, str):
173173
dest_file = open(dest_file, 'w')
174174
connection = self.protocol_handler.Open(
175175
self.handle, destination='sync:',
@@ -178,7 +178,7 @@ def Pull(self, device_filename, dest_file='', timeout_ms=None):
178178
connection.Close()
179179
# An empty call to cStringIO.StringIO returns an instance of
180180
# cStringIO.OutputType.
181-
if isinstance(dest_file, cStringIO.OutputType):
181+
if isinstance(dest_file, io.StringIO):
182182
return dest_file.getvalue()
183183

184184
def Stat(self, device_filename):

adb/adb_protocol.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ def checksum(self):
184184
@staticmethod
185185
def CalculateChecksum(data):
186186
# The checksum is just a sum of all the bytes. I swear.
187+
if isinstance(data, bytes):
188+
return sum(map(ord, data.decode('ascii'))) & 0xFFFFFFFF
187189
return sum(map(ord, data)) & 0xFFFFFFFF
188190

189191
def Pack(self):
@@ -229,7 +231,11 @@ def Read(cls, usb, expected_cmds, timeout_ms=None, total_timeout_ms=None):
229231
data = ''
230232
while data_length > 0:
231233
temp = usb.BulkRead(data_length, timeout_ms)
232-
data += temp
234+
if isinstance(temp, bytes):
235+
data += temp.decode('ascii')
236+
else:
237+
data += temp
238+
233239
data_length -= len(temp)
234240

235241
actual_checksum = cls.CalculateChecksum(data)

adb/fastboot.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
import argparse
1717
import binascii
1818
import collections
19-
import cStringIO
2019
import logging
2120
import os
2221
import struct
22+
import sys
23+
24+
PYTHON_27 = sys.version_info < (3,0)
25+
if PYTHON_27:
26+
from cStringIO import StringIO
27+
else:
28+
from io import StringIO
2329

2430
from adb import common
2531
from adb import usb_exceptions
@@ -87,7 +93,8 @@ def SendCommand(self, command, arg=None):
8793
"""
8894
if arg is not None:
8995
command = '%s:%s' % (command, arg)
90-
self._Write(cStringIO.StringIO(command), len(command))
96+
97+
self._Write(StringIO(command), len(command))
9198

9299
def HandleSimpleResponses(
93100
self, timeout_ms=None, info_cb=DEFAULT_MESSAGE_CALLBACK):
@@ -189,7 +196,7 @@ def _Write(self, data, length, progress_callback=None):
189196
"""Sends the data to the device, tracking progress with the callback."""
190197
if progress_callback:
191198
progress = self._HandleProgress(length, progress_callback)
192-
progress.next()
199+
next(progress)
193200
while length:
194201
tmp = data.read(self.chunk_kb * 1024)
195202
length -= len(tmp)
@@ -277,14 +284,14 @@ def Download(self, source_file, source_len=0,
277284
Returns:
278285
Response to a download request, normally nothing.
279286
"""
280-
if isinstance(source_file, basestring):
287+
if isinstance(source_file, str):
281288
source_len = os.stat(source_file).st_size
282289
source_file = open(source_file)
283290

284291
if source_len == 0:
285292
# Fall back to storing it all in memory :(
286293
data = source_file.read()
287-
source_file = cStringIO.StringIO(data)
294+
source_file = StringIO(data)
288295
source_len = len(data)
289296

290297
self._protocol.SendCommand('download', '%08x' % source_len)

adb/filesync_protocol.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ def __init__(self, adb_connection, recv_header_format):
135135
self.adb = adb_connection
136136

137137
# Sending
138-
self.send_buffer = ''
138+
self.send_buffer = b''
139139
self.send_header_len = struct.calcsize('<2I')
140140

141141
# Receiving
142142
self.recv_buffer = ''
143-
self.recv_header_format = recv_header_format
143+
self.recv_header_format = recv_header_format.encode('utf8')
144144
self.recv_header_len = struct.calcsize(recv_header_format)
145145

146146
def Send(self, command_id, data='', size=0):
@@ -158,18 +158,17 @@ def Send(self, command_id, data='', size=0):
158158
size = len(data)
159159

160160
if not self._CanAddToSendBuffer(len(data)):
161-
self._Flush()
162-
161+
self._Flush()
163162
header = struct.pack('<2I', self.id_to_wire[command_id], size)
164-
self.send_buffer += header + data
163+
self.send_buffer += header + data.encode('utf8')
165164

166165
def Read(self, expected_ids, read_data=True):
167166
"""Read ADB messages and return FileSync packets."""
168167
if self.send_buffer:
169168
self._Flush()
170169

171170
# Read one filesync packet off the recv buffer.
172-
header_data = self._ReadBuffered(self.recv_header_len)
171+
header_data = self._ReadBuffered(self.recv_header_len).encode('utf8')
173172
header = struct.unpack(self.recv_header_format, header_data)
174173
# Header is (ID, ...).
175174
command_id = self.wire_to_id[header[0]]

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
'Development Status :: 4 - Beta',
5555
'License :: OSI Approved :: Apache Software License',
5656
'Programming Language :: Python',
57-
#'Programming Language :: Python :: 3', ## import cStringIO fails with python3
57+
'Programming Language :: Python :: 2',
58+
'Programming Language :: Python :: 3',
5859
'Topic :: Software Development :: Testing'
5960
]
6061
)

test/adb_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
"""Tests for adb."""
1616

17-
import cStringIO
17+
import io
1818
import struct
1919
import unittest
2020

@@ -159,7 +159,7 @@ def _MakeSyncHeader(cls, command, *int_parts):
159159

160160
@classmethod
161161
def _MakeWriteSyncPacket(cls, command, data='', size=None):
162-
return cls._MakeSyncHeader(command, size or len(data)) + data
162+
return cls._MakeSyncHeader(command, size or len(data)) + data.encode("ascii")
163163

164164
@classmethod
165165
def _ExpectSyncCommand(cls, write_commands, read_commands):
@@ -180,7 +180,7 @@ def _ExpectSyncCommand(cls, write_commands, read_commands):
180180
return usb
181181

182182
def testPush(self):
183-
filedata = 'alo there, govnah'
183+
filedata = u'alo there, govnah'
184184
mtime = 100
185185

186186
send = [
@@ -189,10 +189,10 @@ def testPush(self):
189189
self._MakeWriteSyncPacket('DONE', size=mtime),
190190
]
191191
data = 'OKAY\0\0\0\0'
192-
usb = self._ExpectSyncCommand([''.join(send)], [data])
192+
usb = self._ExpectSyncCommand([b''.join(send)], [data])
193193

194194
adb_commands = self._Connect(usb)
195-
adb_commands.Push(cStringIO.StringIO(filedata), '/data', mtime=mtime)
195+
adb_commands.Push(io.StringIO(filedata), '/data', mtime=mtime)
196196

197197
def testPull(self):
198198
filedata = "g'ddayta, govnah"
@@ -202,7 +202,7 @@ def testPull(self):
202202
self._MakeWriteSyncPacket('DATA', filedata),
203203
self._MakeWriteSyncPacket('DONE'),
204204
]
205-
usb = self._ExpectSyncCommand([recv], [''.join(data)])
205+
usb = self._ExpectSyncCommand([recv], [b''.join(data)])
206206
adb_commands = self._Connect(usb)
207207
self.assertEqual(filedata, adb_commands.Pull('/data'))
208208

test/common_stub.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def __init__(self):
2020

2121
def BulkWrite(self, data, unused_timeout_ms=None):
2222
expected_data = self.written_data.pop(0)
23+
if type(expected_data) != type(data) and isinstance(expected_data, bytes):
24+
expected_data = expected_data.decode('utf8')
2325
if expected_data != data:
24-
raise ValueError('Expected %s, got %s (%s)' % (
25-
_Dotify(expected_data), binascii.hexlify(data), _Dotify(data)))
26+
raise ValueError('Expected %s got %s (%s)' % (
27+
_Dotify(expected_data), binascii.hexlify(data), _Dotify(data)))
2628

2729
def BulkRead(self, length,
2830
timeout_ms=None): # pylint: disable=unused-argument

test/fastboot_test.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
# limitations under the License.
1515
"""Tests for adb.fastboot."""
1616

17-
import cStringIO
17+
import sys
18+
PYTHON_27 = sys.version_info < (3,0)
19+
20+
if PYTHON_27:
21+
from cStringIO import StringIO
22+
else:
23+
from io import StringIO
1824
import os
1925
import tempfile
2026
import unittest
@@ -58,7 +64,7 @@ def ExpectFlash(self, partition, succeed=True):
5864

5965
def testDownload(self):
6066
raw = 'aoeuidhtnsqjkxbmwpyfgcrl'
61-
data = cStringIO.StringIO(raw)
67+
data = StringIO(raw)
6268

6369
self.ExpectDownload([raw])
6470
commands = fastboot.FastbootCommands(self.usb)
@@ -68,14 +74,14 @@ def testDownload(self):
6874

6975
def testDownloadFail(self):
7076
raw = 'aoeuidhtnsqjkxbmwpyfgcrl'
71-
data = cStringIO.StringIO(raw)
77+
data = StringIO(raw)
7278

7379
self.ExpectDownload([raw], succeed=False)
7480
commands = fastboot.FastbootCommands(self.usb)
7581
with self.assertRaises(fastboot.FastbootRemoteFailure):
7682
commands.Download(data)
7783

78-
data = cStringIO.StringIO(raw)
84+
data = StringIO(raw)
7985
self.ExpectDownload([raw], accept_data=False)
8086
with self.assertRaises(fastboot.FastbootTransferError):
8187
commands.Download(data)
@@ -86,7 +92,7 @@ def testFlash(self):
8692
self.ExpectFlash(partition)
8793
commands = fastboot.FastbootCommands(self.usb)
8894

89-
output = cStringIO.StringIO()
95+
output = StringIO()
9096
def InfoCb(message):
9197
if message.header == 'INFO':
9298
output.write(message.message)
@@ -108,7 +114,7 @@ def testFlashFromFile(self):
108114
# More than one packet, ends somewhere into the 3rd packet.
109115
raw = 'SOMETHING' * 1086
110116
tmp = tempfile.NamedTemporaryFile(delete=False)
111-
tmp.write(raw)
117+
tmp.write(raw.encode('ascii'))
112118
tmp.close()
113119
progresses = []
114120

0 commit comments

Comments
 (0)