summaryrefslogtreecommitdiff
path: root/bin/config_list
blob: d7e0fcb2248d267d12904d4f606ca369ca3d4a9e (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#! /usr/bin/env python
#
# Copyright (C) 1998,1999,2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""Configure a list from a text file description.

Usage: config_list [options] listname

Options:
    --inputfile filename
    -i filename
        Configure the list by assigning each module-global variable in the
        file to an attribute on the list object, then saving the list.  The
        named file is loaded with execfile() and must be legal Python code.
        Any variable that isn't already an attribute of the list object is
        ignored (a warning message is printed).  See also the -c option.

        A special variable named `mlist' is put into the globals during the
        execfile, which is bound to the actual MailList object.  This lets you
        do all manner of bizarre thing to the list object, but BEWARE!  Using
        this can severely (and possibly irreparably) damage your mailing list!

    --outputfile filename
    -o filename
        Instead of configuring the list, print out a list's configuration
        variables in a format suitable for input using this script.  In this
        way, you can easily capture the configuration settings for a
        particular list and imprint those settings on another list.  filename
        is the file to output the settings to.  If filename is `-', standard
        out is used.

    --checkonly
    -c
        With this option, the modified list is not actually changed.  Only
        useful with -i.

    --verbose
    -v
        Print the name of each attribute as it is being changed.  Only useful
        with -i.

    --help
    -h
        Print this help message and exit.

The options -o and -i are mutually exclusive.

"""

import sys
import time
import string
import getopt
from types import TupleType

import paths
from Mailman import mm_cfg
from Mailman import MailList
from Mailman import Utils



def usage(code, msg=''):
    print __doc__
    if msg:
        print msg
    sys.exit(code)



def do_output(listname, outfile):
    closep = 0
    stdout = sys.stdout
    try:
        if outfile == '-':
            outfp = sys.stdout
        else:
            outfp = open(outfile, 'w')
            closep = 1
        sys.stdout = outfp
        # open the specified list unlocked, since we're only reading it
        try:
            mlist = MailList.MailList(listname, lock=0)
        except Errors.MMUnknownListError:
            usage(1, 'List not found: ' + listname)
        # get all the list config info.  all this stuff is accessible via the
        # web interface
        print '## "%s" mailing list configuration settings' % listname,
        print '-*- python -*-'
        print '## captured on', time.ctime(time.time())
        print
        config = mlist.GetConfigInfo()
        categories = ('general', 'privacy', 'nondigest', 'digest', 'bounce',
                      'archive', 'gateway')
        for k in categories:
            info = config[k]
            print '##', string.capitalize(k), 'options'
            print '#'
            desc = Utils.wrap(info[0])
            for line in string.split(desc, '\n'):
                print '#', line
            print
            for data in info[1:]:
                if type(data) <> TupleType:
                    continue
                varname = data[0]
                vtype = data[1]
                desc = Utils.wrap(data[-1])
                for line in string.split(desc, '\n'):
                    print '#', line
                # munge the value based on its type
                value = getattr(mlist, varname)
                if vtype == mm_cfg.Text:
                    print varname, '=',
                    lines = string.split(string.replace(value, '\r', ''), '\n')
                    if len(lines) == 1:
                        print repr(value)
                    else:
                        first = 1
                        sys.stdout.write('"""')
                        for line in lines:
                            if first:
                                first = 0
                            else:
                                print
                            sys.stdout.write(string.replace(line, '"', '\\"'))
                        print '"""'
                elif vtype in (mm_cfg.Radio, mm_cfg.Toggle):
                    print '#'
                    print '# legal values are:'
                    i = 0
                    for choice in data[2]:
                        print '#   ', i, '= "%s"' % choice
                        i = i + 1
                    print varname, '=', repr(value)
                else:
                    print varname, '=', repr(value)
                print
    finally:
        if closep:
            outfp.close()
        sys.stdout = stdout



def do_input(listname, infile, checkonly, verbose):
    # open the specified list locked, unless checkonly is set
    try:
        mlist = MailList.MailList(listname, lock=not checkonly)
    except Errors.MMUnknownListError:
        usage(1, 'List not found: ' + listname)
    savelist = 0
    try:
        globals = {'mlist': mlist}
        # any exception that occurs in execfile() will cause the list to not
        # be saved, but any other problems are not save-fatal
        execfile(infile, globals)
        savelist = 1
        for k, v in globals.items():
            if k in ('mlist', '__builtins__'):
                continue
            if not hasattr(mlist, k):
                print 'attribute "%s" ignored' % k
                continue
            if verbose:
                print 'attribute "%s" changed' % k
            setattr(mlist, k, v)
    finally:
        if savelist and not checkonly:
            mlist.Save()
        mlist.Unlock()



def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'ci:o:vh',
            ['checkonly', 'inputfile=', 'outputfile=', 'verbose', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    # defaults
    infile = None
    outfile = None
    checkonly = 0
    verbose = 0
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-o', '--outputfile'):
            outfile = arg
        elif opt in ('-i', '--inputfile'):
            infile = arg
        elif opt in ('-c', '--checkonly'):
            checkonly = 1
        elif opt in ('-v', '--verbose'):
            verbose = 1

    # sanity check
    if infile is not None and outfile is not None:
        usage(1, 'only one of -i or -o is allowed')
    if infile is None and outfile is None:
        usage(1, 'one of -i or -o is required')

    # get the list name
    if len(args) <> 1:
        usage(1, 'list name is required')
    listname = args[0]

    if outfile:
        do_output(listname, outfile)
    else:
        do_input(listname, infile, checkonly, verbose)



if __name__ == '__main__':
    main()