[rhmessaging-commits] rhmessaging commits: r1786 - in mgmt/cumin: python/cumin and 1 other directory.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Fri Mar 21 16:54:52 EDT 2008


Author: justi9
Date: 2008-03-21 16:54:51 -0400 (Fri, 21 Mar 2008)
New Revision: 1786

Modified:
   mgmt/cumin/bin/cumin
   mgmt/cumin/bin/cumin-admin
   mgmt/cumin/bin/cumin-bench
   mgmt/cumin/bin/cumin-test
   mgmt/cumin/python/cumin/__init__.py
   mgmt/cumin/python/cumin/util.py
Log:
Clean up the command-line option and config file parser.  Add support
for option summaries.  Use this to generate consistent command line
usage.

Take profiling out of cumin-test, since we don't need it there.



Modified: mgmt/cumin/bin/cumin
===================================================================
--- mgmt/cumin/bin/cumin	2008-03-21 18:49:34 UTC (rev 1785)
+++ mgmt/cumin/bin/cumin	2008-03-21 20:54:51 UTC (rev 1786)
@@ -5,25 +5,11 @@
 from cumin import *
 from cumin.util import *
 
-def usage():
-    print """Usage: cumin OPTIONS...
-Options:
-    -h, --help            Print this message
-    --data URI            Connect to database at URI
-                          (default postgesql://cumin@localhost/cumin)
-    --spec PATH           Use AMQP spec file at PATH
-    --addr ADDR           Run web server at address ADDR (default localhost)
-    --port PORT           Run web server on port PORT (default 8080)
-    --ssl yes             Serve pages using SSL (default no)
-    --debug               Enable debugging"""
-    
-    sys.exit(1)
-
-def do_main(home, data, spec, addr, port, ssl, debug):
-    app = Cumin(home, data, spec)
+def do_main(config):
+    app = Cumin(config.home, config.data, config.spec)
     log = logging.getLogger("cumin.config")
 
-    if debug:
+    if config.debug:
         app.enable_debug()
 
     try:
@@ -36,9 +22,9 @@
 
     app.init()
 
-    server = CuminServer(app, addr, port)
+    server = CuminServer(app, config.addr, config.port)
 
-    if ssl:
+    if config.ssl:
         cpath = os.path.join(home, "etc", "cumin.crt")
         kpath = os.path.join(home, "etc", "cumin.key")
 
@@ -61,27 +47,27 @@
         raise
 
 def main():
-    if "-h" in sys.argv or "--help" in sys.argv:
-        usage()
-
     config = CuminConfig()
-    config.add_param("addr", "s", "localhost")
-    config.add_param("port", "i", 8080)
-    config.add_param("ssl", "b", False)
+
+    summ = ("ADDR", "Run web server at address ADDR")
+    config.add_param("addr", str, "localhost", summ)
+
+    summ = ("PORT", "Run web server on port PORT")
+    config.add_param("port", int, 8080, summ)
+
+    summ = "Serve web pages using SSL"
+    config.add_param("ssl", bool, False, summ)
+
     config.init()
 
-    home = config.home
-    data = config.get("data")
-    spec = config.get("spec")
-    addr = config.get("addr")
-    port = config.get("port")
-    ssl = config.get("ssl")
-    debug = config.get("debug")
+    if "-h" in sys.argv or "--help" in sys.argv:
+        config.print_usage("cumin")
+        sys.exit(0)
 
-    if debug:
+    if config.debug:
         config.prt()
 
-    do_main(home, data, spec, addr, port, ssl, debug)
+    do_main(config)
 
 if __name__ == "__main__":
     try:

Modified: mgmt/cumin/bin/cumin-admin
===================================================================
--- mgmt/cumin/bin/cumin-admin	2008-03-21 18:49:34 UTC (rev 1785)
+++ mgmt/cumin/bin/cumin-admin	2008-03-21 20:54:51 UTC (rev 1786)
@@ -50,8 +50,8 @@
         home = os.path.normpath("/usr/share/cumin")
 
     config = Config()
