Commits

Rufus Pollock committed 14c4160

[cli][s]: create paster command 'db' for handling db stuff (create, clean etc).

Comments (0)

Files changed (2)

commentonit/lib/cli.py

+import os
+
+import paste.script
+
+class CkanCommand(paste.script.command.Command):
+    parser = paste.script.command.Command.standard_parser(verbose=True)
+    parser.add_option('-c', '--config', dest='config',
+            default='development.ini', help='Config file to use.')
+    default_verbosity = 1
+    group_name = 'commentonit'
+
+    def _load_config(self):
+        from paste.deploy import appconfig
+        from commentonit.config.environment import load_environment
+        if not self.options.config:
+            msg = 'No config file supplied'
+            raise self.BadCommand(msg)
+        self.filename = os.path.abspath(self.options.config)
+        conf = appconfig('config:' + self.filename)
+        load_environment(conf.global_conf, conf.local_conf)
+
+    def _setup_app(self):
+        cmd = paste.script.appinstall.SetupCommand('setup-app') 
+        cmd.run([self.filename]) 
+
+
+class ManageDb(CkanCommand):
+    '''Perform various tasks on the database.
+    '''
+    summary = __doc__.split('\n')[0]
+    usage = __doc__
+    max_args = None
+    min_args = 1
+
+    def command(self):
+        self._load_config()
+        from commentonit import model
+
+        cmd = self.args[0]
+        if cmd == 'create':
+            model.repo.create_db()
+        elif cmd == 'init':
+            model.repo.init_db()
+        elif cmd == 'clean':
+            model.repo.clean_db()
+        else:
+            print 'Command %s not recognized' % cmd
+
 
     [paste.app_install]
     main = pylons.util:PylonsInstaller
+
+    [paste.paster_command]
+    db = commentonit.lib.cli:ManageDb
     """,
 )