summaryrefslogtreecommitdiff
path: root/testing/tools/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'testing/tools/common.py')
-rwxr-xr-xtesting/tools/common.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/testing/tools/common.py b/testing/tools/common.py
index 737169f8d3..fc16004210 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 @@ def RunCommand(cmd):
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 @@ def GetBooleanGnArg(arg_name, build_dir, verbose=False):
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