list_owners.py 2.87 KB
Newer Older
Barry Warsaw's avatar
Barry Warsaw committed
1
# Copyright (C) 2002-2015 by the Free Software Foundation, Inc.
bwarsaw's avatar
bwarsaw committed
2
#
Barry Warsaw's avatar
Barry Warsaw committed
3
# This file is part of GNU Mailman.
bwarsaw's avatar
bwarsaw committed
4
#
Barry Warsaw's avatar
Barry Warsaw committed
5 6 7 8
# GNU Mailman 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 3 of the License, or (at your option)
# any later version.
bwarsaw's avatar
bwarsaw committed
9
#
Barry Warsaw's avatar
Barry Warsaw committed
10 11 12 13 14 15 16
# GNU Mailman 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
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
bwarsaw's avatar
bwarsaw committed
17 18 19 20

import sys
import optparse

21 22
from zope.component import getUtility

23
from mailman.MailList import MailList
24
from mailman.core.i18n import _
25
from mailman.initialize import initialize
Barry Warsaw's avatar
Barry Warsaw committed
26
from mailman.interfaces.listmanager import IListManager
27
from mailman.version import MAILMAN_VERSION
28

bwarsaw's avatar
bwarsaw committed
29 30 31


def parseargs():
32
    parser = optparse.OptionParser(version=MAILMAN_VERSION,
bwarsaw's avatar
bwarsaw committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
                                   usage=_("""\
%prog [options] [listname ...]

List the owners of a mailing list, or all mailing lists if no list names are
given."""))
    parser.add_option('-w', '--with-listnames',
                      default=False, action='store_true',
                      help=_("""\
Group the owners by list names and include the list names in the output.
Otherwise, the owners will be sorted and uniquified based on the email
address."""))
    parser.add_option('-m', '--moderators',
                      default=False, action='store_true',
                      help=_('Include the list moderators in the output.'))
47 48
    parser.add_option('-C', '--config',
                      help=_('Alternative configuration file to use'))
bwarsaw's avatar
bwarsaw committed
49 50 51 52 53 54 55
    opts, args = parser.parse_args()
    return parser, opts, args



def main():
    parser, opts, args = parseargs()
56
    initialize(opts.config)
bwarsaw's avatar
bwarsaw committed
57

58 59
    list_manager = getUtility(IListManager)
    listnames = set(args or list_manager.names)
bwarsaw's avatar
bwarsaw committed
60 61 62
    bylist = {}

    for listname in listnames:
63
        mlist = list_manager.get(listname)
64
        addrs = [addr.address for addr in mlist.owners.addresses]
bwarsaw's avatar
bwarsaw committed
65
        if opts.moderators:
66
            addrs.extend([addr.address for addr in mlist.moderators.addresses])
bwarsaw's avatar
bwarsaw committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        bylist[listname] = addrs

    if opts.with_listnames:
        for listname in listnames:
            unique = set()
            for addr in bylist[listname]:
                unique.add(addr)
            keys = list(unique)
            keys.sort()
            print listname
            for k in keys:
                print '\t', k
    else:
        unique = set()
        for listname in listnames:
            for addr in bylist[listname]:
                unique.add(addr)
84
        for k in sorted(unique):
bwarsaw's avatar
bwarsaw committed
85 86 87 88 89 90
            print k



if __name__ == '__main__':
    main()