From dac57f3eb1cad9e181ea7efa130299a473db50a6 Mon Sep 17 00:00:00 2001 From: Yilin Yang Date: Wed, 23 Sep 2020 14:57:09 +0800 Subject: usb_updater: Migrate servo_updater.py to python2/3 compatible BUG=chromium:1031705 BRANCH=master TEST=`sudo python3 ./extra/usb_updater/servo_updater.py -b servo_v4 --force` shows "update complete". TEST=`sudo python2 ./extra/usb_updater/servo_updater.py -b servo_v4 --force` shows "update complete". Signed-off-by: kerker Change-Id: I632203b9eca4aa99dc63063c37f0ab5fc2e54dbb Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2425784 Reviewed-by: Hung-Te Lin Reviewed-by: Jack Rosenthal --- extra/tigertool/ecusb/pty_driver.py | 4 ++++ extra/tigertool/ecusb/stm32uart.py | 2 +- extra/tigertool/ecusb/tiny_servo_common.py | 10 ++++++---- extra/usb_updater/servo_updater.py | 13 +++++++++++-- 4 files changed, 22 insertions(+), 7 deletions(-) (limited to 'extra') diff --git a/extra/tigertool/ecusb/pty_driver.py b/extra/tigertool/ecusb/pty_driver.py index 2aec6fecad..63df230e7e 100644 --- a/extra/tigertool/ecusb/pty_driver.py +++ b/extra/tigertool/ecusb/pty_driver.py @@ -155,6 +155,8 @@ class ptyDriver(object): # Create a tuple which contains the entire matched string and all # the subgroups of the match. result = match.group(*range(lastindex + 1)) if match else None + if result: + result = tuple(res.decode('utf-8') for res in result) result_list.append(result) except pexpect.TIMEOUT: raise ptyError('Timeout waiting for response.') @@ -188,6 +190,8 @@ class ptyDriver(object): # Create a tuple which contains the entire matched string and all # the subgroups of the match. result = match.group(*range(lastindex + 1)) if match else None + if result: + result = tuple(res.decode('utf-8') for res in result) result_list.append(result) except pexpect.TIMEOUT: break diff --git a/extra/tigertool/ecusb/stm32uart.py b/extra/tigertool/ecusb/stm32uart.py index 21fcbd2cc2..cfbbf485a4 100644 --- a/extra/tigertool/ecusb/stm32uart.py +++ b/extra/tigertool/ecusb/stm32uart.py @@ -15,7 +15,7 @@ import time import tty import usb -import stm32usb +from . import stm32usb class SuartError(Exception): diff --git a/extra/tigertool/ecusb/tiny_servo_common.py b/extra/tigertool/ecusb/tiny_servo_common.py index 185f2a67d1..0e650a2d1d 100644 --- a/extra/tigertool/ecusb/tiny_servo_common.py +++ b/extra/tigertool/ecusb/tiny_servo_common.py @@ -12,8 +12,8 @@ import subprocess import sys import time -import pty_driver -import stm32uart +from . import pty_driver +from . import stm32uart class TinyServoError(Exception): @@ -40,7 +40,8 @@ def check_usb(vidpid, serialname=None): Returns: True if found, False, otherwise. """ if serialname: - output = subprocess.check_output(['lsusb', '-v', '-d', vidpid]) + output = subprocess.check_output(['lsusb', '-v', '-d', vidpid], + encoding='utf-8') m = re.search(r'^\s*iSerial\s+\d+\s+%s$' % serialname, output, flags=re.M) if m: return True @@ -63,7 +64,8 @@ def check_usb_sn(vidpid): Returns: string serial number if found, None otherwise. """ - output = subprocess.check_output(['lsusb', '-v', '-d', vidpid]) + output = subprocess.check_output(['lsusb', '-v', '-d', vidpid], + encoding='utf-8') m = re.search(r'^\s*iSerial\s+(.*)$', output, flags=re.M) if m: return m.group(1) diff --git a/extra/usb_updater/servo_updater.py b/extra/usb_updater/servo_updater.py index 06c36f37b6..397aa28566 100755 --- a/extra/usb_updater/servo_updater.py +++ b/extra/usb_updater/servo_updater.py @@ -1,8 +1,10 @@ -#!/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. +# Note: This is a py2/3 compatible file. + from __future__ import print_function import argparse @@ -14,6 +16,8 @@ import time import tempfile import json +import six + import fw_update import ecusb.tiny_servo_common as c @@ -236,8 +240,13 @@ def find_available_version(boardname, binfile): Returns: the version string. """ + args = {} + if six.PY3: + args['encoding'] = 'utf-8' + rawstrings = subprocess.check_output( - ['cbfstool', binfile, 'read', '-r', 'RO_FRID', '-f', '/dev/stdout']) + ['cbfstool', binfile, 'read', '-r', 'RO_FRID', '-f', '/dev/stdout'], + **args) m = re.match(r'%s_v\S+' % boardname, rawstrings) if m: newvers = m.group(0).strip(' \t\r\n\0') -- cgit v1.2.1