-    config.add_param("data", "s", "postgresql://cumin@localhost/cumin")
-    config.add_param("force", "b", False)
+    config.add_param("data", str, "postgresql://cumin@localhost/cumin")
+    config.add_param("force", bool, False)
 
     config.load_file(os.path.join(home, "etc", "cumin.conf"))
     config.load_file(os.path.join(os.path.expanduser("~"), ".cumin.conf"))

Modified: mgmt/cumin/bin/cumin-bench
===================================================================
--- mgmt/cumin/bin/cumin-bench	2008-03-21 18:49:34 UTC (rev 1785)
+++ mgmt/cumin/bin/cumin-bench	2008-03-21 20:54:51 UTC (rev 1786)
@@ -8,16 +8,6 @@
 from cumin.test import *
 from cumin.util import *
 
-def usage():
-    print """Usage: cumin-bench OPTIONS...
-Options:
-    -h, --help            Print this message
-    --data URL            Connect to database at URL
-                          (default postgesql://cumin@localhost/cumin)
-    --hits HITS           Stop at HITS page hits (default 1000)
-    --profile"""
-    sys.exit(1)
-
 def do_main(home, data, spec, hits):
     app = Cumin(home, data, spec)
 
@@ -41,24 +31,30 @@
         pass
 
 def main():
-    if "-h" in sys.argv or "--help" in sys.argv:
-        usage()
-
     config = CuminConfig()
-    config.add_param("hits", "i", 1000)
-    config.add_param("profile", "b", False)
+
+    summ = ("COUNT", "Stop after COUNT page hits")
+    config.add_param("hits", int, 1000, summ)
+
+    summ = "Enable profiling"
+    config.add_param("profile", bool, False, summ)
+
     config.init()
 
-    if config.get("debug"):
+    if "-h" in sys.argv or "--help" in sys.argv:
+        config.print_usage("cumin-bench")
+        sys.exit(0)
+
+    if config.debug:
         config.prt()
 
     home = config.home
-    data = config.get("data")
-    spec = config.get("spec")
-    hits = config.get("hits")
-    profile = config.get("profile")
+    data = config.data
+    spec = config.spec
+    hits = config.hits
+    profile = config.profile
 
-    if profile:
+    if config.profile:
         from profile import Profile
         from pstats import Stats
 

Modified: mgmt/cumin/bin/cumin-test
===================================================================
--- mgmt/cumin/bin/cumin-test	2008-03-21 18:49:34 UTC (rev 1785)
+++ mgmt/cumin/bin/cumin-test	2008-03-21 20:54:51 UTC (rev 1786)
@@ -2,29 +2,15 @@
 
 import sys, os
 from time import time
-from wooly.devel import BenchmarkHarness
 
 from cumin import *
 from cumin.test import *
 from cumin.util import *
 
-def usage():
-    print """Usage: cumin-test OPTIONS...
-Options:
-    -h, --help            Print this message
-    --data URI            Connect to database at URI
-                          (default postgesql://cumin@localhost/cumin)
-    --spec PATH           Use AMQP spec file at PATH
-    --broker ADDRESS      Register new test broker at ADDRESS
-                          (default localhost:5672)
-    --profile yes         Enable profiling
-    --debug yes           Enable debugging"""
-    sys.exit(1)
+def do_main(config):
+    app = Cumin(config.home, config.data, config.spec)
 
-def do_main(home, data, spec, broker_host, broker_port, debug):
-    app = Cumin(home, data, spec)
-
-    if debug or bench:
+    if config.debug:
         app.enable_debug()
 
     try:
@@ -37,64 +23,30 @@
 
     app.init()
 
-    env = TestEnvironment(app, broker_host, broker_port)
+    host, port = parse_broker_addr(config.broker)
+
+    env = TestEnvironment(app, host, port)
     env.init();
 
     session = env.run_test(MainTest(env))
     session.report(sys.stdout)
 
 def main():
-    if "-h" in sys.argv or "--help" in sys.argv:
-        usage()
-
     config = CuminConfig()
-    config.add_param("broker", "s", "localhost:5672")
-    config.add_param("profile", "b", False)
-    config.init()
 
-    if config.get("debug"):
-        config.prt()
+    summ = ("ADDR", "Register new test broker at ADDR")
+    config.add_param("broker", str, "localhost:5672", summ)
 
