Add runquery tool. Update setup.py.
authorRadek Brich <radek.brich@devl.cz>
Fri, 15 Feb 2013 16:38:23 +0100
changeset 67 78478a49c0bd
parent 66 3cd28308d67b
child 68 b0d972be2631
Add runquery tool. Update setup.py.
runquery.py
setup.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runquery.py	Fri Feb 15 16:38:23 2013 +0100
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3.2
+"""
+runquery
+
+Execute configured queries once.
+
+"""
+
+from pgtoolkit import toolbase, config
+
+import logging.handlers
+import time
+from datetime import datetime, timedelta
+
+
+class RunQueryTool(toolbase.ToolBase):
+    def __init__(self):
+        toolbase.ToolBase.__init__(self, name='runquery', desc='Run configured queries.')
+        self.parser.add_argument('target', nargs='?', metavar='target', type=str, help='Target database')
+        self.parser.add_argument('-c', dest='config', type=str, help='Additional config file (besides pgtoolkit.conf).')
+        self.parser.add_argument('-q', dest='queries', metavar='QUERY', nargs='*', help='Queries to run.')
+
+        self.config.add_argument('target', type=str, default=None)
+        self.config.add_argument('queries', type=list, default=[])
+        self.config.add_argument('log_path', type=str)
+
+        self.target_isolation_level = 'autocommit'
+
+        self.init()
+
+    def init(self):
+        toolbase.ToolBase.init(self)
+        if self.args.config:
+            self.config.load(self.args.config)
+        self.queries = self.args.queries or self.config.queries
+        if self.config.log_path:
+            self.init_file_logs(self.config.log_path)
+        self.prepare_conns(target = self.args.target or self.config.target)
+
+    def init_file_logs(self, path):
+        format = logging.Formatter('%(asctime)s %(levelname)-5s %(message)s', '%y-%m-%d %H:%M:%S')
+        handler = logging.handlers.TimedRotatingFileHandler(path+'/main.log', when='midnight', backupCount=5)
+        handler.setFormatter(format)
+        handler.setLevel(logging.DEBUG)
+        logging.getLogger('main').addHandler(handler)
+
+        format = logging.Formatter('%(asctime)s %(message)s', '%y-%m-%d %H:%M:%S')
+        handler = logging.handlers.TimedRotatingFileHandler(path+'/pgnotices.log', when='midnight', backupCount=5)
+        handler.setFormatter(format)
+        handler.setLevel(logging.DEBUG)
+        logging.getLogger('pgmanager_notices').addHandler(handler)
+
+    def main(self):
+        """Execute the queries."""
+        for q in self.queries:
+            self.log.info('%s', q)
+            with self.pgm.cursor('target') as curs:
+                rows = curs.execute(q)
+                self.log.info('Rows affected: %d', curs.rowcount)
+                self.log.info('Result: %r', rows)
+        self.log.info('Done')
+
+
+tool = RunQueryTool()
+tool.main()
+
--- a/setup.py	Wed Feb 13 15:30:37 2013 +0100
+++ b/setup.py	Fri Feb 15 16:38:23 2013 +0100
@@ -1,16 +1,17 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3.2
 
 from distutils.core import setup
 
 setup(
     name='pgtoolkit',
-    version='0.2.0',
+    version='0.3.0',
     description='Postgres utilities, build on top of psycopg2',
     author='Radek Brich',
     author_email='radek.brich@devl.cz',
     url='http://hg.devl.cz/pgtoolkit',
-#    download_url='',
     keywords=['postgresql', 'psycopg2', 'pool', 'keepalive'],
-    packages=['pgtoolkit'],
+    packages=['pgtoolkit', 'mytoolkit'],
+    scripts=['analyzeall.py', 'bigtables.py', 'listdepends.py', 'listserial.py',
+        'longqueries.py', 'loopquery.py', 'runquery.py', 'schemadiff.py', 'tablediff.py'],
     )