~ulrith/mailman/russian-2.1.20

« back to all changes in this revision

Viewing changes to cron/cull_bad_shunt

  • Committer: Mark Sapiro
  • Date: 2008-06-06 18:13:09 UTC
  • mfrom: (1073.1.2 cull_bad_shunt)
  • Revision ID: mark@msapiro.net-20080606181309-sub898y3deolqu6w
Merged the cull_bad_shunt branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! @PYTHON@
 
2
#
 
3
# Copyright (C) 2008 by the Free Software Foundation, Inc.
 
4
#
 
5
# This program is free software; you can redistribute it and/or
 
6
# modify it under the terms of the GNU General Public License
 
7
# as published by the Free Software Foundation; either version 2
 
8
# of the License, or (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
18
# USA.
 
19
 
 
20
"""Cull bad and shunt queues, recommended once per day.
 
21
 
 
22
This script goes through the 'bad' and 'shunt' queue directories and,
 
23
if mm_cfg.BAD_SHUNT_STALE_AFTER is > 0, it removes all files more than
 
24
that many seconds old.
 
25
 
 
26
If mm_cfg.BAD_SHUNT_ARCHIVE_DIRECTORY is a writable directory, the old
 
27
files are moved there. Otherwise they are deleted.
 
28
 
 
29
Only regular files immediately subordinate to the 'bad' and 'shunt'
 
30
directories are processed. Anything else is skipped.
 
31
 
 
32
Usage: %(PROGRAM)s [options]
 
33
 
 
34
Options:
 
35
    -h / --help
 
36
        Print this message and exit.
 
37
"""
 
38
 
 
39
import os
 
40
import sys
 
41
import stat
 
42
import time
 
43
import errno
 
44
import getopt
 
45
 
 
46
import paths
 
47
# mm_cfg must be imported before the other modules, due to the side-effect of
 
48
# it hacking sys.paths to include site-packages.  Without this, running this
 
49
# script from cron with python -S will fail.
 
50
from Mailman import mm_cfg
 
51
from Mailman.i18n import _
 
52
 
 
53
# Work around known problems with some RedHat cron daemons
 
54
import signal
 
55
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
 
56
 
 
57
PROGRAM = sys.argv[0]
 
58
 
 
59
 
 
60
 
 
61
def usage(code, msg=''):
 
62
    if code:
 
63
        fd = sys.stderr
 
64
    else:
 
65
        fd = sys.stdout
 
66
    print >> fd, _(__doc__)
 
67
    if msg:
 
68
        print >> fd, msg
 
69
    sys.exit(code)
 
70
 
 
71
 
 
72
 
 
73
def main():
 
74
    try:
 
75
        opts, args = getopt.getopt(
 
76
            sys.argv[1:], 'h', ['help'])
 
77
    except getopt.error, msg:
 
78
        usage(1, msg)
 
79
 
 
80
    if args:
 
81
        usage(1)
 
82
 
 
83
    if mm_cfg.BAD_SHUNT_STALE_AFTER <= 0:
 
84
        # Nothing to do
 
85
        return
 
86
    now = time.time()
 
87
    old = now - float(mm_cfg.BAD_SHUNT_STALE_AFTER)
 
88
    os.stat_float_times(True)
 
89
    adir = mm_cfg.BAD_SHUNT_ARCHIVE_DIRECTORY
 
90
    if (adir and os.access(adir, os.W_OK | os.X_OK)
 
91
             and stat.S_ISDIR(os.stat(adir).st_mode)):
 
92
        pass
 
93
    else:
 
94
        if adir:
 
95
            print >>sys.stderr, '%s: archive directory %s not usable.' % (
 
96
                    PROGRAM, adir)
 
97
        adir = None
 
98
    for qdir in (mm_cfg.BADQUEUE_DIR, mm_cfg.SHUNTQUEUE_DIR):
 
99
        try:
 
100
            names = os.listdir(qdir)
 
101
        except OSError, e:
 
102
            if e.errno <> errno.ENOENT:
 
103
                # OK if qdir doesn't exist, else
 
104
                raise
 
105
            continue
 
106
        for name in names:
 
107
            pathname = os.path.join(qdir, name)
 
108
            pstat = os.stat(pathname)
 
109
            if not stat.S_ISREG(pstat.st_mode):
 
110
                # Not a regular file
 
111
                continue
 
112
            if pstat.st_mtime > old:
 
113
                # File is new
 
114
                continue
 
115
            if adir:
 
116
                os.rename(pathname, os.path.join(adir, name))
 
117
            else:
 
118
                os.remove(pathname)
 
119
 
 
120
 
 
121
 
 
122
if __name__ == '__main__':
 
123
    main()