Make errors in called processes more evident and easier to debug.
Also trying to get unicode filenames right again.
Change-Id: I501c94921b92b8a8cd6a10441aff1595fc6d878e
Reviewed-on: https://pdfium-review.googlesource.com/10630
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: Nicolás Peña <npm@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
diff --git a/testing/tools/common.py b/testing/tools/common.py
index 737169f..fc16004 100755
--- a/testing/tools/common.py
+++ b/testing/tools/common.py
@@ -3,6 +3,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+import datetime
import glob
import os
import re
@@ -26,6 +27,38 @@
except subprocess.CalledProcessError as e:
return e
+
+def RunCommandPropagateErr(cmd, stdout_has_errors=False,
+ exit_status_on_error=None):
+ """Run a command as a subprocess.
+
+ Errors in that subprocess are printed out if it returns an error exit code.
+
+ Args:
+ cmd: Command to run as a list of strings.
+ stdout_has_errors: Whether to print stdout instead of stderr on an error
+ exit.
+ exit_status_on_error: If specified, upon an error in the subprocess the
+ caller script exits immediately with the given status.
+ """
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ output, err = p.communicate()
+
+ if p.returncode:
+ PrintErr('\nError when invoking "%s"' % ' '.join(cmd))
+ if stdout_has_errors:
+ PrintErr(output)
+
+ PrintErr(err)
+
+ if exit_status_on_error is not None:
+ sys.exit(exit_status_on_error)
+
+ return None
+
+ return output
+
+
# RunCommandExtractHashedFiles returns a tuple: (raised_exception, hashed_files)
# It runs the given command. If it fails it will return an exception and None.
# If it succeeds it will return None and the list of processed files extracted
@@ -110,3 +143,14 @@
if verbose:
print >> sys.stderr, "Found '%s' for value of %s" % (arg_match_output, arg)
return arg_match_output == 'true'
+
+
+def PrintWithTime(s):
+ """Prints s prepended by a timestamp."""
+ print '[%s] %s' % (datetime.datetime.now().strftime("%Y%m%d %H:%M:%S"),
+ s)
+
+
+def PrintErr(s):
+ """Prints s to stderr."""
+ print >> sys.stderr, s