~launchpad-pqm/launchpad/devel

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
#! /usr/bin/python
#
# Copyright 2009, 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import os
import grp
import pwd
import sys
import errno
import socket
import tempfile
import subprocess

from lp.services.config import config
from lp.services.mailman.config import (
    configure_prefix, configure_siteowner)
from lp.services.mailman.monkeypatches import monkey_patch
from lazr.config import as_username_groupname

basepath = [part for part in sys.path if part]


def build_mailman():
    # Build and install Mailman if it is enabled and not yet built.
    if not config.mailman.build:
        # There's nothing to do.
        return 0
    mailman_path = configure_prefix(config.mailman.build_prefix)
    mailman_bin = os.path.join(mailman_path, 'bin')
    var_dir = os.path.abspath(config.mailman.build_var_dir)

    # If we can import the package, we assume Mailman is properly built and
    # installed.  This does not catch re-installs that might be necessary
    # should our copy in sourcecode be updated.  Do that manually.
    sys.path.append(mailman_path)
    try:
        import Mailman
        # Also check for Launchpad-specific bits stuck into the source tree by
        # monkey_patch(), in case this is half-installed.  See
        # <https://bugs.launchpad.net/launchpad-registry/+bug/683486>.
        from Mailman.Queue import XMLRPCRunner
        from Mailman.Handlers import LPModerate
    except ImportError:
        pass
    else:
        return 0

    # sys.path_importer_cache is a mapping of elements of sys.path to importer
    # objects used to handle them. In Python2.5+ when an element of sys.path
    # is found to not exist on disk, a NullImporter is created and cached -
    # this causes Python to never bother re-inspecting the disk for that path
    # element. We must clear that cache element so that our second attempt to
    # import MailMan after building it will actually check the disk.
    del sys.path_importer_cache[mailman_path]

    # Make sure the target directories exist and have the correct
    # permissions, otherwise configure will complain.
    user, group = as_username_groupname(config.mailman.build_user_group)
    # Now work backwards to get the uid and gid
    try:
        uid = pwd.getpwnam(user).pw_uid
    except KeyError:
        print >> sys.stderr, 'No user found:', user
        sys.exit(1)
    try:
        gid = grp.getgrnam(group).gr_gid
    except KeyError:
        print >> sys.stderr, 'No group found:', group
        sys.exit(1)

    # Ensure that the var_dir exists, is owned by the user:group, and has
    # the necessary permissions.  Set the mode separately after the
    # makedirs() call because some platforms ignore mkdir()'s mode (though
    # I think Linux does not ignore it -- better safe than sorry).
    try:
        os.makedirs(var_dir)
    except OSError, e:
        if e.errno != errno.EEXIST:
            raise
    os.chown(var_dir, uid, gid)
    os.chmod(var_dir, 02775)

    mailman_source = os.path.join('sourcecode', 'mailman')
    if config.mailman.build_host_name:
        build_host_name = config.mailman.build_host_name
    else:
        build_host_name = socket.getfqdn()

    # Build and install the Mailman software.  Note that we don't care about
    # --with-cgi-gid because we're not going to use that Mailman subsystem.
    executable = os.path.abspath('bin/py')
    configure_args = (
        './configure',
        '--prefix', mailman_path,
        '--with-var-prefix=' + var_dir,
        '--with-python=' + executable,
        '--with-username=' + user,
        '--with-groupname=' + group,
        '--with-mail-gid=' + group,
        '--with-mailhost=' + build_host_name,
        '--with-urlhost=' + build_host_name,
        )
    # Configure.
    retcode = subprocess.call(configure_args, cwd=mailman_source)
    if retcode:
        print >> sys.stderr, 'Could not configure Mailman:'
        sys.exit(retcode)
    # Make.
    retcode = subprocess.call(('make', ), cwd=mailman_source)
    if retcode:
        print >> sys.stderr, 'Could not make Mailman.'
        sys.exit(retcode)
    # We have a brief interlude before we install.  Hardy will not
    # accept a script as the executable for the shebang line--it will
    # treat the file as a shell script instead. The ``bin/by``
    # executable that we specified in '--with-python' above is a script
    # so this behavior causes problems for us. Our work around is to
    # prefix the ``bin/py`` script with ``/usr/bin/env``, which makes
    # Hardy happy.  We need to do this before we install because the
    # installation will call Mailman's ``bin/update``, which is a script
    # that needs this fix.
    build_dir = os.path.join(mailman_source, 'build')
    original = '#! %s\n' % (executable, )
    modified = '#! /usr/bin/env %s\n' % (executable, )
    for (dirpath, dirnames, filenames) in os.walk(build_dir):
        for filename in filenames:
            filename = os.path.join(dirpath, filename)
            f = open(filename, 'r')
            if f.readline() == original:
                rest = f.read()
                f.close()
                f = open(filename, 'w')
                f.write(modified)
                f.write(rest)
            f.close()
    # Now we actually install.
    retcode = subprocess.call(('make', 'install'), cwd=mailman_source)
    if retcode:
        print >> sys.stderr, 'Could not install Mailman.'
        sys.exit(retcode)
    # Try again to import the package.
    try:
        # pylint: disable-msg=W0404
        import Mailman
    except ImportError:
        print >> sys.stderr, 'Could not import the Mailman package'
        return 1

    # Check to see if the site list exists.  The output can go to /dev/null
    # because we don't really care about it.  The site list exists if
    # config_list returns a zero exit status, otherwise it doesn't
    # (probably).  Before we can do this however, we must monkey patch
    # Mailman, otherwise mm_cfg.py won't be set up correctly.
    monkey_patch(mailman_path, config)

    import Mailman.mm_cfg
    retcode = subprocess.call(
        ('./config_list', '-o', '/dev/null',
         Mailman.mm_cfg.MAILMAN_SITE_LIST),
        cwd=mailman_bin,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    if retcode:
        addr, password = configure_siteowner(
            config.mailman.build_site_list_owner)

        # The site list does not yet exist, so create it now.
        retcode = subprocess.call(
            ('./newlist', '--quiet',
             '--emailhost=' + build_host_name,
             Mailman.mm_cfg.MAILMAN_SITE_LIST,
             addr, password),
            cwd=mailman_bin)
        if retcode:
            print >> sys.stderr, 'Could not create site list'
            return retcode

    retcode = configure_site_list(
        mailman_bin, Mailman.mm_cfg.MAILMAN_SITE_LIST)
    if retcode:
        print >> sys.stderr, 'Could not configure site list'
        return retcode

    # Create a directory to hold the gzip'd tarballs for the directories of
    # deactivated lists.
    try:
        os.mkdir(os.path.join(Mailman.mm_cfg.VAR_PREFIX, 'backups'))
    except OSError, e:
        if e.errno != errno.EEXIST:
            raise

    return 0


def configure_site_list(mailman_bin, site_list_name):
    """Configure the site list.

    Currently, the only thing we want to set is to not advertise the
    site list.
    """
    fd, config_file_name = tempfile.mkstemp()
    try:
        os.close(fd)
        config_file = open(config_file_name, 'w')
        try:
            print >> config_file, 'advertised = False'
        finally:
            config_file.close()
        return subprocess.call(
            ('./config_list', '-i', config_file_name, site_list_name),
            cwd=mailman_bin)
    finally:
        os.remove(config_file_name)


def main():
    # setting python paths
    program = sys.argv[0]

    src = 'lib'
    here = os.path.dirname(os.path.abspath(program))
    srcdir = os.path.join(here, src)
    sys.path = [srcdir, here] + basepath
    return build_mailman()


if __name__ == '__main__':
    return_code = main()
    sys.exit(return_code)