~launchpad-pqm/launchpad/devel

4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
1
#! /usr/bin/python2.4
2
#
3
# Copyright 2007 Canonical Ltd.  All rights reserved.
4
5
import os
6
import grp
7
import pwd
8
import sys
9
import errno
4897.4.6 by Barry Warsaw
The first step toward a comprehensive Launchpad/Mailman integration test.
10
import tempfile
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
11
import subprocess
12
4002.5.26 by Barry Warsaw
Branch fixes based on salgado's review.
13
from canonical.config import config
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
14
from canonical.launchpad.mailman.monkeypatches import monkey_patch
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
15
from configs import generate_overrides
16
17
basepath = filter(None, sys.path)
18
19
20
def build_mailman():
21
    # Build and install Mailman if it is enabled and not yet built.
5415.1.1 by Barry Warsaw
If there is no <mailman> or <mailman-build> section, there's nothing to build, so return immediately.
22
    if config.mailman is None or config.mailman.build is None:
23
        # There is no <mailman> or <mailman-build> section in the
24
        # configuration file, so there's nothing to build.
25
        return 0
4002.5.18 by Barry Warsaw
Update the tests for this branch, since we're no longer doing the same kind of
26
    mailman_path = config.mailman.build.prefix
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
27
    mailman_bin = os.path.join(mailman_path, 'bin')
4002.5.26 by Barry Warsaw
Branch fixes based on salgado's review.
28
    var_dir = os.path.abspath(config.mailman.build.var_dir)
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
29
30
    # If we can import the package, we assume Mailman is properly built and
31
    # installed.  This does not catch re-installs that might be necessary
32
    # should our copy in sourcecode be updated.  Do that manually.
33
    sys.path.append(mailman_path)
34
    try:
35
        import Mailman
36
    except ImportError:
37
        pass
38
    else:
39
        return 0
40
41
    if not config.mailman.build.build:
42
        # There's nothing to do.
43
        return 0
44
45
    # Make sure the target directories exist and have the correct
46
    # permissions, otherwise configure will complain.
4002.5.18 by Barry Warsaw
Update the tests for this branch, since we're no longer doing the same kind of
47
    user, group = config.mailman.build.user_group
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
48
    # Now work backwards to get the uid and gid
5373.2.8 by Barry Warsaw
Solve a few other problems related to staging's Mailman configs.
49
    try:
50
        uid = pwd.getpwnam(user).pw_uid
51
    except KeyError:
52
        print >> sys.stderr, 'No user found:', user
53
        sys.exit(1)
54
    try:
55
        gid = grp.getgrnam(group).gr_gid
56
    except KeyError:
57
        print >> sys.stderr, 'No group found:', group
58
        sys.exit(1)
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
59
60
    # Ensure that the var_dir exists, is owned by the user:group, and has
61
    # the necessary permissions.  Set the mode separately after the
62
    # makedirs() call because some platforms ignore mkdir()'s mode (though
63
    # I think Linux does not ignore it -- better safe than sorry).
64
    try:
65
        os.makedirs(var_dir)
66
    except OSError, e:
4002.5.26 by Barry Warsaw
Branch fixes based on salgado's review.
67
        if e.errno != errno.EEXIST:
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
68
            raise
69
    os.chown(var_dir, uid, gid)
70
    os.chmod(var_dir, 02775)
71
72
    mailman_source = os.path.join('sourcecode', 'mailman')
73
5427.2.1 by Barry Warsaw
Correct a bald-faced lie. Mailman's buildout for Launchpad /does/ need to set
74
    # Build and install the Mailman software.  Note that we don't care about
75
    # --with-cgi-gid because we're not going to use that Mailman subsystem.
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
76
    configure_args = (
77
        './configure',
78
        '--prefix', mailman_path,
79
        '--with-var-prefix=' + var_dir,
80
        '--with-python=' + sys.executable,
81
        '--with-username=' + user,
82
        '--with-groupname=' + group,
5427.2.1 by Barry Warsaw
Correct a bald-faced lie. Mailman's buildout for Launchpad /does/ need to set
83
        '--with-mail-gid=' + group,
4002.5.18 by Barry Warsaw
Update the tests for this branch, since we're no longer doing the same kind of
84
        '--with-mailhost=' + config.mailman.build.host_name,
5440.2.4 by Barry Warsaw
Update the integration tests to match the fixed semantics about posting with
85
        '--with-urlhost=' + config.mailman.build.host_name,
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
86
        )
