~launchpad-pqm/launchpad/devel

7315.5.3 by Barry Warsaw
The start of a mailing list importer and its tests.
1
#!/usr/bin/python2.4
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
7315.5.3 by Barry Warsaw
The start of a mailing list importer and its tests.
5
6
"""Import a mailing list (well, parts of it)."""
7
8
# XXX BarryWarsaw 2008-11-24
9
# Things this script does NOT currently do.
10
#
11
# - Import archives.
12
13
__metaclass__ = type
7315.5.6 by Barry Warsaw
Test logging, but also a start to the command line script.
14
__all__ = [
15
    'MailingListImport',
16
    ]
17
18
19
import sys
7315.5.7 by Barry Warsaw
Added security.cfg for the script's access.
20
import logging
21
import textwrap
7315.5.3 by Barry Warsaw
The start of a mailing list importer and its tests.
22
23
# pylint: disable-msg=W0403
24
import _pythonpath
25
7370.2.5 by Barry Warsaw
* Fix email sending to the AppServerLayer's SMTP server in zopeless mode.
26
from canonical.config import config
8356.1.9 by Leonard Richardson
Renamed the base script module in scripts/, which module_rename.py didn't touch because it wasn't under lib/.
27
from lp.services.scripts.base import LaunchpadScript
7315.5.6 by Barry Warsaw
Test logging, but also a start to the command line script.
28
from canonical.launchpad.scripts.mlistimport import Importer
7315.5.3 by Barry Warsaw
The start of a mailing list importer and its tests.
29
30
31
class MailingListImport(LaunchpadScript):
32
    """
7315.5.7 by Barry Warsaw
Added security.cfg for the script's access.
33
    %prog [options] team_name
7315.5.3 by Barry Warsaw
The start of a mailing list importer and its tests.
34
35
    Import various mailing list artifacts into a Launchpad mailing
36
    list.  This script allows you to import e.g. the membership list
37
    from an external mailing list into a Launchpad hosted mailng list.
38
    """
39
40
    loglevel = logging.INFO
41
    description = 'Import data into a Launchpad mailing list.'
42
43
    def __init__(self, name, dbuser=None):
44
        self.usage = textwrap.dedent(self.__doc__)
45
        super(MailingListImport, self).__init__(name, dbuser)
46
7315.5.6 by Barry Warsaw
Test logging, but also a start to the command line script.
47
    def add_my_options(self):
48
        """See `LaunchpadScript`."""
49
        self.parser.add_option('-f', '--filename', default='-', help=(
50
            'The file name containing the addresses to import, one '
51
            "per line.  If '-' is used or this option is not given, "
52
            'then addresses are read from standard input.'))
7370.2.5 by Barry Warsaw
* Fix email sending to the AppServerLayer's SMTP server in zopeless mode.
53
        self.parser.add_option('--notifications',
54
                               default=False, action='store_true',
55
                               help=(
56
            'Enable team-join notification sending to team admins.'))
7315.5.6 by Barry Warsaw
Test logging, but also a start to the command line script.
57
7315.5.3 by Barry Warsaw
The start of a mailing list importer and its tests.
58
    def main(self):
59
        """See `LaunchpadScript`."""
60
        team_name = None
61
        if len(self.args) == 0:
62
            self.parser.error('Missing team name')
63
        elif len(self.args) > 1:
64
            self.parser.error('Too many arguments')
65
        else:
66
            team_name = self.args[0]
67
7315.5.6 by Barry Warsaw
Test logging, but also a start to the command line script.
68
        importer = Importer(team_name, self.logger)
69
7370.2.5 by Barry Warsaw
* Fix email sending to the AppServerLayer's SMTP server in zopeless mode.
70
        # Suppress sending emails based on the (absence) of the --notification
71
        # switch.  Notifications are disabled by default because they can
72
        # cause huge amounts to be sent to the team owner.
73
        send_email_config = """
74
            [zopeless]
75
            send_email: %s
76
            """ % self.options.notifications
77
        config.push('send_email_config', send_email_config)
78
7315.5.6 by Barry Warsaw
Test logging, but also a start to the command line script.
79
        if self.options.filename == '-':
80
            # Read all the addresses from standard input, parse them
81
            # here, and use the direct interface to the importer.
82
            addresses = []
83
            while True:
84
                line = sys.stdin.readline()
85
                if line == '':
86
                    break
87
                addresses.append(line[:-1])
88
            importer.importAddresses(addresses)
89
        else:
90
            importer.importFromFile(self.options.filename)
91
7315.5.3 by Barry Warsaw
The start of a mailing list importer and its tests.
92
        # All done; commit the database changes.
93
        self.txn.commit()
94
        return 0
95
96
97
if __name__ == '__main__':
98
    script = MailingListImport('scripts.mlist-import', 'mlist-import')
99
    status = script.lock_and_run()
100
    sys.exit(status)