summaryrefslogtreecommitdiff
path: root/extra/usb_serial
diff options
context:
space:
mode:
authorYilin Yang <kerker@google.com>2020-09-22 14:06:52 +0800
committerCommit Bot <commit-bot@chromium.org>2020-09-26 02:04:26 +0000
commit289bb8aa3b431395d895105e6f44a58ef64c9543 (patch)
tree74913cb645c42178f3f65e792e2b988b906070e6 /extra/usb_serial
parente4486b47c0e617e867439665dd1a774f50f68f4c (diff)
downloadchrome-ec-289bb8aa3b431395d895105e6f44a58ef64c9543.tar.gz
usb_serial: Migrate console.py to python2/3 compatible
Trace pyusb source code to make sure the correct interface, and change them properly. BUG=chromium:1031705 BRANCH=master TEST=None Signed-off-by: kerker <kerker@chromium.org> Change-Id: I185bf8a7d47f641f6554ba2d36a11d56967661b0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2422929 Reviewed-by: Hung-Te Lin <hungte@chromium.org> Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Diffstat (limited to 'extra/usb_serial')
-rwxr-xr-xextra/usb_serial/console.py41
1 files changed, 26 insertions, 15 deletions
diff --git a/extra/usb_serial/console.py b/extra/usb_serial/console.py
index 75879fb99c..bd1fbaacc6 100755
--- a/extra/usb_serial/console.py
+++ b/extra/usb_serial/console.py
@@ -1,12 +1,15 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Allow creation of uart/console interface via usb google serial endpoint."""
+
+# Note: This is a py2/3 compatible file.
+
+from __future__ import print_function
import argparse
import array
-import exceptions
import os
import sys
import termios
@@ -17,13 +20,21 @@ import tty
try:
import usb
except:
- print "import usb failed"
- print "try running these commands:"
- print " sudo apt-get install python-pip"
- print " sudo pip install --pre pyusb"
- print ""
+ print("import usb failed")
+ print("try running these commands:")
+ print(" sudo apt-get install python-pip")
+ print(" sudo pip install --pre pyusb")
+ print()
sys.exit(-1)
+import six
+
+
+def GetBuffer(stream):
+ if six.PY3:
+ return stream.buffer
+ return stream
+
"""Class Susb covers USB device discovery and initialization.
@@ -177,14 +188,14 @@ class Suart():
try:
r = self._susb._read_ep.read(64, self._susb.TIMEOUT_MS)
if r:
- sys.stdout.write(r.tostring())
- sys.stdout.flush()
+ GetBuffer(sys.stdout).write(r.tostring())
+ GetBuffer(sys.stdout).flush()
except Exception as e:
# If we miss some characters on pty disconnect, that's fine.
# ep.read() also throws USBError on timeout, which we discard.
- if not isinstance(e, (exceptions.OSError, usb.core.USBError)):
- print "rx %s" % (e,)
+ if not isinstance(e, (OSError, usb.core.USBError)):
+ print("rx %s" % e)
finally:
self._done.set()
@@ -192,14 +203,14 @@ class Suart():
try:
while True:
try:
- r = sys.stdin.read(1)
+ r = GetBuffer(sys.stdin).read(1)
if not r or r == b"\x03":
break
if r:
- self._susb._write_ep.write(array.array(b"B", r),
+ self._susb._write_ep.write(array.array('B', r),
self._susb.TIMEOUT_MS)
except Exception as e:
- print "tx %s" % (e,)
+ print("tx %s" % e)
finally:
self._done.set()
@@ -276,7 +287,7 @@ def main():
os.system("stty echo")
# Avoid having the user's shell prompt start mid-line after the final output
# from this program.
- print
+ print()
if __name__ == '__main__':