~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
10
import subprocess
11
4002.5.26 by Barry Warsaw
Branch fixes based on salgado's review.
12
from canonical.config import config
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
13
from canonical.launchpad.mailman.monkeypatches import monkey_patch
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
14
from configs import generate_overrides
15
16
basepath = filter(None, sys.path)
17
18
19
def build_mailman():
20
    # Build and install Mailman if it is enabled and not yet built.
4002.5.18 by Barry Warsaw
Update the tests for this branch, since we're no longer doing the same kind of
21
    mailman_path = config.mailman.build.prefix
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
22
    mailman_bin = os.path.join(mailman_path, 'bin')
4002.5.26 by Barry Warsaw
Branch fixes based on salgado's review.
23
    var_dir = os.path.abspath(config.mailman.build.var_dir)
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
24
25
    # If we can import the package, we assume Mailman is properly built and
26
    # installed.  This does not catch re-installs that might be necessary
27
    # should our copy in sourcecode be updated.  Do that manually.
28
    sys.path.append(mailman_path)
29
    try:
30
        import Mailman
31
    except ImportError:
32
        pass
33
    else:
34
        return 0
35
36
    if not config.mailman.build.build:
37
        # There's nothing to do.
38
        return 0
39
40
    # Make sure the target directories exist and have the correct
41
    # 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
42
    user, group = config.mailman.build.user_group
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
43
    # Now work backwards to get the uid and gid
44
    uid = pwd.getpwnam(user).pw_uid
45
    gid = grp.getgrnam(group).gr_gid
46
47
    # Ensure that the var_dir exists, is owned by the user:group, and has
48
    # the necessary permissions.  Set the mode separately after the
49
    # makedirs() call because some platforms ignore mkdir()'s mode (though
50
    # I think Linux does not ignore it -- better safe than sorry).
51
    try:
52
        os.makedirs(var_dir)
53
    except OSError, e:
4002.5.26 by Barry Warsaw
Branch fixes based on salgado's review.
54
        if e.errno != errno.EEXIST:
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
55
            raise
56
    os.chown(var_dir, uid, gid)
57
    os.chmod(var_dir, 02775)
58
59
    mailman_source = os.path.join('sourcecode', 'mailman')
60
61
    # Build and install the Mailman software.  Note that we don't care
62
    # about --with-mail-gid or --with-cgi-gid because we're not going to
63
    # use those Mailman subsystems.
64
    configure_args = (
65
        './configure',
66
        '--prefix', mailman_path,
67
        '--with-var-prefix=' + var_dir,
68
        '--with-python=' + sys.executable,
69
        '--with-username=' + user,
70
        '--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
71
        '--with-mailhost=' + config.mailman.build.host_name,
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
72
        )
73
    retcode = subprocess.call(configure_args, cwd=mailman_source)
74
    if retcode:
75
        print >> sys.stderr, 'Could not configure Mailman:'
76
        sys.exit(retcode)
77
    retcode = subprocess.call(('make',), cwd=mailman_source)
78
    if retcode:
79
        print >> sys.stderr, 'Could not make Mailman.'
80
        sys.exit(retcode)
81
    retcode = subprocess.call(('make', 'install'), cwd=mailman_source)
82
    if retcode:
83
        print >> sys.stderr, 'Could not install Mailman.'
84
        sys.exit(retcode)
85
    # Try again to import the package.
86
    try:
87
        import Mailman
88
    except ImportError:
89
        print >> sys.stderr, 'Could not import the Mailman package'
90
        return 1
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
91
92
    # Check to see if the site list exists.  The output can go to /dev/null
93
    # because we don't really care about it.  The site list exists if
94
    # config_list returns a zero exit status, otherwise it doesn't
95
    # (probably).  Before we can do this however, we must monkey patch
96
    # Mailman, otherwise mm_cfg.py won't be set up correctly.
97
    monkey_patch(mailman_path, config)
98
99
    import Mailman.mm_cfg
100
    retcode = subprocess.call(
101
        ('./config_list', '-o', '/dev/null',
102
         Mailman.mm_cfg.MAILMAN_SITE_LIST),
103
        cwd=mailman_bin,
104
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
105
106
    if retcode:
107
        addr, password = config.mailman.build.site_list_owner
108
109
        # The site list does not yet exist, so create it now.
110
        retcode = subprocess.call(
111
            ('./newlist', '--quiet',
112
             '--emailhost=' + config.mailman.build.host_name,
113
             Mailman.mm_cfg.MAILMAN_SITE_LIST,
114
             addr, password),
115
            cwd=mailman_bin)
116
        if retcode:
117
            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
118
            return retcode
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
119
4483.4.14 by Barry Warsaw
First crack at the implementation of Mailman's side of the XMLRPCRunner. This
120
    # Create a directory to hold the gzip'd tarballs for the directories of
121
    # deactivated lists.
4483.4.16 by Barry Warsaw
buildmailman.py: Ignore EEXIST if $VAR_PREFIX/backups already exists.
122
    try:
123
        os.mkdir(os.path.join(Mailman.mm_cfg.VAR_PREFIX, 'backups'))
124
    except OSError, e:
125
        if e.errno != errno.EEXIST:
126
            raise
4483.4.14 by Barry Warsaw
First crack at the implementation of Mailman's side of the XMLRPCRunner. This
127
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
128
    return 0
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
129
130
131
def main():
132
    # Sort ZCML overrides for our current config
133
    generate_overrides()
134
135
    # setting python paths
136
    program = sys.argv[0]
137
138
    src = 'lib'
139
    here = os.path.dirname(os.path.abspath(program))
140
    srcdir = os.path.join(here, src)
141
    sys.path = [srcdir, here] + basepath
142
    return build_mailman()
4002.5.31 by Barry Warsaw
Updates to the mmbuild branch based on feedback from SteveA and spiv over
143
4002.5.14 by Barry Warsaw
Changes to address jamesh's review comments.
144
145
146
if __name__ == '__main__':
147
    return_code = main()
148
    sys.exit(return_code)