factory: Set encoding for `subprocess.Popen`
`subprocess.Popen` uses bytes by default, we will get errors if
we treat it as str type when processing. In python3, we should use str
type as much as possible, so we need to set the encoding or use
`process_utils.Spawn` to get str type.
BUG=chromium:999876
TEST=make test
TEST=manually run code segment in dut python interpreter
Change-Id: Icf617d1d9f2473f6074623ac230c36a702e51d04
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/factory/+/2182095
Commit-Queue: Yilin Yang (kerker) <kerker@chromium.org>
Tested-by: Yilin Yang (kerker) <kerker@chromium.org>
Reviewed-by: Pin-yen Lin <treapking@chromium.org>
diff --git a/py/bundle_creator/docker/util.py b/py/bundle_creator/docker/util.py
index f5dec32..c6c9ad6 100644
--- a/py/bundle_creator/docker/util.py
+++ b/py/bundle_creator/docker/util.py
@@ -84,7 +84,8 @@
os.path.join(temp_dir, 'MANIFEST.yaml')],
bufsize=1,
stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
+ stderr=subprocess.STDOUT,
+ encoding='utf-8')
output = ''
while True:
line = process.stdout.readline()
diff --git a/py/test/gooftools.py b/py/test/gooftools.py
index 4cf1e2d..174a6d2 100644
--- a/py/test/gooftools.py
+++ b/py/test/gooftools.py
@@ -53,7 +53,8 @@
proc = subprocess.Popen(system_cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
- shell=True)
+ shell=True,
+ encoding='utf-8')
# The order of output is reversed because we swapped stdout and stderr.
(err, out) = proc.communicate()
diff --git a/py/test/pytests/touchscreen_calibration/touchscreen_calibration_utils.py b/py/test/pytests/touchscreen_calibration/touchscreen_calibration_utils.py
index 287796f..7c483b7 100644
--- a/py/test/pytests/touchscreen_calibration/touchscreen_calibration_utils.py
+++ b/py/test/pytests/touchscreen_calibration/touchscreen_calibration_utils.py
@@ -51,7 +51,8 @@
def SimpleSystemOutput(cmd):
"""Execute a system command and get its output."""
try:
- proc = subprocess.Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
+ proc = subprocess.Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT,
+ encoding='utf-8')
stdout, unused_stderr = proc.communicate()
except Exception as e:
logging.warning('Command (%s) failed (%s).', cmd, e)
diff --git a/py/tools/ovl.py b/py/tools/ovl.py
index 1d6845d..0bef645 100755
--- a/py/tools/ovl.py
+++ b/py/tools/ovl.py
@@ -44,6 +44,8 @@
from ws4py.client import WebSocketBaseClient
import yaml
+from cros.factory.utils import process_utils
+
_CERT_DIR = os.path.expanduser('~/.config/ovl')
@@ -873,14 +875,13 @@
'%s%s' % (user + '@' if user else '', host)
]).wait()
- p = subprocess.Popen([
+ p = process_utils.Spawn([
'ssh',
'-S', control_file,
'-O', 'check', host,
- ], stderr=subprocess.PIPE)
- unused_stdout, stderr = p.communicate()
+ ], read_stderr=True, ignore_stdout=True)
- s = re.search(r'pid=(\d+)', stderr)
+ s = re.search(r'pid=(\d+)', p.stderr_data)
if s:
return int(s.group(1))
diff --git a/py/utils/net_utils.py b/py/utils/net_utils.py
index 7500c73..16d0582 100644
--- a/py/utils/net_utils.py
+++ b/py/utils/net_utils.py
@@ -15,7 +15,6 @@
import socket
import socketserver
import struct
-import subprocess
import time
import xmlrpc.client
@@ -203,9 +202,9 @@
last_level = 0
devices = GetEthernetInterfaces(name_patterns)
for dev in devices:
- p = subprocess.Popen('ethtool %s' % dev, shell=True,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- stat = p.communicate()[0]
+ p = process_utils.Spawn('ethtool %s' % dev, shell=True, read_stdout=True,
+ ignore_stderr=True)
+ stat = p.stdout_data
# A 4G introduced ethernet interface would not be able to report its
# setting data because it won't get online during the factory flow.