~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
5863.9.1 by Curtis Hovey
Updated configs and code to used simple datatypes. Changes may still be needed aftert testing.
14
from canonical.launchpad.mailman.config import (
5863.9.7 by Curtis Hovey
Refacortings per review.
15
    configure_hostname, configure_prefix, configure_siteowner,
16
    configure_usergroup)
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
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
18
from configs import generate_overrides
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.
5863.9.7 by Curtis Hovey
Refacortings per review.
45
    user, group = configure_usergroup(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')
5863.9.7 by Curtis Hovey
Refacortings per review.
71
    build_host_name = configure_hostname(config.mailman.build_host_name)
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
72
5427.2.1 by Barry Warsaw
Correct a bald-faced lie. Mailman's buildout for Launchpad /does/ need to set
73
    # Build and install the Mailman software.  Note that we don't care about
74
    # --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.
75
    configure_args = (
76
        './configure',
77
        '--prefix', mailman_path,
78
        '--with-var-prefix=' + var_dir,
79
        '--with-python=' + sys.executable,
80
        '--with-username=' + user,
81
        '--with-groupname=' + group,
5427.2.1 by Barry Warsaw
Correct a bald-faced lie. Mailman's buildout for Launchpad /does/ need to set
82
        '--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.
83
        '--with-mailhost=' + build_host_name,
84
        '--with-urlhost=' + 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:
5863.9.7 by Curtis Hovey
Refacortings per review.
120
        addr, password = configure_siteowner(
121
            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
122
123
        # The site list does not yet exist, so create it now.
124
        retcode = subprocess.call(
125
            ('./newlist', '--quiet',
5863.9.1 by Curtis Hovey
Updated configs and code to used simple datatypes. Changes may still be needed aftert testing.
126
             '--emailhost=' + build_host_name,
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
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)