[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r20735: {projects} Check options and arguments with optparse. (projects/archives/trunk/exonerator)



Author: kloesing
Date: 2009-10-03 13:23:59 -0400 (Sat, 03 Oct 2009)
New Revision: 20735

Modified:
   projects/archives/trunk/exonerator/HOWTO
   projects/archives/trunk/exonerator/exonerator.py
Log:
Check options and arguments with optparse.


Modified: projects/archives/trunk/exonerator/HOWTO
===================================================================
--- projects/archives/trunk/exonerator/HOWTO	2009-10-03 15:40:31 UTC (rev 20734)
+++ projects/archives/trunk/exonerator/HOWTO	2009-10-03 17:23:59 UTC (rev 20735)
@@ -47,18 +47,21 @@
 
 - Run the script, providing it with the parameters it needs:
 
-  python exonerator.py <descriptor archive directory>
+  python exonerator.py [--archive=<descriptor archive directory>]
            <IP address in question>
            <timestamp, in UTC, formatted as YYYY-MM-DD hh:mm:ss>
            [<target address>[:<target port>]]
 
+  The --archive option defaults to data/ . In the following examples, it is
+  assumed that this default applies.
+
   Make sure that the timestamp is provided in UTC, which is similar to GMT,
   and not in your local timezone! Otherwise, results will very likely be
   wrong.
 
   A sample invocation might be:
 
-  $ python exonerator.py data/ 209.17.171.104 2009-08-15 16:05:00 \
+  $ python exonerator.py 209.17.171.104 2009-08-15 16:05:00
         209.85.129.104:80
 
 ---------------------------------------------------------------------------
@@ -117,34 +120,33 @@
 
 - Positive result of echelon1+2 being a relay:
 
-  $ python exonerator.py data/ 209.17.171.104 2009-08-15 16:05:00
+  $ python exonerator.py 209.17.171.104 2009-08-15 16:05:00
   $ java -cp .:bcprov-jdk16-143.jar ExoneraTor data/ 209.17.171.104 \
         2009-08-15 16:05:00
 
 - Positive result of echelon1+2 exiting to google.com on any port
 
-  $ python exonerator.py data/ 209.17.171.104 2009-08-15 16:05:00 \
-        209.85.129.104
+  $ python exonerator.py 209.17.171.104 2009-08-15 16:05:00 209.85.129.104
   $ java -cp .:bcprov-jdk16-143.jar ExoneraTor data/ 209.17.171.104 \
         2009-08-15 16:05:00 209.85.129.104
 
 - Positive result of echelon1+2 exiting to google.com on port 80
 
-  $ python exonerator.py data/ 209.17.171.104 2009-08-15 16:05:00 \
+  $ python exonerator.py 209.17.171.104 2009-08-15 16:05:00 \
         209.85.129.104:80
   $ java -cp .:bcprov-jdk16-143.jar ExoneraTor data/ 209.17.171.104 \
         2009-08-15 16:05:00 209.85.129.104:80
 
 - Negative result of echelon1+2 exiting to google.com, but not on port 25
 
-  $ python exonerator.py data/ 209.17.171.104 2009-08-15 16:05:00 \
+  $ python exonerator.py 209.17.171.104 2009-08-15 16:05:00 \
         209.85.129.104:25
   $ java -cp .:bcprov-jdk16-143.jar ExoneraTor data/ 209.17.171.104 \
         2009-08-15 16:05:00 209.85.129.104:25
 
 - Negative result with IP address of echelon1+2 changed in the last octet
 
-  $ python exonerator.py data/ 209.17.171.50 2009-08-15 16:05:00
+  $ python exonerator.py 209.17.171.50 2009-08-15 16:05:00
   $ java -cp .:bcprov-jdk16-143.jar ExoneraTor data/ 209.17.171.50 \
         2009-08-15 16:05:00
 

Modified: projects/archives/trunk/exonerator/exonerator.py
===================================================================
--- projects/archives/trunk/exonerator/exonerator.py	2009-10-03 15:40:31 UTC (rev 20734)
+++ projects/archives/trunk/exonerator/exonerator.py	2009-10-03 17:23:59 UTC (rev 20735)
@@ -5,35 +5,42 @@
 import os
 import sys
 import time
+from optparse import OptionParser
 
 # check parameters
-if len(sys.argv) not in (5, 6):
-    print "\nUsage: python exonerator.py <descriptor archive directory> " \
-          "<IP address in question> <timestamp, in UTC, formatted as " \
-          "YYYY-MM-DD hh:mm:ss> [<target address>[:<target port>]]\n"
-    sys.exit()
-archiveDirectory = sys.argv[1]
-if not os.path.isdir(archiveDirectory):
-    print "\nDescriptor archive directory %s does not exist or is not a " \
-          "directory.\n" % os.path.abspath(archiveDirectory)
-    sys.exit()
-archiveDirectory = os.path.dirname(archiveDirectory)
-relayIP = sys.argv[2]
-timestampStr = "%s %s" % (sys.argv[3], sys.argv[4])
+usage = "usage: %prog [options] <IP address in question> " \
+        "<timestamp, in UTC, formatted as YYYY-MM-DD hh:mm:ss> " \
+        "[<target address>[:<target port>]]"
+parser = OptionParser(usage=usage)
+parser.add_option("-a", "--archive", dest="archive", default="data/",
+                  help="descriptor archive directory")
+(options, args) = parser.parse_args()
+if len(args) not in (3, 4):
+    parser.error("incorrect number of arguments")
+if not os.path.isdir(options.archive):
+    parser.error("descriptor archive directory %s does not exist or is " \
+                 "not a directory." % os.path.abspath(archiveDirectory))
+archiveDirectory = os.path.dirname(options.archive)
+relayIP = args[0]
+timestampStr = "%s %s" % (args[1], args[2])
 os.environ['TZ'] = 'UTC'
 time.tzset()
-timestamp = time.strptime(timestampStr, "%Y-%m-%d %H:%M:%S")
+try:
+    timestamp = time.strptime(timestampStr, "%Y-%m-%d %H:%M:%S")
+except ValueError:
+    parser.error("incorrect time format: '%s'" % timestampStr)
 # if a target is given, parse address and possibly port part of it
 target = None
 targetIP = None
 targetPort = None
-if len(sys.argv) == 6:
-    target = sys.argv[5]
+if len(args) == 4:
+    target = args[3]
     targetParts = target.split(":")
     targetIP = targetParts[0]
     if len(targetParts) == 2:
         targetPort = targetParts[1]
     targetIPParts = targetIP.split(".")
+
 DELIMITER = "-----------------------------------------------------------" \
             "----------------"
 targetHelpStr = ""