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