python,scons: Move readCommand and compareVersions into site_scons.

These functions are only used by scons, so it makes sense to move them
to site_scons/gem5_scons/util.py.

Change-Id: If2b3995f208cb71adf3c59aac4eabe378c47f94f
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/41599
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-02-18 03:03:34 -08:00
parent 016440c58e
commit 2e27ed2351
9 changed files with 64 additions and 70 deletions

View File

@@ -41,8 +41,6 @@ import os
import re
import sys
from itertools import zip_longest
from . import convert
from . import jobfile
@@ -112,31 +110,6 @@ def applyOrMap(objOrSeq, meth, *args, **kwargs):
else:
return [applyMethod(o, meth, *args, **kwargs) for o in objOrSeq]
def compareVersions(v1, v2):
"""helper function: compare arrays or strings of version numbers.
E.g., compare_version((1,3,25), (1,4,1)')
returns -1, 0, 1 if v1 is <, ==, > v2
"""
def make_version_list(v):
if isinstance(v, (list,tuple)):
return v
elif isinstance(v, str):
return list(map(lambda x: int(re.match('\d+', x).group()),
v.split('.')))
else:
raise TypeError()
v1 = make_version_list(v1)
v2 = make_version_list(v2)
# Compare corresponding elements of lists
# The shorter list is filled with 0 till the lists have the same length
for n1,n2 in zip_longest(v1, v2, fillvalue=0):
if n1 < n2: return -1
if n1 > n2: return 1
return 0
def crossproduct(items):
if len(items) == 1:
for i in items[0]:
@@ -173,38 +146,6 @@ 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
:param cmd: command to run with Popen
:type cmd: string, list
:returns: command stdout
:rtype: string
"""
from subprocess import Popen, PIPE, STDOUT
if isinstance(cmd, str):
cmd = cmd.split()
no_exception = 'exception' in kwargs
exception = kwargs.pop('exception', None)
kwargs.setdefault('shell', False)
kwargs.setdefault('stdout', PIPE)
kwargs.setdefault('stderr', STDOUT)
kwargs.setdefault('close_fds', True)
try:
subp = Popen(cmd, **kwargs)
except Exception as e:
if no_exception:
return -1, exception
raise
output = subp.communicate()[0].decode('utf-8')
return output
def makeDir(path):
"""Make a directory if it doesn't exist. If the path does exist,
ensure that it is a directory"""