-    home = config.home
-    data = config.get("data")
-    spec = config.get("spec")
-    broker = config.get("broker")
-    debug = config.get("debug")
-    profile = config.get("profile")
+    config.init()
 
-    host, port = parse_broker_addr(broker)
+    if "-h" in sys.argv or "--help" in sys.argv:
+        config.print_usage("cumin-test")
+        sys.exit(0)
 
-    if profile:
-        from profile import Profile
-        from pstats import Stats
+    if config.debug:
+        config.prt()
 
-        prof = Profile()
+    do_main(config)
 
-        try:
-            statement = "do_main('%s', '%s', '%s', %s, %i, %r)" \
-                % (home, data, host, port, debug)
-
-	    prof.run(statement)
-
-	    raise KeyboardInterrupt()
-	except KeyboardInterrupt:
-            file = "/tmp/cumin-test-stats"
-
-            prof.dump_stats(file)
-
-	    stats = Stats(file)
-
-	    stats.sort_stats("cumulative").print_stats(15)
-	    stats.sort_stats("time").print_stats(15)
-
-            stats.print_callees("wooly/__init__.*\\(marshal_url_vars\\)")
-            stats.print_callees("wooly/__init__.*\\(path\\)")
-            stats.print_callees("wooly/__init__.*\\(get\\)")
-            stats.print_callees("wooly/__init__.*\\(render\\)")
-
-	    stats.strip_dirs()
-    else:
-        do_main(home, data, spec, host, port, debug)
-
 if __name__ == "__main__":
     main()

Modified: mgmt/cumin/python/cumin/__init__.py
===================================================================
--- mgmt/cumin/python/cumin/__init__.py	2008-03-21 18:49:34 UTC (rev 1785)
+++ mgmt/cumin/python/cumin/__init__.py	2008-03-21 20:54:51 UTC (rev 1786)
@@ -141,15 +141,27 @@
         sdef = os.path.normpath("/usr/share/amqp/amqp.0-10-preview.xml")
         spec = os.environ.get("AMQP_SPEC", sdef)
 
-        self.add_param("debug", "b", False)
-        self.add_param("data", "s", "postgresql://cumin@localhost/cumin")
-        self.add_param("spec", "s", spec)
-        self.add_param("log", "s", os.path.join(self.home, "log", "cumin.log"))
+        summ = "Print this message"
+        self.add_param("help", bool, False, summ)
 
+        summ = ("URI", "Connect to database at URI")
+        self.add_param("data", str, "postgresql://cumin@localhost/cumin", summ)
+
+        summ = ("PATH", "Use AMQP spec file at PATH")
+        self.add_param("spec", str, spec, summ)
+
+        lpath = os.path.join(self.home, "log", "cumin.log")
+        summ = ("PATH", "Log to file at PATH")
+        self.add_param("log",  str, lpath, summ)
+
+        summ = "Enable debug mode"
+        self.add_param("debug", bool, False, summ)
+
     def init(self):
         root = logging.getLogger()
         root.setLevel(logging.NOTSET)
 
+        self.load_defaults()
         self.load_args(sys.argv)
 
         h = self.get_console_handler()
@@ -162,8 +174,8 @@
         h = self.get_console_handler()
         root.addHandler(h)
 
-        h = logging.FileHandler(self.get("log"))
-        if self.get("debug"):
+        h = logging.FileHandler(self.log)
+        if self.debug:
             h.setLevel(logging.DEBUG)
         else:
             h.setLevel(logging.INFO)
@@ -171,7 +183,7 @@
 
     def get_console_handler(self):
         h = logging.StreamHandler()
-        if self.get("debug"):
+        if self.debug:
             h.setLevel(logging.DEBUG)
         else:
             h.setLevel(logging.ERROR)

Modified: mgmt/cumin/python/cumin/util.py
===================================================================
--- mgmt/cumin/python/cumin/util.py	2008-03-21 18:49:34 UTC (rev 1785)
+++ mgmt/cumin/python/cumin/util.py	2008-03-21 20:54:51 UTC (rev 1786)
@@ -39,57 +39,45 @@
         self.id = id
 
 class Config(object):
