~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
74
    # Build and install the Mailman software.  Note that we don't care
75
    # about --with-mail-gid or --with-cgi-gid because we're not going to
76
    # use those Mailman subsystems.
77
    configure_args = (
78
        './configure',
79
        '--prefix', mailman_path,
80
        '--with-var-prefix=' + var_dir,
81
        '--with-python=' + sys.executable,
82
        '--with-username=' + user,
83
        '--with-groupname=' + 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,
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
85
        )
86
    retcode = subprocess.call(configure_args, cwd=mailman_source)
87
    if retcode:
88
        print >> sys.stderr, 'Could not configure Mailman:'
89
        sys.exit(retcode)
90
    retcode = subprocess.call(('make',), cwd=mailman_source)
91
    if retcode:
92
        print >> sys.stderr, 'Could not make Mailman.'
93
        sys.exit(retcode)
94
    retcode = subprocess.call(('make', 'install'), cwd=mailman_source)
95
    if retcode:
96
        print >> sys.stderr, 'Could not install Mailman.'
97
        sys.exit(retcode)
98
    # Try again to import the package.
99
    try:
100
        import Mailman
101
    except ImportError:
102
        print >> sys.stderr, 'Could not import the Mailman package'
103
        return 1
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
104
105
    # Check to see if the site list exists.  The output can go to /dev/null
106
    # because we don't really care about it.  The site list exists if
107
    # config_list returns a zero exit status, otherwise it doesn't
108
    # (probably).  Before we can do this however, we must monkey patch
109
    # Mailman, otherwise mm_cfg.py won't be set up correctly.
110
    monkey_patch(mailman_path, config)
111
112
    import Mailman.mm_cfg
113
    retcode = subprocess.call(
114
        ('./config_list', '-o', '/dev/null',
115
         Mailman.mm_cfg.MAILMAN_SITE_LIST),
116
        cwd=mailman_bin,
117
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
118
119
    if retcode:
120
        addr, password = config.mailman.build.site_list_owner
121
122
        # The site list does not yet exist, so create it now.
123
        retcode = subprocess.call(
124
            ('./newlist', '--quiet',
125
             '--emailhost=' + config.mailman.build.host_name,
126
             Mailman.mm_cfg.MAILMAN_SITE_LIST,
127
             addr, password),
128
            cwd=mailman_bin)
129
        if retcode:
130
            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
131
            return retcode
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
132
5046.3.2 by Barry Warsaw
Some modifications in response to Francis's comments.
133
    retcode = configure_site_list(
134
        mailman_bin, Mailman.mm_cfg.MAILMAN_SITE_LIST)
135
    if retcode:
136
        print >> sys.stderr, 'Could not configure site list'
137
        return retcode
138
139
    # Create a directory to hold the gzip'd tarballs for the directories of
140
    # deactivated lists.
141
    try:
142
        os.mkdir(os.path.join(Mailman.mm_cfg.VAR_PREFIX, 'backups'))
143
    except OSError, e:
144
        if e.errno != errno.EEXIST:
145
            raise
146
147
    return 0
148
149
150
def configure_site_list(mailman_bin, site_list_name):
151
    """Configure the site list.
152
153
    Currently, the only thing we want to set is to not advertise the site list.
154
    """
4897.4.6 by Barry Warsaw
The first step toward a comprehensive Launchpad/Mailman integration test.
155
    fd, config_file_name = tempfile.mkstemp()
156
    try:
157
        os.close(fd)
158
        config_file = open(config_file_name, 'w')
159
        try:
160
            print >> config_file, 'advertised = False'
161
        finally:
162
            config_file.close()
5046.3.2 by Barry Warsaw
Some modifications in response to Francis's comments.
163
        return subprocess.call(
164
            ('./config_list', '-i', config_file_name, site_list_name),
165
            cwd=mailman_bin)
4897.4.6 by Barry Warsaw
The first step toward a comprehensive Launchpad/Mailman integration test.
166
    finally:
167
        os.remove(config_file_name)
168
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
169
170
def main():
171
    # Sort ZCML overrides for our current config
172
    generate_overrides()
173
174
    # setting python paths
175
    program = sys.argv[0]
176
177
    src = 'lib'
178
    here = os.path.dirname(os.path.abspath(program))
179
    srcdir = os.path.join(here, src)
180
    sys.path = [srcdir, here] + basepath
181
    return build_mailman()
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
182
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
183
184
185
if __name__ == '__main__':
186
    return_code = main()
187
    sys.exit(return_code)