diff mbox

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

Message ID 1425420521-6153-1-git-send-email-johannes@sipsolutions.net
State Accepted
Headers show

Commit Message

Johannes Berg March 3, 2015, 10:08 p.m. UTC
From: Johannes Berg <johannes.berg@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@intel.com>
---
 tests/hwsim/vm/parallel-vm.py | 49 ++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 24 deletions(-)

Comments

Jouni Malinen March 7, 2015, 8:10 p.m. UTC | #1
On Tue, Mar 03, 2015 at 11:08:40PM +0100, Johannes Berg wrote:
> Instead of hand-writing a (positional) parser, use the argparse module.
> This also gets us nice help output.

Thanks, applied with number of fixes.

> diff --git a/tests/hwsim/vm/parallel-vm.py b/tests/hwsim/vm/parallel-vm.py
> @@ -257,26 +258,24 @@ def main():
> -    timestamp = int(time.time())

> +    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())

This move of timestamp initialization broke --codecov..

> -    if '-f' in sys.argv[idx:]:
> -        extra_args = sys.argv[idx:]
> +    if '-f' in args.params:
> +        extra_args = args.params

This doesn't work work argparse at least in its current form.
parallel-vm.py was able to pass some of the run-tests.py arguments to
the VM and anything starting with '-' seems to be broken with this
design.. That said, the earlier design was not exactly clean either and
most of the arguments are not really used. I ended up handling the ones
I care about (-f <modules..> and --long) separately here. If anything
else is needed, they can be handled similarly to allow this parsing to
be kept cleaner by not mixing two different command line uses.
diff mbox

Patch

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: