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:
@@ -91,8 +91,6 @@ import SCons.Node
|
|||||||
import SCons.Node.FS
|
import SCons.Node.FS
|
||||||
import SCons.Tool
|
import SCons.Tool
|
||||||
|
|
||||||
from m5.util import compareVersions, readCommand
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
#
|
#
|
||||||
@@ -132,6 +130,7 @@ from gem5_scons import error, warning, summarize_warnings, parse_build_path
|
|||||||
from gem5_scons import TempFileSpawn, EnvDefaults, MakeAction, MakeActionTool
|
from gem5_scons import TempFileSpawn, EnvDefaults, MakeAction, MakeActionTool
|
||||||
import gem5_scons
|
import gem5_scons
|
||||||
from gem5_scons.builders import ConfigFile, AddLocalRPATH, SwitchingHeaders
|
from gem5_scons.builders import ConfigFile, AddLocalRPATH, SwitchingHeaders
|
||||||
|
from gem5_scons.util import compareVersions, readCommand
|
||||||
|
|
||||||
Export('MakeAction')
|
Export('MakeAction')
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ import os, subprocess
|
|||||||
|
|
||||||
Import('main')
|
Import('main')
|
||||||
|
|
||||||
from m5.util import compareVersions
|
|
||||||
|
|
||||||
elf_files = []
|
elf_files = []
|
||||||
def ElfFile(filename):
|
def ElfFile(filename):
|
||||||
elf_files.append(File(filename))
|
elf_files.append(File(filename))
|
||||||
|
|||||||
@@ -38,6 +38,8 @@
|
|||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
import itertools
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import SCons.Script
|
import SCons.Script
|
||||||
@@ -50,3 +52,60 @@ def ignore_style():
|
|||||||
|
|
||||||
def get_termcap():
|
def get_termcap():
|
||||||
return m5.util.terminal.get_termcap(SCons.Script.GetOption('use_colors'))
|
return m5.util.terminal.get_termcap(SCons.Script.GetOption('use_colors'))
|
||||||
|
|
||||||
|
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 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 itertools.zip_longest(v1, v2, fillvalue=0):
|
||||||
|
if n1 < n2: return -1
|
||||||
|
if n1 > n2: return 1
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import gem5_scons.util
|
import gem5_scons.util
|
||||||
from m5.util import readCommand
|
|
||||||
|
|
||||||
git_style_message = """
|
git_style_message = """
|
||||||
You're missing the gem5 style or commit message hook. These hooks help
|
You're missing the gem5 style or commit message hook. These hooks help
|
||||||
@@ -52,7 +51,7 @@ Press enter to continue, or ctrl-c to abort: """
|
|||||||
|
|
||||||
def install_style_hooks(env):
|
def install_style_hooks(env):
|
||||||
try:
|
try:
|
||||||
gitdir = env.Dir(readCommand(
|
gitdir = env.Dir(gem5_scons.util.readCommand(
|
||||||
["git", "rev-parse", "--git-dir"]).strip("\n"))
|
["git", "rev-parse", "--git-dir"]).strip("\n"))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Warning: Failed to find git repo directory: %s" % e)
|
print("Warning: Failed to find git repo directory: %s" % e)
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ Export('env')
|
|||||||
|
|
||||||
build_env = [(opt, env[opt]) for opt in export_vars]
|
build_env = [(opt, env[opt]) for opt in export_vars]
|
||||||
|
|
||||||
from m5.util import code_formatter, compareVersions, readCommand
|
from m5.util import code_formatter
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
# Code for adding source files of various types
|
# Code for adding source files of various types
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
Import('*')
|
Import('*')
|
||||||
|
|
||||||
from gem5_scons import warning
|
from gem5_scons import warning
|
||||||
from m5.util import readCommand, compareVersions
|
from gem5_scons.util import readCommand, compareVersions
|
||||||
|
|
||||||
import gem5_scons
|
import gem5_scons
|
||||||
|
|
||||||
|
|||||||
@@ -41,8 +41,6 @@ import os
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from itertools import zip_longest
|
|
||||||
|
|
||||||
from . import convert
|
from . import convert
|
||||||
from . import jobfile
|
from . import jobfile
|
||||||
|
|
||||||
@@ -112,31 +110,6 @@ def applyOrMap(objOrSeq, meth, *args, **kwargs):
|
|||||||
else:
|
else:
|
||||||
return [applyMethod(o, meth, *args, **kwargs) for o in objOrSeq]
|
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):
|
def crossproduct(items):
|
||||||
if len(items) == 1:
|
if len(items) == 1:
|
||||||
for i in items[0]:
|
for i in items[0]:
|
||||||
@@ -173,38 +146,6 @@ def printList(items, indent=4):
|
|||||||
line += item
|
line += item
|
||||||
print(line)
|
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):
|
def makeDir(path):
|
||||||
"""Make a directory if it doesn't exist. If the path does exist,
|
"""Make a directory if it doesn't exist. If the path does exist,
|
||||||
ensure that it is a directory"""
|
ensure that it is a directory"""
|
||||||
|
|||||||
@@ -23,8 +23,6 @@
|
|||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
from m5.util import compareVersions
|
|
||||||
|
|
||||||
import gem5_scons
|
import gem5_scons
|
||||||
|
|
||||||
Import('*')
|
Import('*')
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
Import('*')
|
Import('*')
|
||||||
|
|
||||||
from m5.util import compareVersions
|
from gem5_scons.util import compareVersions
|
||||||
|
|
||||||
if env['USE_SYSTEMC']:
|
if env['USE_SYSTEMC']:
|
||||||
if main['GCC'] and compareVersions(main['CXXVERSION'], '10.0') >= 0:
|
if main['GCC'] and compareVersions(main['CXXVERSION'], '10.0') >= 0:
|
||||||
|
|||||||
Reference in New Issue
Block a user