[POS-commit] r3986 - stoq/trunk/bin

Johan Dahlin jdahlin at async.com.br
Wed Jul 26 11:52:52 BRT 2006


Author: jdahlin
Date: Wed Jul 26 11:52:52 2006
New Revision: 3986

Added:
   stoq/trunk/bin/stoqdbadmin   (contents, props changed)
Removed:
   stoq/trunk/bin/init-database

Log:
Move init-database functionallity to bin/stoqdbadmin script, fixes #2706
r=henrique



Added: stoq/trunk/bin/stoqdbadmin
==============================================================================
--- (empty file)
+++ stoq/trunk/bin/stoqdbadmin	Wed Jul 26 11:52:52 2006
@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+# -*- Mode: Python; coding: iso-8859-1 -*-
+# vi:si:et:sw=4:sts=4:ts=4
+
+##
+## Copyright (C) 2006 Async Open Source <http://www.async.com.br>
+## All rights reserved
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., or visit: http://www.gnu.org/.
+##
+## Author(s):   Johan Dahlin   <jdahlin at async.com.br>
+##
+""" stoqdbadmin: Command line utility to manipulate the database.  """
+
+import optparse
+import sys
+
+class StoqCommandHandler:
+    def __init__(self, prog_name):
+        self.prog_name = prog_name
+
+    def _read_config(self, options):
+        from stoq.lib.configparser import StoqConfig
+        from stoq.lib.startup import setup
+        config = StoqConfig(options.filename)
+        config.check_connection()
+        setup(config, options)
+        return config
+
+    def add_options(self, parser, cmd):
+
+        group = optparse.OptionGroup(parser, 'Database migration')
+        group.add_option('', '--dry-run',
+                         action="store_true",
+                         dest="dry",
+                         help='Dry run, do not commit')
+        parser.add_option_group(group)
+
+        func = getattr(self, 'opt_' + cmd, None)
+        if not func:
+            return
+
+        group = optparse.OptionGroup(parser, '%s options' % cmd)
+        func(parser, group)
+        parser.add_option_group(group)
+
+    def run_command(self, options, cmd, args):
+        func = getattr(self, 'cmd_' + cmd, None)
+        if func is None:
+            print "%s: Invalid command: `%s' type `%s help' for usage." % (
+                self.prog_name, cmd, self.prog_name)
+            return 1
+
+        nargs = func.func_code.co_argcount - 2
+        if len(args) < nargs:
+            raise SystemExit(
+                "%s: %s requires at least %d argument(s)" % (
+                self.prog_name, cmd, nargs))
+
+        return func(options, *args)
+
+    def cmd_help(self, options):
+        cmds = [attr[4:] for attr in dir(self) if attr.startswith('cmd_')]
+        cmds.sort()
+        cmds.remove('help')
+        print 'Available commands:'
+        for name in cmds:
+            print '  ', name
+        print
+        return 0
+
+    def cmd_init(self, options):
+        options.clean = True
+        config = self._read_config(options)
+
+        if options.create_examples:
+            from stoq.lib.startup import create_examples
+            create_examples(config)
+
+    def opt_init(self, parser, group):
+        group.add_option('-e', '--create-examples',
+                         action='store_true',
+                         dest='create_examples')
+
+def main(args):
+    pname = args[0]
+    args = args[1:]
+    if not args:
+        print "Type '%s help' for usage." % pname
+        return 1
+
+    cmd = args[0]
+    args = args[1:]
+
+    from stoq.lib.startup import get_option_parser
+    parser = get_option_parser()
+
+    handler = StoqCommandHandler(parser.get_prog_name())
+    handler.add_options(parser, cmd)
+    options, args = parser.parse_args(args)
+
+    return handler.run_command(options, cmd, args)
+
+if __name__ == '__main__':
+    try:
+        sys.exit(main(sys.argv))
+    except KeyboardInterrupt:
+        print 'Interrupted'


More information about the POS-commit mailing list