[PATCH 1/2] parallel-vm.py: use argparse module

Johannes Berg johannes at sipsolutions.net
Tue Mar 3 17:08:40 EST 2015


From: Johannes Berg <johannes.berg at intel.com>

Instead of hand-writing a (positional) parser, use the argparse module.
This also gets us nice help output.

Signed-off-by: Johannes Berg <johannes.berg at intel.com>
---
 tests/hwsim/vm/parallel-vm.py | 49 ++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/tests/hwsim/vm/parallel-vm.py b/tests/hwsim/vm/parallel-vm.py
index 13205fe8afca..27033791fc0f 100755
--- a/tests/hwsim/vm/parallel-vm.py
+++ b/tests/hwsim/vm/parallel-vm.py
@@ -240,6 +240,7 @@ def show_progress(scr):
     time.sleep(0.3)
 
 def main():
+    import argparse
     global num_servers
     global vm
     global dir
@@ -257,26 +258,24 @@ def main():
     debug_level = logging.INFO
     rerun_failures = True
 
-    if len(sys.argv) < 2:
-        sys.exit("Usage: %s <number of VMs> [-1] [--debug] [--codecov] [params..]" % sys.argv[0])
-    num_servers = int(sys.argv[1])
-    if num_servers < 1:
-        sys.exit("Too small number of VMs")
-
-    timestamp = int(time.time())
-
-    idx = 2
-
-    if len(sys.argv) > idx and sys.argv[idx] == "-1":
-        idx += 1
-        rerun_failures = False
-
-    if len(sys.argv) > idx and sys.argv[idx] == "--debug":
-        idx += 1
+    p = argparse.ArgumentParser(description='run multiple testing VMs in parallel')
+    p.add_argument('num_servers', metavar='number of VMs', type=int, choices=range(1, 100),
+                   help="number of VMs to start")
+    p.add_argument('-1', dest='no_retry', action='store_const', const=True, default=False,
+                   help="don't retry failed tests automatically")
+    p.add_argument('--debug', dest='debug', action='store_const', const=True, default=False,
+                   help="enable debug logging")
+    p.add_argument('--codecov', dest='codecov', action='store_const', const=True, default=False,
+                   help="enable code coverage collection")
+    p.add_argument('--shuffle-tests', dest='shuffle', action='store_const', const=True, default=False,
+                   help="enable code coverage collection")
+    p.add_argument('params', nargs='*')
+    args = p.parse_args()
+    num_servers = args.num_servers
+    rerun_failures = not args.no_retry
+    if args.debug:
         debug_level = logging.DEBUG
-
-    if len(sys.argv) > idx and sys.argv[idx] == "--codecov":
-        idx += 1
+    if args.codecov:
         print "Code coverage - build separate binaries"
         logdir = "/tmp/hwsim-test-logs/" + str(timestamp)
         os.makedirs(logdir)
@@ -287,19 +286,21 @@ def main():
         codecov_args = []
         codecov = False
 
+    timestamp = int(time.time())
+
     first_run_failures = []
     tests = []
-    cmd = [ '../run-tests.py', '-L' ] + sys.argv[idx:]
+    cmd = [ '../run-tests.py', '-L' ] + args.params
     lst = subprocess.Popen(cmd, stdout=subprocess.PIPE)
     for l in lst.stdout.readlines():
         name = l.split(' ')[0]
         tests.append(name)
     if len(tests) == 0:
         sys.exit("No test cases selected")
-    if '-f' in sys.argv[idx:]:
-        extra_args = sys.argv[idx:]
+    if '-f' in args.params:
+        extra_args = args.params
     else:
-        extra_args = [x for x in sys.argv[idx:] if x not in tests]
+        extra_args = [x for x in args.params if x not in tests]
 
     dir = '/tmp/hwsim-test-logs'
     try:
@@ -307,7 +308,7 @@ def main():
     except:
         pass
 
-    if "--shuffle-tests" in extra_args:
+    if args.shuffle:
         from random import shuffle
         shuffle(tests)
     elif num_servers > 2 and len(tests) > 100:
-- 
2.1.4



More information about the HostAP mailing list