~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/mlist-sync.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2008-01-03 20:18:56 UTC
  • mfrom: (5427.2.2 mailman-config-fix)
  • Revision ID: launchpad@pqm.canonical.com-20080103201856-p11fhtsc1fzkzdnq
[r=flacoste] Two fixes found while testing mailing lists on staging.  First, the build process needs the proper gid that Exim will invoke the wrapper with.  Second, a simple minded script to sync the Mailman data structures with staging's copy of production.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.4
 
2
# Copyright 2008 Canonical Ltd.  All rights reserved.
 
3
 
 
4
import os
 
5
import sys
 
6
import logging
 
7
import textwrap
 
8
import subprocess
 
9
 
 
10
# pylint: disable-msg=W0403
 
11
import _pythonpath
 
12
 
 
13
from canonical.config import config
 
14
from canonical.launchpad.scripts.base import LaunchpadScript
 
15
 
 
16
 
 
17
class MailingListSyncScript(LaunchpadScript):
 
18
    """
 
19
    %prog [options]
 
20
 
 
21
    Sync the mailing list data structures with the database.  This is
 
22
    necessary for example when the production database is copied to staging.
 
23
 
 
24
    XXX For now, we /know/ that production has no databases, so this script
 
25
    takes the cheap way out by deleting all lists except the site list.
 
26
    """
 
27
 
 
28
    loglevel = logging.INFO
 
29
    description = 'Sync the Mailman data structures with the database.'
 
30
 
 
31
    def __init__(self):
 
32
        self.usage = textwrap.dedent(self.__doc__)
 
33
        super(MailingListSyncScript, self).__init__('scripts.mlist_sync')
 
34
 
 
35
    def add_my_options(self):
 
36
        self.parser.add_option('-n', '--dry-run',
 
37
                               default=False, action='store_true', help="""\
 
38
Show the lists that would be deleted, but do not delete them.""")
 
39
 
 
40
    def main(self):
 
41
        """See `LaunchpadScript`."""
 
42
        if len(self.args) != 0:
 
43
            self.parser.error('Too many arguments')
 
44
 
 
45
        # Set up access to the Mailman package and import the defaults.
 
46
        mailman_path = config.mailman.build.prefix
 
47
        mailman_bin = os.path.join(mailman_path, 'bin')
 
48
        sys.path.append(mailman_path)
 
49
        from Mailman import mm_cfg
 
50
        from Mailman import Utils
 
51
 
 
52
        deletable_lists = set(Utils.list_names())
 
53
        deletable_lists.remove(mm_cfg.MAILMAN_SITE_LIST)
 
54
        if len(deletable_lists) == 0:
 
55
            print 'Nothing to do.'
 
56
            return 0
 
57
        
 
58
        if self.options.dry_run:
 
59
            print 'Lists that would be deleted:'
 
60
            for list_name in sorted(deletable_lists):
 
61
                print '\t', list_name
 
62
                return 0
 
63
 
 
64
        # Deleting lists is done with the rmlist script, which unfortunately
 
65
        # is not importable in Mailman 2.1.  Because we also want to
 
66
        # completely delete the archives, we'll just shell out to rmlist.
 
67
        errors = 0
 
68
        for list_name in sorted(deletable_lists):
 
69
            print 'Removing all traces of mailing list:', list_name
 
70
            retcode = subprocess.call(
 
71
                ('./rmlist', '-a', list_name),
 
72
                cwd=mailman_bin)
 
73
            if retcode:
 
74
                print >> sys.stderr, 'Could not delete list:', list_name
 
75
                # For now, keep going.
 
76
                errors += 1
 
77
        return errors
 
78
 
 
79
 
 
80
if __name__ == '__main__':
 
81
    script = MailingListSyncScript()
 
82
    status = script.run()
 
83
    sys.exit(status)