Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(922)

Unified Diff: testing/scripts/common.py

Issue 2889153007: Update run_gtest_perf_test.py to streaming command output to stdout & log file at the same time (Closed)
Patch Set: Address Daniel's comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | testing/scripts/run_gtest_perf_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: testing/scripts/common.py
diff --git a/testing/scripts/common.py b/testing/scripts/common.py
index d366fe15b24e9d95c922b17441704f76167963a4..ed9bcc855745f250b2800e94d0941350f5322276 100644
--- a/testing/scripts/common.py
+++ b/testing/scripts/common.py
@@ -4,11 +4,14 @@
import argparse
import contextlib
+import io
import json
import os
+import logging
import subprocess
import sys
import tempfile
+import time
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
@@ -70,21 +73,22 @@ def run_command(argv, env=None, cwd=None):
return rc
-def run_command_with_output(argv, env=None, cwd=None, stdoutfile=None):
+def run_command_with_output(argv, stdoutfile, env=None, cwd=None, ):
dnj 2017/05/19 21:27:16 Nit, trailing comma.
nednguyen 2017/05/19 21:31:15 Done.
+ """ Run command and stream its stdout/stderr to the console & |stdoutfile|.
+ """
print 'Running %r in %r (env: %r)' % (argv, cwd, env)
- rc = 1
- try:
- output = subprocess.check_output(argv, env=env, cwd=cwd,
- stderr=subprocess.STDOUT)
- if stdoutfile:
- with open(stdoutfile, 'w') as fp:
- fp.write(output)
- rc = 0
- except Exception:
- # Exit code remains 1 and we don't write output
- pass
- print 'Command %r returned exit code %d' % (argv, rc)
- return rc
+ assert stdoutfile
+ with io.open(stdoutfile, 'wb') as writer:
dnj 2017/05/19 21:27:16 Nit, you can combine this into a single block: wi
nednguyen 2017/05/19 21:31:14 Done.
+ with io.open(stdoutfile, 'rb', 1) as reader:
dnj 2017/05/19 21:27:16 Note: the original used "w" (text-mode); "wb" and
nednguyen 2017/05/19 21:31:15 Oh, I just use "w" & "r" here to be sure.
+ process = subprocess.Popen(argv, env=env, cwd=cwd, stdout=writer,
+ stderr=subprocess.STDOUT)
+ while process.poll() is None:
+ sys.stdout.write(reader.read())
+ time.sleep(1)
dnj 2017/05/19 21:27:16 Note: if the subprocess is fast and repeated (e.g.
nednguyen 2017/05/19 21:31:15 Done.
+ # Read the remaining
+ sys.stdout.write(reader.read())
+ print 'Command %r returned exit code %d' % (argv, process.returncode)
+ return process.returncode
def run_runtest(cmd_args, runtest_args):
« no previous file with comments | « no previous file | testing/scripts/run_gtest_perf_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698