File: pglistener.py

package info (click to toggle)
pglistener 4
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 224 kB
  • ctags: 132
  • sloc: python: 594; perl: 364; sh: 120; sql: 89; makefile: 2
file content (89 lines) | stat: -rw-r--r-- 2,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Postgres Listener, writes out flatfiles when SQL NOTIFYs are triggered
# Copyright (C) 2005 Rob Bradford <rob@robster.org.uk>
# Copyright (C) 2007-8 Robert McQueen <robert.mcqueen@bluelinux.co.uk>
# Copyright (C) 2008 Collabora Ltd.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import errno
import os
import select
import stat
import syslog

class PgListener:
    def __init__(self, options):
        """Creates object and make connection to server and setup a cursor for
        the connection."""
        self.sleeptime = 0
        self.options = options
        self.name = options['name']
        self.dsn = options['dsn']
        self.query = options['query']
        self.notifications = options['notifications']
        self.destination = options['destination']
        self.conn = None

    def err(self, msg):
        syslog.syslog(syslog.LOG_ERR, "%s: %s" % (self.name, msg))

    def info(self, msg):
        syslog.syslog(syslog.LOG_INFO, "%s: %s" % (self.name, msg))

    def do_format(self, row):
        """Apply the format string against a row."""
        return self.options['format'].replace('\\n', '\n') % row

    def write_file(self, path, f):
        tmp = path + "~"
        fh = open(tmp, "w+")

        try:
            f(fh)
        finally:
            fh.close()

        self.do_perms(path, tmp)
        os.rename(tmp, path)

    def do_write(self, result, target):
        """For each row in the result set apply the format and write it out to
        the target file."""

        def write_results(fh):
            for row in result:
                fh.write(self.do_format(row))

        self.write_file(target, write_results)

    def do_perms(self, source, target):
        """Apply the same file permissions from the original destination
        version of the file to the target."""

        if os.path.exists(source):
            orig = os.stat(source)

            try:
                os.chmod(target, orig[stat.ST_MODE])
            except select.error, (errno, strerror):
                self.err("failed to chmod new file: %s" % strerror)

    def do_posthooks(self):
        """Execute all the provided hooks."""

        for hook in self.options.get('posthooks', '').strip().splitlines():
            self.info("executing: %s" % hook)
            os.system(hook)