Skip to content

Commit 17d47b6

Browse files
flere114Commit Bot
authored and
Commit Bot
committed
cts: Migrate cts.py to python2/3 compatible
BUG=chromium:1031705 BRANCH=master TEST=None Signed-off-by: kerker <[email protected]> Change-Id: If043aa2d7d8b758571f43730635f741e3b81d8ad Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2431316 Reviewed-by: Jack Rosenthal <[email protected]> Reviewed-by: Daisuke Nojiri <[email protected]>
1 parent 60f8307 commit 17d47b6

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

cts/common/board.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
# Use of this source code is governed by a BSD-style license that can be
33
# found in the LICENSE file.
44

5+
# Note: This is a py2/3 compatible file.
6+
7+
from __future__ import print_function
8+
59
from abc import ABCMeta
610
from abc import abstractmethod
711
import os
812
import shutil
913
import subprocess as sp
1014
import serial
1115

16+
import six
17+
1218

1319
OCD_SCRIPT_DIR = '/usr/share/openocd/scripts'
1420
OPENOCD_CONFIGS = {
@@ -24,7 +30,13 @@
2430
REBOOT_MARKER = 'UART initialized after reboot'
2531

2632

27-
class Board(object):
33+
def get_subprocess_args():
34+
if six.PY3:
35+
return {'encoding': 'utf-8'}
36+
return {}
37+
38+
39+
class Board(six.with_metaclass(ABCMeta, object)):
2840
"""Class representing a single board connected to a host machine.
2941
3042
Attributes:
@@ -38,8 +50,6 @@ class Board(object):
3850
tty: String of file descriptor for tty_port
3951
"""
4052

41-
__metaclass__ = ABCMeta # This is an Abstract Base Class (ABC)
42-
4353
def __init__(self, board, module, hla_serial=None):
4454
"""Initializes a board object with given attributes.
4555
@@ -80,7 +90,7 @@ def get_stlink_serials():
8090
List of serials
8191
"""
8292
usb_args = ['sudo', 'lsusb', '-v', '-d', '0x0483:0x374b']
83-
st_link_info = sp.check_output(usb_args)
93+
st_link_info = sp.check_output(usb_args, **get_subprocess_args())
8494
st_serials = []
8595
for line in st_link_info.split('\n'):
8696
if 'iSerial' not in line:
@@ -123,7 +133,7 @@ def send_openocd_commands(self, commands):
123133

124134
def dump_openocd_log(self):
125135
with open(self.openocd_log) as log:
126-
print log.read()
136+
print(log.read())
127137

128138
def build(self, ec_dir):
129139
"""Builds test suite module for board.
@@ -151,7 +161,7 @@ def build(self, ec_dir):
151161

152162
def dump_build_log(self):
153163
with open(self.build_log) as log:
154-
print log.read()
164+
print(log.read())
155165

156166
def flash(self, image_path):
157167
"""Flashes board with most recent build ec.bin."""
@@ -212,7 +222,7 @@ def read_tty(self, max_boot_count=1):
212222
line = []
213223
boot = 0
214224
while True:
215-
c = self.tty.read()
225+
c = self.tty.read().decode('utf-8')
216226
if not c:
217227
break
218228
line.append(c)
@@ -240,7 +250,8 @@ def identify_tty_port(self):
240250
for device in com_devices:
241251
self.tty_port = os.path.join(dev_dir, device)
242252
properties = sp.check_output(
243-
['udevadm', 'info', '-a', '-n', self.tty_port, '--query=property'])
253+
['udevadm', 'info', '-a', '-n', self.tty_port, '--query=property'],
254+
**get_subprocess_args())
244255
for line in [l.strip() for l in properties.split('\n')]:
245256
if line.startswith(id_prefix):
246257
if self.hla_serial == line[len(id_prefix):]:
@@ -311,7 +322,7 @@ def save_serial(self):
311322
f.write(s)
312323
self.hla_serial = s
313324

314-
print 'Your TH serial', s, 'has been saved as', self.serial_path
325+
print('Your TH serial', s, 'has been saved as', self.serial_path)
315326
return
316327

317328

cts/cts.py

+24-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python
22
#
33
# Copyright 2016 The Chromium OS Authors. All rights reserved.
44
# Use of this source code is governed by a BSD-style license that can be
@@ -16,6 +16,9 @@
1616
# $ ./cts.py
1717
# It'll run mock tests. The result will be stored in CTS_TEST_RESULT_DIR.
1818

19+
# Note: This is a py2/3 compatible file.
20+
21+
from __future__ import print_function
1922

2023
import argparse
2124
import os
@@ -76,10 +79,10 @@ def __init__(self, ec_dir, th, dut, module):
7679

7780
def build(self):
7881
"""Build images for DUT and TH."""
79-
print 'Building DUT image...'
82+
print('Building DUT image...')
8083
if not self.dut.build(self.ec_dir):
8184
raise RuntimeError('Building module %s for DUT failed' % (self.module))
82-
print 'Building TH image...'
85+
print('Building TH image...')
8386
if not self.th.build(self.ec_dir):
8487
raise RuntimeError('Building module %s for TH failed' % (self.module))
8588

@@ -88,11 +91,11 @@ def flash_boards(self):
8891
cts_module = 'cts_' + self.module
8992
image_path = os.path.join('build', self.th.board, cts_module, 'ec.bin')
9093
self.identify_boards()
91-
print 'Flashing TH with', image_path
94+
print('Flashing TH with', image_path)
9295
if not self.th.flash(image_path):
9396
raise RuntimeError('Flashing TH failed')
9497
image_path = os.path.join('build', self.dut.board, cts_module, 'ec.bin')
95-
print 'Flashing DUT with', image_path
98+
print('Flashing DUT with', image_path)
9699
if not self.dut.flash(image_path):
97100
raise RuntimeError('Flashing DUT failed')
98101

@@ -212,7 +215,7 @@ def parse_output(self, output):
212215

213216
def get_return_code_name(self, code, strip_prefix=False):
214217
name = ''
215-
for k, v in self.return_codes.iteritems():
218+
for k, v in self.return_codes.items():
216219
if v == code:
217220
if strip_prefix:
218221
name = k[len(CTS_RC_PREFIX):]
@@ -299,52 +302,52 @@ def evaluate_result(self, result, expected_rc, expected_string):
299302

300303
def run(self):
301304
"""Resets boards, records test results in results dir."""
302-
print 'Reading serials...'
305+
print('Reading serials...')
303306
self.identify_boards()
304-
print 'Opening DUT tty...'
307+
print('Opening DUT tty...')
305308
self.dut.setup_tty()
306-
print 'Opening TH tty...'
309+
print('Opening TH tty...')
307310
self.th.setup_tty()
308311

309312
# Boards might be still writing to tty. Wait a few seconds before flashing.
310313
time.sleep(3)
311314

312315
# clear buffers
313-
print 'Clearing DUT tty...'
316+
print('Clearing DUT tty...')
314317
self.dut.read_tty()
315-
print 'Clearing TH tty...'
318+
print('Clearing TH tty...')
316319
self.th.read_tty()
317320

318321
# Resets the boards and allows them to run tests
319322
# Due to current (7/27/16) version of sync function,
320323
# both boards must be rest and halted, with the th
321324
# resuming first, in order for the test suite to run in sync
322-
print 'Halting TH...'
325+
print('Halting TH...')
323326
if not self.th.reset_halt():
324327
raise RuntimeError('Failed to halt TH')
325-
print 'Halting DUT...'
328+
print('Halting DUT...')
326329
if not self.dut.reset_halt():
327330
raise RuntimeError('Failed to halt DUT')
328-
print 'Resuming TH...'
331+
print('Resuming TH...')
329332
if not self.th.resume():
330333
raise RuntimeError('Failed to resume TH')
331-
print 'Resuming DUT...'
334+
print('Resuming DUT...')
332335
if not self.dut.resume():
333336
raise RuntimeError('Failed to resume DUT')
334337

335338
time.sleep(MAX_SUITE_TIME_SEC)
336339

337-
print 'Reading DUT tty...'
340+
print('Reading DUT tty...')
338341
dut_output, _ = self.dut.read_tty()
339342
self.dut.close_tty()
340-
print 'Reading TH tty...'
343+
print('Reading TH tty...')
341344
th_output, _ = self.th.read_tty()
342345
self.th.close_tty()
343346

344-
print 'Halting TH...'
347+
print('Halting TH...')
345348
if not self.th.reset_halt():
346349
raise RuntimeError('Failed to halt TH')
347-
print 'Halting DUT...'
350+
print('Halting DUT...')
348351
if not self.dut.reset_halt():
349352
raise RuntimeError('Failed to halt DUT')
350353

@@ -353,7 +356,7 @@ def run(self):
353356
'reading ttyACMx, please kill that process and try '
354357
'again.')
355358

356-
print 'Pursing results...'
359+
print('Pursing results...')
357360
th_results, dut_results = self.evaluate_run(dut_output, th_output)
358361

359362
# Print out results
@@ -372,7 +375,7 @@ def run(self):
372375
with open(dest, 'w') as fl:
373376
fl.write(dut_output)
374377

375-
print self.formatted_results
378+
print(self.formatted_results)
376379

377380
# TODO(chromium:735652): Should set exit code for the shell
378381

0 commit comments

Comments
 (0)