From 35f6fa31f27ffe5ce91bb2a8de18f606d09f4963 Mon Sep 17 00:00:00 2001 From: "Sakthipriyan Vairamani (thefourtheye)" Date: Mon, 28 Jan 2019 19:33:06 +0530 Subject: [PATCH 1/3] tools: make test.py Python 3 compatible --- tools/test.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/tools/test.py b/tools/test.py index 633c347e74f0d7..e088ffc319addc 100755 --- a/tools/test.py +++ b/tools/test.py @@ -65,6 +65,14 @@ def cmp(x, y): # Python 3 except NameError: xrange = range # Python 3 +try: + import urllib.parse # Python 3 + urllib_unquote = urllib.parse.unquote +except ImportError: + import urllib # Python 2 + urllib_unquote = urllib.unquote + + logger = logging.getLogger('testrunner') skip_regex = re.compile(r'# SKIP\S*\s+(.*)', re.IGNORECASE) @@ -116,7 +124,7 @@ def Run(self, tasks): # Spawn N-1 threads and then use this thread as the last one. # That way -j1 avoids threading altogether which is a nice fallback # in case of threading problems. - for i in xrange(tasks - 1): + for i in range(tasks - 1): thread = threading.Thread(target=self.RunSingle, args=[True, i + 1]) threads.append(thread) thread.start() @@ -752,7 +760,7 @@ def Execute(args, context, timeout=None, env={}, faketty=False, disable_core_fil del env_copy["NODE_PATH"] # Extend environment - for key, value in env.iteritems(): + for key, value in env.items(): env_copy[key] = value preexec_fn = None @@ -805,7 +813,7 @@ def __init__(self, context, root, section): def Contains(self, path, file): if len(path) > len(file): return False - for i in xrange(len(path)): + for i in range(len(path)): if not path[i].match(NormalizePath(file[i])): return False return True @@ -1260,7 +1268,7 @@ def GetOutcomes(self, env, defs): def Contains(self, path): if len(self.path) > len(path): return False - for i in xrange(len(self.path)): + for i in range(len(self.path)): if not self.path[i].match(path[i]): return False return True @@ -1327,7 +1335,7 @@ def BuildOptions(): help='write test output to file. NOTE: this only applies the tap progress indicator') result.add_option("-p", "--progress", help="The style of progress indicator (verbose, dots, color, mono, tap)", - choices=PROGRESS_INDICATORS.keys(), default="mono") + choices=list(PROGRESS_INDICATORS.keys()), default="mono") result.add_option("--report", help="Print a summary of the tests to be run", default=False, action="store_true") result.add_option("-s", "--suite", help="A test suite", @@ -1400,7 +1408,7 @@ def ProcessOptions(options): options.mode = options.mode.split(',') options.run = options.run.split(',') # Split at commas and filter out all the empty strings. - options.skip_tests = filter(bool, options.skip_tests.split(',')) + options.skip_tests = [test for test in options.skip_tests.split(',') if test] if options.run == [""]: options.run = None elif len(options.run) != 2: @@ -1408,7 +1416,7 @@ def ProcessOptions(options): return False else: try: - options.run = map(int, options.run) + options.run = [int(level) for level in options.run] except ValueError: print("Could not parse the integers from the run argument.") return False @@ -1477,9 +1485,8 @@ def ExpandCommand(args): return ExpandCommand else: pos = value.find('@') - import urllib - prefix = urllib.unquote(value[:pos]).split() - suffix = urllib.unquote(value[pos+1:]).split() + prefix = urllib_unquote(value[:pos]).split() + suffix = urllib_unquote(value[pos+1:]).split() def ExpandCommand(args): return prefix + args + suffix return ExpandCommand @@ -1528,8 +1535,8 @@ def PrintCrashed(code): def ArgsToTestPaths(test_root, args, suites): if len(args) == 0 or 'default' in args: - def_suites = filter(lambda s: s not in IGNORED_SUITES, suites) - args = filter(lambda a: a != 'default', args) + def_suites + def_suites = [s for s in suites if s not in IGNORED_SUITES] + args = [a for a in args if a != 'default'] + def_suites subsystem_regex = re.compile(r'^[a-zA-Z-]*$') check = lambda arg: subsystem_regex.match(arg) and (arg not in suites) mapped_args = ["*/test*-%s-*" % arg if check(arg) else arg for arg in args] @@ -1696,7 +1703,9 @@ def should_keep(case): else: return True - cases_to_run = filter(should_keep, all_cases) + cases_to_run = [ + test_case for test_case in all_cases if should_keep(test_case) + ] if options.report: print(REPORT_TEMPLATE % { @@ -1713,7 +1722,7 @@ def should_keep(case): # can be different in different machines cases_to_run.sort(key=lambda c: (c.arch, c.mode, c.file)) cases_to_run = [ cases_to_run[i] for i - in xrange(options.run[0], + in range(options.run[0], len(cases_to_run), options.run[1]) ] if len(cases_to_run) == 0: From df481e58eeebc563558d19944173aa61ec789a50 Mon Sep 17 00:00:00 2001 From: "Sakthipriyan Vairamani (thefourtheye)" Date: Tue, 29 Jan 2019 00:49:52 +0530 Subject: [PATCH 2/3] simplify importing urllib's unquote --- tools/test.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tools/test.py b/tools/test.py index e088ffc319addc..9ad61b1ede04a3 100755 --- a/tools/test.py +++ b/tools/test.py @@ -66,11 +66,9 @@ def cmp(x, y): # Python 3 xrange = range # Python 3 try: - import urllib.parse # Python 3 - urllib_unquote = urllib.parse.unquote + from urllib.parse import unquote # Python 3 except ImportError: - import urllib # Python 2 - urllib_unquote = urllib.unquote + from urllib import unquote # Python 2 logger = logging.getLogger('testrunner') @@ -1485,8 +1483,8 @@ def ExpandCommand(args): return ExpandCommand else: pos = value.find('@') - prefix = urllib_unquote(value[:pos]).split() - suffix = urllib_unquote(value[pos+1:]).split() + prefix = unquote(value[:pos]).split() + suffix = unquote(value[pos+1:]).split() def ExpandCommand(args): return prefix + args + suffix return ExpandCommand From 8644359f8b87b3de78a76181f44a28bfe6151fe1 Mon Sep 17 00:00:00 2001 From: "Sakthipriyan Vairamani (thefourtheye)" Date: Tue, 29 Jan 2019 01:12:33 +0530 Subject: [PATCH 3/3] simplify string splitting with partition --- tools/test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/test.py b/tools/test.py index 9ad61b1ede04a3..9dee2fcf850f76 100755 --- a/tools/test.py +++ b/tools/test.py @@ -1482,9 +1482,9 @@ def ExpandCommand(args): return args return ExpandCommand else: - pos = value.find('@') - prefix = unquote(value[:pos]).split() - suffix = unquote(value[pos+1:]).split() + prefix, _, suffix = value.partition('@') + prefix = unquote(prefix).split() + suffix = unquote(suffix).split() def ExpandCommand(args): return prefix + args + suffix return ExpandCommand