~futatuki/mailman/2.1-forbid-subscription

« back to all changes in this revision

Viewing changes to bin/check_db

  • Committer:
  • Date: 2003-01-02 05:25:50 UTC
  • Revision ID: vcs-imports@canonical.com-20030102052550-qqbl1i96tzg3bach
This commit was manufactured by cvs2svn to create branch
'Release_2_1-maint'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! @PYTHON@
 
2
#
 
3
# Copyright (C) 1998,1999,2000,2001,2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 
 
19
"""Check a list's config database file for integrity.
 
20
 
 
21
All of the following files are checked:
 
22
 
 
23
    config.pck
 
24
    config.pck.last
 
25
    config.db
 
26
    config.db.last
 
27
    config.safety
 
28
 
 
29
It's okay if any of these are missing.  config.pck and config.pck.last are
 
30
pickled versions of the config database file for 2.1a3 and beyond.  config.db
 
31
and config.db.last are used in all earlier versions, and these are Python
 
32
marshals.  config.safety is a pickle written by 2.1a3 and beyond when the
 
33
primary config.pck file could not be read.
 
34
 
 
35
Usage: %(PROGRAM)s [options] [listname [listname ...]]
 
36
 
 
37
Options:
 
38
 
 
39
    --all / -a
 
40
        Check the databases for all lists.  Otherwise only the lists named on
 
41
        the command line are checked.
 
42
 
 
43
    --verbose / -v
 
44
        Verbose output.  The state of every tested file is printed.
 
45
        Otherwise only corrupt files are displayed.
 
46
 
 
47
    --help / -h
 
48
        Print this text and exit.
 
49
"""
 
50
 
 
51
import sys
 
52
import os
 
53
import errno
 
54
import getopt
 
55
import marshal
 
56
import cPickle
 
57
 
 
58
import paths
 
59
from Mailman import mm_cfg
 
60
from Mailman import Utils
 
61
from Mailman.MailList import MailList
 
62
from Mailman.i18n import _
 
63
 
 
64
PROGRAM = sys.argv[0]
 
65
 
 
66
 
 
67
 
 
68
def usage(code, msg=''):
 
69
    if code:
 
70
        fd = sys.stderr
 
71
    else:
 
72
        fd = sys.stdout
 
73
    print >> fd, _(__doc__)
 
74
    if msg:
 
75
        print >> fd, msg
 
76
    sys.exit(code)
 
77
 
 
78
 
 
79
 
 
80
def testfile(dbfile):
 
81
    if dbfile.endswith('.db') or dbfile.endswith('.db.last'):
 
82
        loadfunc = marshal.load
 
83
    elif dbfile.endswith('.pck') or dbfile.endswith('.pck.last'):
 
84
        loadfunc = cPickle.load
 
85
    else:
 
86
        assert 0
 
87
    fp = open(dbfile)
 
88
    try:
 
89
        loadfunc(fp)
 
90
    finally:
 
91
        fp.close()
 
92
 
 
93
 
 
94
def main():
 
95
    try:
 
96
        opts, args = getopt.getopt(sys.argv[1:], 'ahv',
 
97
                                   ['all', 'verbose', 'help'])
 
98
    except getopt.error, msg:
 
99
        usage(1, msg)
 
100
 
 
101
    verbose = 0
 
102
    listnames = args
 
103
 
 
104
    for opt, arg in opts:
 
105
        if opt in ('-h', '--help'):
 
106
            usage(0)
 
107
        elif opt in ('-v', '--verbose'):
 
108
            verbose = 1
 
109
        elif opt in ('-a', '--all'):
 
110
            listnames = Utils.list_names()
 
111
 
 
112
    listnames = [n.lower().strip() for n in listnames]
 
113
    if not listnames:
 
114
        print _('Nothing to do.')
 
115
        sys.exit(0)
 
116
 
 
117
    for listname in listnames:
 
118
        if not Utils.list_exists(listname):
 
119
            print _('No list named:'), listname
 
120
            continue
 
121
        mlist = MailList(listname, lock=0)
 
122
        pfile = os.path.join(mlist.fullpath(), 'config.pck')
 
123
        plast = pfile + '.last'
 
124
        dfile = os.path.join(mlist.fullpath(), 'config.db')
 
125
        dlast = dfile + '.last'
 
126
 
 
127
        if verbose:
 
128
            print _('List:'), listname
 
129
 
 
130
        for file in (pfile, plast, dfile, dlast):
 
131
            status = 0
 
132
            try:
 
133
                testfile(file)
 
134
            except IOError, e:
 
135
                # Don't report ENOENT unless we're in verbose mode
 
136
                if verbose or e.errno <> errno.ENOENT:
 
137
                    status = e
 
138
            except Exception, e:
 
139
                status = e
 
140
            # Report errors
 
141
            if status:
 
142
                if isinstance(status, EnvironmentError):
 
143
                    # This already includes the file name
 
144
                    print '   ', status
 
145
                else:
 
146
                    print '    %s: %s' % (file, status)
 
147
            elif verbose:
 
148
                print _('   %(file)s: okay')
 
149
 
 
150
 
 
151
 
 
152
if __name__ == '__main__':
 
153
    main()