From 2d6cd436f795b9159da753aae799acfbcc73560a Mon Sep 17 00:00:00 2001 From: Giacomo Travaglini Date: Mon, 11 May 2020 10:01:46 +0100 Subject: [PATCH] scons: Add readCommandWithReturn helper In this way it will be possible to reliably catch any error in the command execution which is not raising an exception to Popen. Change-Id: I4dc15648423f9bb8e8a470d97291dbd065c48eba Signed-off-by: Giacomo Travaglini Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/28847 Reviewed-by: Jason Lowe-Power Maintainer: Jason Lowe-Power Tested-by: kokoro --- src/python/m5/util/__init__.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/python/m5/util/__init__.py b/src/python/m5/util/__init__.py index 98a7a08dd..c59f40a81 100644 --- a/src/python/m5/util/__init__.py +++ b/src/python/m5/util/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016 ARM Limited +# Copyright (c) 2016, 2020 ARM Limited # All rights reserved. # # The license below extends only to copyright in the software and shall @@ -177,9 +177,16 @@ def printList(items, indent=4): line += item print(line) -def readCommand(cmd, **kwargs): - """run the command cmd, read the results and return them - this is sorta like `cmd` in shell""" +def readCommandWithReturn(cmd, **kwargs): + """ + run the command cmd, read the results and return them + this is sorta like `cmd` in shell + + :param cmd: command to run with Popen + :type cmd: string, list + :returns: pair consisting on Popen retcode and the command stdout + :rtype: (int, string) + """ from subprocess import Popen, PIPE, STDOUT if isinstance(cmd, str): @@ -196,10 +203,23 @@ def readCommand(cmd, **kwargs): subp = Popen(cmd, **kwargs) except Exception as e: if no_exception: - return exception + return -1, exception raise - return subp.communicate()[0].decode('utf-8') + output = subp.communicate()[0].decode('utf-8') + return subp.returncode, output + +def readCommand(cmd, **kwargs): + """ + run the command cmd, read the results and return them + this is sorta like `cmd` in shell + + :param cmd: command to run with Popen + :type cmd: string, list + :returns: command stdout + :rtype: string + """ + return readCommandWithReturn(cmd, **kwargs)[1] def makeDir(path): """Make a directory if it doesn't exist. If the path does exist, -- 2.30.2