You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ra...@apache.org on 2008/05/06 22:19:22 UTC

svn commit: r653904 - /incubator/qpid/trunk/qpid/python/commands/qpid-queue-stats

Author: rajith
Date: Tue May  6 13:19:22 2008
New Revision: 653904

URL: http://svn.apache.org/viewvc?rev=653904&view=rev
Log:
This patch was attached to QPID-953.
It allows to specify a comma separated list of queue names to filter with -f flag.
Also I removed getopt and added optparse as it provides a more easy way of handling CLI functionality
including a free help function.

Modified:
    incubator/qpid/trunk/qpid/python/commands/qpid-queue-stats

Modified: incubator/qpid/trunk/qpid/python/commands/qpid-queue-stats
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/commands/qpid-queue-stats?rev=653904&r1=653903&r2=653904&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/commands/qpid-queue-stats (original)
+++ incubator/qpid/trunk/qpid/python/commands/qpid-queue-stats Tue May  6 13:19:22 2008
@@ -20,8 +20,9 @@
 #
 
 import os
-import getopt
+import optparse
 import sys
+import re
 import socket
 import qpid
 from threading       import Condition
@@ -31,21 +32,6 @@
 from qpid.util       import connect
 from time            import sleep
 
-defspecpath  = "/usr/share/amqp/amqp.0-10.xml"
-specpath     = defspecpath
-host         = "localhost"
-
-def Usage ():
-    print "Usage:  qpid-queue-stats [OPTIONS]"
-    print
-    print "Options:"
-    print "    -a <broker-addr>     default: localhost"
-    print "         broker-addr is in the form:   hostname | ip-address [:<port>]"
-    print "         ex:  localhost, 10.1.1.7:10000, broker-host:10000"
-    print "    -s <amqp-spec-file>  default:", defspecpath
-    print
-    sys.exit (1)
-
 class Broker:
     def __init__ (self, text):
         colon = text.find (":")
@@ -78,13 +64,15 @@
         self.src     = None
         self.broker  = None
         self.objects = {}
+        self.filter  = None   
+        self.specpath="/usr/share/amqp/amqp.0-10.xml"
 
     def SetBroker (self, broker):
         self.broker = broker
 
     def ConnectToBroker (self):
         try:
-            self.spec     = qpid.spec.load (specpath)
+            self.spec     = qpid.spec.load (self.specpath)
             self.sessionId = "%s.%d" % (os.uname()[1], os.getpid())
             self.conn     = Connection (connect (self.broker.host, self.broker.port), self.spec)
             self.conn.start ()
@@ -94,6 +82,12 @@
             print "Connect Error:", e
             exit (1)
 
+    def setFilter(self,filter):
+        self.filter = filter
+
+    def setSpecpath(self,spec):
+        self.specpath = spec
+
     def Disconnect (self):
         self.mclient.removeChannel (self.mchannel)
 
@@ -120,6 +114,16 @@
             self.objects[obj.id] = (name, obj, None)
             return
 
+        if len(self.filter) > 0 :
+           match = False
+                    
+           for x in self.filter:
+              if x.match(name):                 
+                 match = True
+                 break
+           if match == False:
+              return
+
         if last == None:
             lastSample = first
         else:
@@ -148,19 +152,27 @@
 ##
 ## Main Program
 ##
+def main():
+  p = optparse.OptionParser()
+  p.add_option('--broker-address','-a', default='localhost' , help='broker-addr is in the form:   hostname | ip-address [:<port>] \n ex:  localhost, 10.1.1.7:10000, broker-host:10000')
+  p.add_option('--amqp-spec-file','-s', default='"/usr/share/amqp/amqp.0-10.xml', help='the path to the amqp spec file')
+  p.add_option('--filter','-f' ,default=None ,help='a list of comma separated queue names (regex are accepted) to show')
+
+  options, arguments = p.parse_args()
+
+  host = options.broker_address
+  specpath = options.amqp_spec_file
+  filter = []
+  if options.filter != None:
+    for s in options.filter.split(","):
+        filter.append(re.compile(s))
+
+  bm  = BrokerManager ()
+  bm.SetBroker (Broker (host))
+  bm.setSpecpath(specpath)
+  bm.setFilter(filter)
+  bm.Display()
+ 
+if __name__ == '__main__':
+  main()
 
-try:
-    (optlist, cargs) = getopt.getopt (sys.argv[1:], "s:a:")
-except:
-    Usage ()
-
-for opt in optlist:
-    if opt[0] == "-s":
-        specpath = opt[1]
-    if opt[0] == "-a":
-        host = opt[1]
-
-nargs = len (cargs)
-bm  = BrokerManager ()
-bm.SetBroker (Broker (host))
-bm.Display ()