pgtoolkit/toolbase.py
author Radek Brich <radek.brich@devl.cz>
Fri, 25 Nov 2011 18:17:27 +0100
changeset 18 a9e12b7cc207
parent 17 f768a3529ee7
child 20 73f0d53fef6b
permissions -rw-r--r--
Fix MyManager. Add patch for MySQLdb and Python3.2. Add DelayedQuery.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
import argparse
13
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     2
import logging
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
     4
from pgtoolkit import pgmanager, config
13
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     5
from pgtoolkit.coloredformatter import ColoredFormatter
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     6
from pgtoolkit.highlight import highlight
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
class ToolBase:
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    def __init__(self, name, desc):
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
        self.parser = argparse.ArgumentParser(description=desc)
13
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    12
        self.parser.add_argument('-d', dest='debug', action='store_true',
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    13
            help='Debug mode - print database queries.')
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
        self.config = config.ConfigParser()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        self.config.add_argument('databases', type=dict)
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        self.config.add_argument('meta_db')
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.config.add_argument('meta_query')
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        self.pgm = pgmanager.get_instance()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
    def init(self):
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        self.config.load('pgtoolkit.conf')
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        self.args = self.parser.parse_args()
14
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    25
        self.init_logging()
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    26
        
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    27
    def init_logging(self):
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    28
        # logging
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    29
        handler = logging.StreamHandler()
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    30
        format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S')
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    31
        handler.setFormatter(format)
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    32
        handler.setLevel(logging.DEBUG)
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    33
        self.log = logging.getLogger('main')
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    34
        self.log.addHandler(handler)
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    35
        self.log.setLevel(logging.DEBUG)
a900bc629ecc TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    36
        
13
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    37
        if self.args.debug:
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    38
            handler = logging.StreamHandler()
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    39
            handler.setFormatter(format)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    40
            handler.setLevel(logging.DEBUG)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    41
            logger = logging.getLogger('pgmanager')
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    42
            logger.addHandler(handler)
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
17
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    44
    def buildconn(self, name, lookup_name):
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        with self.pgm.cursor('meta') as curs:
17
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    46
            curs.execute(self.config.meta_query, [lookup_name])
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
            row = curs.fetchone_dict()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
            curs.connection.commit()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
            
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
            if not row:
17
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    51
                raise Exception('Unknown database "%s"' % lookup_name)
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
            
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
            self.pgm.create_conn(name=name, **row)
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
17
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    55
    def prepareconns(self, *pgm_names):
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    56
        # use meta db
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    57
        if self.config.meta_db:
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    58
            self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
17
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    59
            for name in pgm_names:
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    60
                lookup_name = self.args.__dict__[name]
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    61
                self.buildconn(name, lookup_name)
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    62
            self.pgm.close_conn('meta')
17
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    63
        
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    64
        # use config dict 'databases'
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    65
        if self.config.databases:
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    66
            for name in pgm_names:
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    67
                lookup_name = self.args.__dict__[name]
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    68
                dsn = self.config.databases[lookup_name]
f768a3529ee7 ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    69
                self.pgm.create_conn(name=name, dsn=dsn)
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    70
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    71
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    72
class SimpleTool(ToolBase):
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    73
    def __init__(self, name, desc):
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    74
        ToolBase.__init__(self, name, desc)        
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    75
        self.parser.add_argument('target', metavar='target', type=str, help='Target database')
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    76
      
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    77
    def init(self):
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    78
        ToolBase.init(self)
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    79
        self.prepareconns('target')
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    80
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    81
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    82
class SrcDstTool(ToolBase):
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    83
    def __init__(self, name, desc):
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    84
        ToolBase.__init__(self, name, desc)        
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    85
        self.parser.add_argument('src', metavar='source', type=str, help='Source database')
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    86
        self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database')
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    87
      
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    88
    def init(self):
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    89
        ToolBase.init(self)
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    90
        self.prepareconns('src', 'dst')
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    91