misc: Merge branch 'release-staging-v20.0.0.0' into develop
[gem5.git] / src / python / m5 / util / __init__.py
index 2fdd5a19b31f753ec3596110ceb1f7ca86167759..c59f40a81189495e1430cd7f80bfd01f450590d3 100644 (file)
@@ -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
@@ -43,6 +43,8 @@ import os
 import re
 import sys
 
+from six import string_types
+
 from . import convert
 from . import jobfile
 
@@ -122,8 +124,9 @@ def compareVersions(v1, v2):
     def make_version_list(v):
         if isinstance(v, (list,tuple)):
             return v
-        elif isinstance(v, str):
-            return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
+        elif isinstance(v, string_types):
+            return list(map(lambda x: int(re.match('\d+', x).group()),
+                            v.split('.')))
         else:
             raise TypeError()
 
@@ -174,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):
@@ -193,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]
+    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,