87
    retcode = subprocess.call(configure_args, cwd=mailman_source)
88
    if retcode:
89
        print >> sys.stderr, 'Could not configure Mailman:'
90
        sys.exit(retcode)
91
    retcode = subprocess.call(('make',), cwd=mailman_source)
92
    if retcode:
93
        print >> sys.stderr, 'Could not make Mailman.'
94
        sys.exit(retcode)
95
    retcode = subprocess.call(('make', 'install'), cwd=mailman_source)
96
    if retcode:
97
        print >> sys.stderr, 'Could not install Mailman.'
98
        sys.exit(retcode)
99
    # Try again to import the package.
100
    try:
101
        import Mailman
102
    except ImportError:
103
        print >> sys.stderr, 'Could not import the Mailman package'
104
        return 1
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
105
106
    # Check to see if the site list exists.  The output can go to /dev/null
107
    # because we don't really care about it.  The site list exists if
108
    # config_list returns a zero exit status, otherwise it doesn't
109
    # (probably).  Before we can do this however, we must monkey patch
110
    # Mailman, otherwise mm_cfg.py won't be set up correctly.
111
    monkey_patch(mailman_path, config)
112
113
    import Mailman.mm_cfg
114
    retcode = subprocess.call(
115
        ('./config_list', '-o', '/dev/null',
116
         Mailman.mm_cfg.MAILMAN_SITE_LIST),
117
        cwd=mailman_bin,
118
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
119
120
    if retcode:
121
        addr, password = config.mailman.build.site_list_owner
122
123
        # The site list does not yet exist, so create it now.
124
        retcode = subprocess.call(
125
            ('./newlist', '--quiet',
126
             '--emailhost=' + config.mailman.build.host_name,
127
             Mailman.mm_cfg.MAILMAN_SITE_LIST,
128
             addr, password),
129
            cwd=mailman_bin)
130
        if retcode:
131
            print >> sys.stderr, 'Could not create site list'
4483.4.25 by Barry Warsaw
Last round of changes in response to bac's comments (who took over in BjornT's
132
            return retcode
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
133
5046.3.2 by Barry Warsaw
Some modifications in response to Francis's comments.
134
    retcode = configure_site_list(
135
        mailman_bin, Mailman.mm_cfg.MAILMAN_SITE_LIST)
136
    if retcode:
137
        print >> sys.stderr, 'Could not configure site list'
138
        return retcode
139
140
    # Create a directory to hold the gzip'd tarballs for the directories of
141
    # deactivated lists.
142
    try:
143
        os.mkdir(os.path.join(Mailman.mm_cfg.VAR_PREFIX, 'backups'))
144
    except OSError, e:
145
        if e.errno != errno.EEXIST:
146
            raise
147
148
    return 0
149
150
151
def configure_site_list(mailman_bin, site_list_name):
152
    """Configure the site list.
153
154
    Currently, the only thing we want to set is to not advertise the site list.
155
    """
4897.4.6 by Barry Warsaw
The first step toward a comprehensive Launchpad/Mailman integration test.
156
    fd, config_file_name = tempfile.mkstemp()
157
    try:
158
        os.close(fd)
159
        config_file = open(config_file_name, 'w')
160
        try:
161
            print >> config_file, 'advertised = False'
162
        finally:
163
            config_file.close()
5046.3.2 by Barry Warsaw
Some modifications in response to Francis's comments.
164
        return subprocess.call(
165
            ('./config_list', '-i', config_file_name, site_list_name),
166
            cwd=mailman_bin)
4897.4.6 by Barry Warsaw
The first step toward a comprehensive Launchpad/Mailman integration test.
167
    finally:
168
        os.remove(config_file_name)
169
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
170
171
def main():
172
    # Sort ZCML overrides for our current config
173
    generate_overrides()
174
175
    # setting python paths
176
    program = sys.argv[0]
177
178
    src = 'lib'
179
    here = os.path.dirname(os.path.abspath(program))
180
    srcdir = os.path.join(here, src)
181
    sys.path = [srcdir, here] + basepath
182
    return build_mailman()
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
183
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
184
185
186
if __name__ == '__main__':
187
    return_code = main()
188
    sys.exit(return_code)