You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bb...@apache.org on 2018/09/03 10:17:05 UTC

[mesos] branch master updated: Fixed argument parsing in python 3 support script mesos-gtest-runner.py.

This is an automated email from the ASF dual-hosted git repository.

bbannier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git


The following commit(s) were added to refs/heads/master by this push:
     new a8432c9  Fixed argument parsing in python 3 support script mesos-gtest-runner.py.
a8432c9 is described below

commit a8432c9092c78f1fc55a7d6efb17cf45f6181122
Author: Armand Grillet <ag...@mesosphere.io>
AuthorDate: Mon Sep 3 12:11:35 2018 +0200

    Fixed argument parsing in python 3 support script mesos-gtest-runner.py.
    
    Review: https://reviews.apache.org/r/68483/
---
 support/python3/mesos-gtest-runner.py | 111 +++++++++++++++++-----------------
 1 file changed, 55 insertions(+), 56 deletions(-)

diff --git a/support/python3/mesos-gtest-runner.py b/support/python3/mesos-gtest-runner.py
index 2edc73d..9cf72af 100755
--- a/support/python3/mesos-gtest-runner.py
+++ b/support/python3/mesos-gtest-runner.py
@@ -36,10 +36,7 @@ import subprocess
 import sys
 
 
-DEFAULT_NUM_JOBS = int(multiprocessing.cpu_count() * 1.5)
-
-
-class Bcolors(object):
+class Bcolors:
     """
     A collection of tty output modifiers.
 
@@ -89,86 +86,96 @@ def run_test(opts):
             universal_newlines=True)
         print(Bcolors.colorize('.', Bcolors.OKGREEN), end='')
         sys.stdout.flush()
-        return True, output.decode(sys.stdout.encoding)
+        return True, output
     except subprocess.CalledProcessError as error:
         print(Bcolors.colorize('.', Bcolors.FAIL), end='')
         sys.stdout.flush()
         return False, error.output
 
-
 def parse_arguments():
     """Return the executable to work on, and a list of options."""
-    parser = argparse.ArgumentParser(
-        usage='Usage: %prog [options] <test> [-- <test_options>]')
 
+    # If the environment variable `MESOS_GTEST_RUNNER_FLAGS` is set we
+    # use it to set a default set of flags to pass. Flags passed on
+    # the command line always have precedence over these defaults.
+    #
+    # We manually construct `args` here and make use of the fact that
+    # in `optparser`'s implementation flags passed later on the
+    # command line overrule identical flags passed earlier.
+
+    env_var = ''
+    if 'MESOS_GTEST_RUNNER_FLAGS' in os.environ:
+        env_var = os.environ['MESOS_GTEST_RUNNER_FLAGS']
+
+    env_parser = argparse.ArgumentParser()
+    env_parser.add_argument('-j', '--jobs', type=int,
+                            default=int(multiprocessing.cpu_count() * 1.5))
+    env_parser.add_argument('-s', '--sequential', type=str, default='')
+    env_parser.add_argument('-v', '--verbosity', type=int, default=1)
+
+    env_args = env_parser.parse_args(shlex.split(env_var))
+
+    # We have set the default values using the environment variable if
+    # possible, we can now parse the arguments.
+    parser = argparse.ArgumentParser()
     parser.add_argument(
-        '-j', '--jobs', type='int',
-        default=DEFAULT_NUM_JOBS,
+        '-j', '--jobs', type=int,
+        default=env_args.jobs,
         help='number of parallel jobs to spawn. DEFAULT: {default_}'
-        .format(default_=DEFAULT_NUM_JOBS))
+        .format(default_=env_args.jobs))
 
     parser.add_argument(
-        '-s', '--sequential', type='string',
-        default='',
+        '-s', '--sequential', type=str,
+        default=env_args.sequential,
         help='gtest filter for tests to run sequentially')
 
     parser.add_argument(
-        '-v', '--verbosity', type='int',
-        default=1,
+        '-v', '--verbosity', type=int,
+        default=env_args.verbosity,
         help='output verbosity:'
         ' 0 only shows summarized information,'
         ' 1 also shows full logs of failed shards, and anything'
         ' >1 shows all output. DEFAULT: 1')
+    parser.add_argument("executable", help="the executable to work on")
 
     parser.epilog = (
         'The environment variable MESOS_GTEST_RUNNER_FLAGS '
         'can be used to set a default set of flags. Flags passed on the '
         'command line always have precedence over these defaults.')
 
-    # If the environment variable `MESOS_GTEST_RUNNER_FLAGS` is set we
-    # use it to set a default set of flags to pass. Flags passed on
-    # the command line always have precedence over these defaults.
-    #
-    # We manually construct `args` here and make use of the fact that
-    # in `optparser`'s implementation flags passed later on the
-    # command line overrule identical flags passed earlier.
-    args = []
-    if 'MESOS_GTEST_RUNNER_FLAGS' in os.environ:
-        args.extend(shlex.split(os.environ['MESOS_GTEST_RUNNER_FLAGS']))
-    args.extend(sys.argv[1:])
+    args = parser.parse_args()
 
-    (options, executable) = parser.parse_args(args)
 
-    if not executable:
+    if not args.executable:
         parser.print_usage()
         sys.exit(1)
 
-    if not os.path.isfile(executable[0]):
+    if not os.path.isfile(args.executable):
         print(
             Bcolors.colorize(
                 "ERROR: File '{file}' does not exists"
-                .format(file=executable[0]), Bcolors.FAIL),
+                .format(file=args.executable), Bcolors.FAIL),
             file=sys.stderr)
         sys.exit(1)
 
-    if not os.access(executable[0], os.X_OK):
+    if not os.access(args.executable, os.X_OK):
         print(
             Bcolors.colorize(
                 "ERROR: File '{file}' is not executable"
-                .format(file=executable[0]), Bcolors.FAIL),
+                .format(file=args.executable), Bcolors.FAIL),
             file=sys.stderr)
         sys.exit(1)
 
-    if options.sequential and options.sequential.count(':-'):
+    if args.sequential and args.sequential.count(':-'):
         print(
             Bcolors.colorize(
                 "ERROR: Cannot use negative filters in "
                 "'sequential' parameter: '{filter}'"
-                .format(filter=options.sequential), Bcolors.FAIL),
+                .format(filter=args.sequential), Bcolors.FAIL),
             file=sys.stderr)
         sys.exit(1)
 
-    if options.sequential and os.environ.get('GTEST_FILTER') and \
+    if args.sequential and os.environ.get('GTEST_FILTER') and \
             os.environ['GTEST_FILTER'].count(':-'):
         print(
             Bcolors.colorize(
@@ -181,15 +188,17 @@ def parse_arguments():
 
     # Since empty strings are falsy, directly compare against `None`
     # to preserve an empty string passed via `GTEST_FILTER`.
-    if os.environ.get('GTEST_FILTER') != None:
-        options.parallel = '{env_filter}:-{sequential_filter}'\
+    if os.environ.get('GTEST_FILTER') is not None:
+        args.parallel = '{env_filter}:-{sequential_filter}'\
                          .format(env_filter=os.environ['GTEST_FILTER'],
-                                 sequential_filter=options.sequential)
+                                 sequential_filter=args.sequential)
     else:
-        options.parallel = '*:-{sequential_filter}'\
-                         .format(sequential_filter=options.sequential)
+        args.parallel = '*:-{sequential_filter}'\
+                         .format(sequential_filter=args.sequential)
 
-    return executable, options
+    executable = args.executable
+    delattr(args, 'executable')
+    return executable, args
 
 
 if __name__ == '__main__':
@@ -206,9 +215,7 @@ if __name__ == '__main__':
         # If we run in a terminal, enable colored test output. We
         # still allow users to disable this themselves via extra args.
         if sys.stdout.isatty():
-            args = executable[1:]
-            executable = '{exe} --gtest_color=yes {args}'\
-                         .format(exe=executable[0], args=args if args else '')
+            executable = '{exe} --gtest_color=yes'.format(exe=executable)
 
         if filter_:
             executable = '{exe} --gtest_filter={filter}'\
@@ -223,25 +230,17 @@ if __name__ == '__main__':
         POOL = multiprocessing.Pool(processes=OPTIONS.jobs)
 
         # Run parallel tests.
-        #
-        # Multiprocessing's `map` cannot properly handle `KeyboardInterrupt` in
-        # some python versions. Use `map_async` with an explicit timeout
-        # instead. See http://stackoverflow.com/a/1408476.
         RESULTS.extend(
-            POOL.map_async(
+            POOL.map(
                 run_test,
-                options_gen(
-                    EXECUTABLE, OPTIONS.parallel, OPTIONS.jobs)).get(
-                        timeout=sys.maxsize))
+                options_gen(EXECUTABLE, OPTIONS.parallel, OPTIONS.jobs)))
 
         # Now run sequential tests.
         if OPTIONS.sequential:
             RESULTS.extend(
-                POOL.map_async(
+                POOL.map(
                     run_test,
-                    options_gen(
-                        EXECUTABLE, OPTIONS.sequential, 1)).get(
-                            timeout=sys.maxsize))
+                    options_gen(EXECUTABLE, OPTIONS.sequential, 1)))
 
         # Count the number of failed shards and print results from
         # failed shards.