From 0da39e6b62fe7dc0f40d5242325b3b2d5b2c9d96 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Tue, 15 Aug 2017 14:37:58 -0400 Subject: Make errors in called processes more evident and easier to debug. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also trying to get unicode filenames right again. Change-Id: I501c94921b92b8a8cd6a10441aff1595fc6d878e Reviewed-on: https://pdfium-review.googlesource.com/10630 Commit-Queue: Henrique Nakashima Reviewed-by: Nicolás Peña Reviewed-by: Ryan Harrison --- testing/tools/common.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'testing/tools/common.py') 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 -- cgit v1.2.3