[Python-checkins] r68105 - sandbox/trunk/iobench/iobench.py

antoine.pitrou python-checkins at python.org
Wed Dec 31 18:19:29 CET 2008


Author: antoine.pitrou
Date: Wed Dec 31 18:19:29 2008
New Revision: 68105

Log:
use OptionParser, and fix a test which can't be run in text mode (because of relative seeks)



Modified:
   sandbox/trunk/iobench/iobench.py

Modified: sandbox/trunk/iobench/iobench.py
==============================================================================
--- sandbox/trunk/iobench/iobench.py	(original)
+++ sandbox/trunk/iobench/iobench.py	Wed Dec 31 18:19:29 2008
@@ -7,6 +7,7 @@
 import sys
 import hashlib
 import functools
+from optparse import OptionParser
 
 out = sys.stdout
 
@@ -104,7 +105,7 @@
     for i in xrange(0, size - 1, 1000):
         f.seek(i, 0)
 
- at with_open_mode("r")
+ at with_open_mode("rb")
 @with_sizes("medium")
 def read_seek_bytewise(f):
     """ alternate read & seek one unit """
@@ -112,7 +113,7 @@
     while f.read(1):
         f.seek(1, 1)
 
- at with_open_mode("r")
+ at with_open_mode("rb")
 @with_sizes("medium")
 def read_seek_blockwise(f):
     """ alternate read & seek 1000 units """
@@ -296,8 +297,6 @@
     }
     file_sizes = list(get_file_sizes())
 
-    options = options or "rwbt"
-
     if "b" in options:
         print("Binary unit = one byte")
     if "t" in options:
@@ -393,15 +392,44 @@
             f.write(head)
             f.write(tail)
 
-def main(options=None):
+def main():
+    usage = "usage: %prog [-h|--help] [options]"
+    parser = OptionParser(usage=usage)
+    parser.add_option("-b", "--binary",
+                      action="store_true", dest="binary", default=False,
+                      help="run binary I/O tests")
+    parser.add_option("-t", "--text",
+                      action="store_true", dest="text", default=False,
+                      help="run text I/O tests")
+    parser.add_option("-r", "--read",
+                      action="store_true", dest="read", default=False,
+                      help="run read tests")
+    parser.add_option("-w", "--write",
+                      action="store_true", dest="write", default=False,
+                      help="run write & modify tests")
+    options, args = parser.parse_args()
+    if args:
+        parser.error("unexpected arguments")
+    
+    test_options = ""
+    if options.read:
+        test_options += "r"
+    if options.write:
+        test_options += "w"
+    elif not options.read:
+        test_options += "rw"
+    if options.text:
+        test_options += "t"
+    if options.binary:
+        test_options += "b"
+    elif not options.text:
+        test_options += "tb"
+
     prepare_files()
-    run_all_tests(options)
+    run_all_tests(test_options)
 
 if __name__ == "__main__":
-    if len(sys.argv) > 1:
-        main(sys.argv[1])
-    else:
-        main()
+    main()
 
 
 # -- This part to exercise text reading. Don't change anything! --


More information about the Python-checkins mailing list