~xav0989/ubuntu/vivid/mailman/ubuntu-logo

« back to all changes in this revision

Viewing changes to cron/cull_bad_shunt

  • Committer: Bazaar Package Importer
  • Author(s): Thijs Kinkhorst
  • Date: 2008-08-11 16:06:19 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080811160619-ymr837d03w2qvnh9
Tags: 1:2.1.11-3
* Updated Catalan debconf translation, thanks David Planella Molas
  (Closes: #494110).
* Added patch 68_update_catalan to update Catalan program translation,
  thanks Jordi Mallach (Closes: #492297).
* Add a README.source file referring to quilt.

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
    for opt, arg in opts:
 
83
        if opt in ('-h', '--help'):
 
84
            usage(0)
 
85
 
 
86
    if mm_cfg.BAD_SHUNT_STALE_AFTER <= 0:
 
87
        # Nothing to do
 
88
        return
 
89
    now = time.time()
 
90
    old = now - float(mm_cfg.BAD_SHUNT_STALE_AFTER)
 
91
    os.stat_float_times(True)
 
92
    adir = mm_cfg.BAD_SHUNT_ARCHIVE_DIRECTORY
 
93
    if (adir and os.access(adir, os.W_OK | os.X_OK)
 
94
             and stat.S_ISDIR(os.stat(adir).st_mode)):
 
95
        pass
 
96
    else:
 
97
        if adir:
 
98
            print >>sys.stderr, '%s: archive directory %s not usable.' % (
 
99
                    PROGRAM, adir)
 
100
        adir = None
 
101
    for qdir in (mm_cfg.BADQUEUE_DIR, mm_cfg.SHUNTQUEUE_DIR):
 
102
        try:
 
103
            names = os.listdir(qdir)
 
104
        except OSError, e:
 
105
            if e.errno <> errno.ENOENT:
 
106
                # OK if qdir doesn't exist, else
 
107
                raise
 
108
            continue
 
109
        for name in names:
 
110
            pathname = os.path.join(qdir, name)
 
111
            pstat = os.stat(pathname)
 
112
            if not stat.S_ISREG(pstat.st_mode):
 
113
                # Not a regular file
 
114
                continue
 
115
            if pstat.st_mtime > old:
 
116
                # File is new
 
117
                continue
 
118
            if adir:
 
119
                os.rename(pathname, os.path.join(adir, name))
 
120
            else:
 
121
                os.remove(pathname)
 
122
 
 
123
 
 
124
 
 
125
if __name__ == '__main__':
 
126
    main()