-    log = getLogger("cumin.config")
+    __log = getLogger("cumin.config")
 
     def __init__(self):
         self.__params = list()
-        self.__param_specs = dict() # param name => (type, default)
-        self.__values = dict()
+        self.__params_by_name = dict()
 
-    def add_param(self, name, type, default):
-        self.__params.append(name)
-        self.__param_specs[name] = (type, default)
+    def add_param(self, name, type, default=None, summary=None):
+        param = self.Parameter(name, type, default, summary)
+        self.__params.append(param)
+        self.__params_by_name[name] = param
 
-    def set(self, name, value):
-        self.__values[name] = value
-        return value
-
-    def unmarshal(self, name, string):
-        type, default = self.__param_specs[name]
-
-        if type == "i":
+    def unmarshal(self, param, string):
+        if param.type is int:
             value = int(string)
-        elif type == "b":
+        elif param.type is bool:
             value = string is None or \
                 string.lower() in ("y", "yes", "t", "true", "1")
-        elif type == "s":
+        elif param.type is str:
             value = string
         else:
-            raise Exception("Invalid type '%s'" % type)
+            raise Exception("Invalid type %s" % type)
 
         return value
 
-    def get(self, name):
-        if name not in self.__params:
-            raise Exception("Parameter '%s' not found" % name)
+    def load_defaults(self):
+        for param in self.__params:
+            if hasattr(self, param.name):
+                raise Exception("Parameter '%s' already present" % name)
 
-        type, default = self.__param_specs[name]
+            setattr(self, param.name, param.default)
 
-        if name in self.__values:
-            value = self.__values.get(name)
-        else:
-            value = default
-
-        return value
-
     def load_file(self, file):
         conf = SafeConfigParser()
         found = conf.read(file)
 
         if found:
-            self.log.info("Found config file '%s' and read it" % file)
+            self.__log.info("Read config file '%s'" % file)
         else:
-            self.log.info("Config file '%s' not found" % file)
+            self.__log.info("Config file '%s' not found" % file)
 
         params = dict()
 
@@ -115,30 +103,54 @@
 
     def load_string_params(self, params):
         for name, value in params.items():
-            if name in self.__params:
-                self.set(name, self.unmarshal(name, value))
+            param = self.__params_by_name.get(name)
+
+            if param:
+                setattr(self, param.name, self.unmarshal(param, value))
             else:
-                self.log.info("Ignoring unrecognized parameter '%s'" % name)
+                self.__log.info("Ignoring unrecognized parameter '%s'" % name)
 
-    def require(self, params):
-        missing = list()
+    def print_usage(self, cmd):
+        print "Usage: %s OPTIONS..." % cmd
+        print "Options:"
 
-        for param in params:
-            if param not in self.__values:
-                missing.append(param)
+        for param in self.__params:
+            if param.summary:
+                if type(param.summary) is str:
+                    opt = "--%s" % param.name
+                    text = param.summary
+                else:
+                    opt = "--%s %s" % (param.name, param.summary[0])
+                    text = param.summary[1]
 
-        return missing
+                main = "    %-15s  %s" % (opt, text)
+                extra = ""
 
+                if param.default not in (None, False):
+                    extra = "(default %s)" % param.default
+
+                if len(main) + len(extra) > 79:
+                    print main
+                    print "    %15s  %s" % ("", extra)
+                else:
+                    print main, extra
+
     def prt(self):
         print "Configuration:"
 
         for param in self.__params:
-            type, default = self.__param_specs[param]
-            value = self.get(param)
+            value = getattr(self, param.name)
 
-            if param in self.__values:
+            if value == param.default:
+                flag = " [default]"
+            else:
                 flag = ""
-            else:
-                flag = " [default]"
 
-            print "    %s = %s%s" % (param, value, flag)
+            print "    %s = %s%s" % (param.name, value, flag)
+
+    class Parameter(object):
+        def __init__(self, name, type, default=None, summary=None):
+            self.name = name
+            self.type = type
+            self.default = default
+            self.summary = summary




More information about the rhmessaging-commits mailing list