aboutsummaryrefslogtreecommitdiffstats
path: root/cron/cull_bad_shunt
blob: eaef0a6946b4d98f5e325a2ce021969b4dfad19e (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! @PYTHON@
#
# Copyright (C) 2008-2018 by the Free Software Foundation, Inc.
#
# 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.

"""Cull bad and shunt queues, recommended once per day.

This script goes through the 'bad' and 'shunt' queue directories and,
if mm_cfg.BAD_SHUNT_STALE_AFTER is > 0, it removes all files more than
that many seconds old.

If mm_cfg.BAD_SHUNT_ARCHIVE_DIRECTORY is a writable directory, the old
files are moved there. Otherwise they are deleted.

Only regular files immediately subordinate to the 'bad' and 'shunt'
directories are processed. Anything else is skipped.

Usage: %(PROGRAM)s [options]

Options:
    -h / --help
        Print this message and exit.
"""

import os
import sys
import stat
import time
import errno
import getopt

import paths
# mm_cfg must be imported before the other modules, due to the side-effect of
# it hacking sys.paths to include site-packages.  Without this, running this
# script from cron with python -S will fail.
from Mailman import mm_cfg
from Mailman.i18n import _

# Work around known problems with some RedHat cron daemons
import signal
signal.signal(signal.SIGCHLD, signal.SIG_DFL)

PROGRAM = sys.argv[0]



def usage(code, msg=''):
    if code:
        fd = sys.stderr
    else:
        fd = sys.stdout
    print >> fd, _(__doc__)
    if msg:
        print >> fd, msg
    sys.exit(code)



def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'h', ['help'])
    except getopt.error, msg:
        usage(1, msg)

    if args:
        usage(1)
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)

    if mm_cfg.BAD_SHUNT_STALE_AFTER <= 0:
        # Nothing to do
        return
    now = time.time()
    old = now - float(mm_cfg.BAD_SHUNT_STALE_AFTER)
    os.stat_float_times(True)
    adir = mm_cfg.BAD_SHUNT_ARCHIVE_DIRECTORY
    if (adir and os.access(adir, os.W_OK | os.X_OK)
             and stat.S_ISDIR(os.stat(adir).st_mode)):
        pass
    else:
        if adir:
            print >>sys.stderr, '%s: archive directory %s not usable.' % (
                    PROGRAM, adir)
        adir = None
    for qdir in (mm_cfg.BADQUEUE_DIR, mm_cfg.SHUNTQUEUE_DIR):
        try:
            names = os.listdir(qdir)
        except OSError, e:
            if e.errno <> errno.ENOENT:
                # OK if qdir doesn't exist, else
                raise
            continue
        for name in names:
            pathname = os.path.join(qdir, name)
            pstat = os.stat(pathname)
            if not stat.S_ISREG(pstat.st_mode):
                # Not a regular file
                continue
            if pstat.st_mtime > old:
                # File is new
                continue
            if adir:
                os.rename(pathname, os.path.join(adir, name))
            else:
                os.remove(pathname)



if __name__ == '__main__':
    main()