소스 검색

Create StoreSeqAction for argument lists

Hamilton Turner 10 년 전
부모
커밋
d5295f50f4
2개의 변경된 파일69개의 추가작업 그리고 2개의 파일을 삭제
  1. 34 2
      toolset/run-tests.py
  2. 35 0
      toolset/test/test-run-tests.py

+ 34 - 2
toolset/run-tests.py

@@ -4,6 +4,8 @@ import ConfigParser
 import sys
 import os
 import multiprocessing
+import itertools
+import copy
 import subprocess
 from pprint import pprint 
 from benchmark.benchmarker import Benchmarker
@@ -14,6 +16,30 @@ from setup.linux import setup_util
 from colorama import init
 init()
 
+class StoreSeqAction(argparse.Action):
+  '''Helper class for parsing a sequence from the command line'''
+  def __init__(self, option_strings, dest, nargs=None, **kwargs):
+     super(StoreSeqAction, self).__init__(option_strings, dest, type=str, **kwargs)
+  def __call__(self, parser, namespace, values, option_string=None):
+    setattr(namespace, self.dest, self.parse_seq(values))
+  def parse_seq(self, argument):
+    try:
+      return [int(argument)]
+    except ValueError:
+      pass
+    if ":" in argument: # 
+      try:
+        (start,step,end) = argument.split(':')
+      except ValueError: 
+        print "Invalid: %s" % argument
+        print "Requires start:step:end, e.g. 1:2:10"
+        raise
+      result = range(int(start), int(end), int(step))
+    else:  # 1,2,3,7
+      result = argument.split(',')
+    return [abs(int(item)) for item in result]
+
+
 ###################################################################################################
 # Main
 ###################################################################################################
@@ -81,9 +107,15 @@ def main(argv=None):
     ##########################################################
     # Set up argument parser
     ##########################################################
-    parser = argparse.ArgumentParser(description='Run the Framework Benchmarking test suite.',
+    parser = argparse.ArgumentParser(description="Install or run the Framework Benchmarks test suite.",
         parents=[conf_parser],
-        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
+        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
+        epilog='''If an argument includes (type int-sequence), then it accepts integer lists in multiple forms. 
+        Using a single number e.g. 5 will create a list [5]. Using commas will create a list containing those 
+        values e.g. 1,3,6 creates [1, 3, 6]. Using three colon-separated numbers of start:step:end will create a 
+        list, using the semantics of python's range function, e.g. 1:3:15 creates [1, 4, 7, 10, 13] while 
+        0:1:5 creates [0, 1, 2, 3, 4]
+        ''')
 
     # SSH options
     parser.add_argument('-s', '--server-host', default=serverHost, help='The application server.')

+ 35 - 0
toolset/test/test-run-tests.py

@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+import argparse
+import ConfigParser
+import sys
+import os
+import multiprocessing
+import itertools
+import copy
+import subprocess
+from pprint import pprint 
+from benchmark.benchmarker import Benchmarker
+from setup.linux.unbuffered import Unbuffered
+from setup.linux import setup_util
+
+from run-tests import StoreSeqAction
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--foo', action=StoreSeqAction)
+tests = ["1", "1,", "0.23",                       # Single numbers
+        "1,5,7", "1,2,-3", "1,1000,12,1,1,1,1,1", # Lists
+        "1:2:10", "1:2", "10:-2:0",               # Sequences
+        "1,2:1:5"                                 # Complex
+]
+for test in tests:
+  try:
+    t = "--foo %s" % test
+    print "Testing %s" % test
+    print "  %s" % parser.parse_args(t.split())
+    print "  Success"
+  except Exception as e: 
+    print "  Exception! %s" % e
+    continue
+
+# vim: sw=2