summaryrefslogtreecommitdiff
path: root/bin/update
blob: 2dd5368e14f002b51fb4374a9010e6d3413c5087 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#! /usr/bin/env python
#
# Copyright (C) 1998,1999,2000 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""Use this script to help you update to the latest release of Mailman from
some previous version.  It knows about versions back to 1.0b4 (?).

"""

import sys
import os
import marshal
import errno

import paths
from Mailman import mm_cfg
from Mailman import Utils
from Mailman import MailList
from Mailman.LockFile import TimeOutError
from Mailman.i18n import _

FRESH = 0
NOTFRESH = -1

LMVFILE = os.path.join(mm_cfg.DATA_DIR, 'last_mailman_version')



def calcversions():
    # Returns a tuple of (lastversion, thisversion).  If the last version
    # could not be determined, lastversion will be FRESH or NOTFRESH,
    # depending on whether this installation appears to be fresh or not.  The
    # determining factor is whether there are files in the $var_prefix/logs
    # subdir or not.  The version numbers are HEX_VERSIONs.
    #
    # See if we stored the last updated version
    lastversion = None
    thisversion = mm_cfg.HEX_VERSION
    try:
        fp = open(LMVFILE)
        data = fp.read()
        fp.close()
        lastversion = int(data, 16)
    except (IOError, ValueError):
        pass
    #
    # try to figure out if this is a fresh install
    if lastversion is None:
        lastversion = FRESH
        try:
            if os.listdir(mm_cfg.LOG_DIR):
                lastversion = NOTFRESH
        except OSError:
            pass
    return (lastversion, thisversion)



def makeabs(relpath):
    return os.path.join(mm_cfg.PREFIX, relpath)



def move_language_templates(mlist):
    print >> sys.stderr, 'Fixing language templates:', mlist.internal_name()
    # In Mailman 2.1, each language gets its own templates under
    # lists/<listname>.  Since English is the default language for the
    # templates, we want to move all the existing templates under
    # lists/<listname> to lists/<listname>/en
    listpath = mlist._full_path
    en_path = os.path.join(listpath, 'en')
    omask = os.umask(0)
    try:
        try:
            os.mkdir(en_path, 02770)
        except OSError, e:
            if e.errno <> errno.EEXIST: raise
    finally:
        os.umask(omask)
    # Now, move all the old template files to the new `en' directory
    for file in [f for f in os.listdir(listpath)
                 if f.endswith('.txt') or f.endswith('.html')]:
        os.rename(os.path.join(listpath, file),
                  os.path.join(listpath, 'en', file))
        


def dolist(listname):
    errors = 0
    mlist = MailList.MailList(listname, lock=0)
    try:
        mlist.Lock(0.5)
    except TimeOutError:
        print >> sys.stderr, _('WARNING: could not acquire lock for list: '
                               '%(listname)s')
        return 1

    mbox_dir = makeabs('archives/private/%s.mbox' % (listname))
    mbox_file = makeabs('archives/private/%s.mbox/%s' % (listname, listname))

    o_pub_mbox_file = makeabs('archives/public/%s' % (listname))
    o_pri_mbox_file = makeabs('archives/private/%s' % (listname))

    html_dir = o_pri_mbox_file
    o_html_dir = makeabs('public_html/archives/%s' % (listname))
    #
    # make the mbox directory if it's not there.
    #
    if not os.path.exists(mbox_dir):
        ou = os.umask(0)
        os.mkdir(mbox_dir, 02775)
        os.umask(ou)
    else:
        # this shouldn't happen, but hey, just in case
        if not os.path.isdir(mbox_dir):
            print _("""\
For some reason, %(mbox_dir)s exists as a file.  This won't work with
b6, so I'm renaming it to %(mbox_dir)s.tmp and proceeding.""")
            os.rename(mbox_dir, "%s.tmp" % (mbox_dir))
            ou = os.umask(0)
            os.mkdir(mbox_dir, 02775)
            os.umask(ou)

    # Move any existing mboxes around, but watch out for both a public and a
    # private one existing
    if os.path.isfile(o_pri_mbox_file) and os.path.isfile(o_pub_mbox_file):
        if mlist.archive_private:
            print _("""\

%(listname)s has both public and private mbox archives.  Since this list
currently uses private archiving, I'm installing the private mbox archive
-- %(o_pri_mbox_file)s -- as the active archive, and renaming
	%(o_pub_mbox_file)s
to
	%(o_pub_mbox_file)s.preb6

You can integrate that into the archives if you want by using the 'arch'
script.
""") % (mlist._internal_name, o_pri_mbox_file, o_pub_mbox_file,
        o_pub_mbox_file)
            os.rename(o_pub_mbox_file, "%s.preb6" % (o_pub_mbox_file))
        else:
            print _("""\
%s has both public and private mbox archives.  Since this list
currently uses public archiving, I'm installing the public mbox file
archive file (%s) as the active one, and renaming
	%s
    to
    	%s.preb6

You can integrate that into the archives if you want by using the 'arch'
script.
""") % (mlist._internal_name, o_pub_mbox_file, o_pri_mbox_file,
        o_pri_mbox_file)
            os.rename(o_pri_mbox_file, "%s.preb6" % (o_pri_mbox_file))
    #
    # move private archive mbox there if it's around
    # and take into account all sorts of absurdities
    #
    print '- updating old private mbox file'
    if os.path.exists(o_pri_mbox_file):
        if os.path.isfile(o_pri_mbox_file):
            os.rename(o_pri_mbox_file, mbox_file)
        elif not os.path.isdir(o_pri_mbox_file):
            newname = "%s.mm_install-dunno_what_this_was_but_its_in_the_way" \
                      % o_pri_mbox_file
            os.rename(o_pri_mbox_file, newname)
            print "    unknown file in the way, moving"
            print "        " + o_pri_mbox_file
            print "    to"
            print "        " + newname
        else:
            # directory
            print "    looks like you have a really recent CVS installation..."
            print "    you're either one brave soul, or you already ran me"
    

    #
    # move public archive mbox there if it's around
    # and take into account all sorts of absurdities.
    #
    print '- updating old public mbox file'
    if os.path.exists(o_pub_mbox_file):
        if os.path.isfile(o_pub_mbox_file):
            os.rename(o_pub_mbox_file, mbox_file)
        elif not os.path.isdir(o_pub_mbox_file):
            newname = "%s.mm_install-dunno_what_this_was_but_its_in_the_way" \
                      % o_pub_mbox_file
            os.rename(o_pub_mbox_file, newname)
            print "    unknown file in the way, moving"
            print "        " + o_pub_mbox_file
            print "    to"
            print "        " + newname
        else: # directory
            print "    looks like you have a really recent CVS installation..."
            print "    you're either one brave soul, or you already ran me"

    #
    # move the html archives there
    #
    if os.path.isdir(o_html_dir):
        os.rename(o_html_dir, html_dir)
        #
        # chmod the html archives
        #
        os.chmod(html_dir, 02775)
    #
    # save the new variables and
    # let it create public symlinks if necessary
    #
    mlist.archive_directory = makeabs('archives/private/%s' % (listname))
    mlist.private_archive_file_dir = makeabs('archives/private/%s.mbox' %
                                             listname)
    mlist.Save()
    #
    # check to see if pre-b4 list-specific templates are around
    # and move them to the new place if there's not already
    # a new one there
    #
    tmpl_dir = os.path.join(mm_cfg.PREFIX, "templates")
    list_dir = os.path.join(mm_cfg.PREFIX, "lists")
    b4_tmpl_dir = os.path.join(tmpl_dir, mlist._internal_name)
    new_tmpl_dir = os.path.join(list_dir, mlist._internal_name)
    if os.path.exists(b4_tmpl_dir):
	print "- This list looks like it might have <= b4 " \
              'list templates around'
        for f in os.listdir(b4_tmpl_dir):
            o_tmpl = os.path.join(b4_tmpl_dir, f)
            n_tmpl = os.path.join(new_tmpl_dir, f)
            if not os.path.exists(n_tmpl):
                os.rename(o_tmpl, n_tmpl)
                print "- moved %s to %s" % (o_tmpl, n_tmpl)
            else:
                print "- both %s and %s exist, leaving untouched" \
                      % (o_tmpl, n_tmpl)
    #
    # Move all the templates to the en language subdirectory as required for
    # Mailman 2.1
    #
    move_language_templates(mlist)
    # Avoid eating filehandles with the list lockfiles
    mlist.Unlock()
    return 0



# this function is passed to os.path.walk
# to fix the perms on old html archives.
#
def archive_path_fixer(unused_arg, dir, files):
    for f in files:
        abs = os.path.join(dir, f)
        if os.path.isdir(abs):
            if f == "database":
                os.chmod(abs, 02770)
            else:
                os.chmod(abs, 02775)
        elif os.path.isfile(abs):
            os.chmod(abs, 0664)

def remove_old_sources(module):
    src = "%s/%s" % (mm_cfg.PREFIX, module)
    pyc = src + "c"
    if os.path.exists(src):
        print "removing", src
        try:
            os.unlink(src)
        except os.error, rest:
            print "Warning: couldn't remove", src, "--", str(rest)
    if module[-3:] == '.py' and os.path.exists(pyc):
        try:
            os.unlink(pyc)
        except os.error, rest:
            print "couldn't remove old file", pyc, "--", str(rest)


def main():
    errors = 0
    # get rid of old stuff
    for mod in ('Mailman/Archiver.py', 'Mailman/HyperArch.py',
                'Mailman/HyperDatabase.py', 'Mailman/pipermail.py',
                'Mailman/smtplib.py',
                'bin/update_to_10b6'):
        remove_old_sources(mod)
    listnames = Utils.list_names()
    if not listnames:
        print "no lists == nothing to do, exiting"
        return
    #
    # for people with web archiving, make sure the directories
    # in the archiving are set with proper perms for b6.
    #
    if os.path.isdir("%s/public_html/archives" % mm_cfg.PREFIX):
        print "fixing all the perms on your old html archives to work with b6"
        print "If your archives are big, this could take a minute or two..."
        os.path.walk("%s/public_html/archives" % mm_cfg.PREFIX,
                     archive_path_fixer, "")
        print "done"
    for listname in listnames:
        print 'Updating mailing list: ', listname
        errors = errors + dolist(listname)
    print 'Updating Usenet watermarks'
    wmfile = os.path.join(mm_cfg.DATA_DIR, 'gate_watermarks')
    try:
        fp = open(wmfile)
    except IOError:
        print '- nothing to update here'
    else:
        d = marshal.load(fp)
        fp.close()
        for listname in d.keys():
            if listname not in listnames:
                # this list no longer exists
                continue
            mlist = MailList.MailList(listname, lock=0)
            try:
                mlist.Lock(0.5)
            except TimeOutError:
                print 'WARNING: could not acquire lock for list:', listname
                errors = errors + 1
            else:
                # Pre 1.0b7 stored 0 in the gate_watermarks file to indicate
                # that no gating had been done yet.  Without coercing this to
                # None, the list could now suddenly get flooded.
                mlist.usenet_watermark = d[listname] or None
                mlist.Save()
                mlist.Unlock()
        os.unlink(wmfile)
        print '- usenet watermarks updated and gate_watermarks removed'
    #
    # This warning was necessary for the upgrade from 1.0b9 to 1.0b10.
    # There's no good way of figuring this out for releases prior to 2.0beta2
    # :(
    #
    if lastversion == NOTFRESH:
        print """

NOTE NOTE NOTE NOTE NOTE

    You are upgrading an existing Mailman installation, but I can't tell what
    version you were previously running.

    If you are upgrading from Mailman 1.0b9 or earlier you will need to
    manually update your mailing lists.  For each mailing list you need to
    copy the file templates/options.html lists/<listname>/options.html.

    However, if you have edited this file via the Web interface, you will have
    to merge your changes into this file, otherwise you will lose your
    changes.

NOTE NOTE NOTE NOTE NOTE

"""
    return errors



if __name__ == '__main__':
    # calculate the versions
    lastversion, thisversion = calcversions()
    if lastversion == thisversion:
        # nothing to do
        sys.exit(0)
    if lastversion > thisversion:
        print 'Downgrade detected, from version', hex(lastversion), \
              'to version', hex(thisversion)
        print 'This is probably not safe.  Exiting.'
        sys.exit(1)
    print 'Upgrading from version', hex(lastversion), 'to', hex(thisversion)
    errors = main()
    if not errors:
        # Record the version we just upgraded to
        fp = open(LMVFILE, 'w')
        fp.write(hex(mm_cfg.HEX_VERSION) + '\n')
        fp.close()
    else:
        print '''\

ERROR:

The locks for some lists could not be acquired.  This means that either
Mailman was still active when you upgraded, or there were stale locks in the
%(lockdir)s directory.

You must put Mailman into a quiescent state and remove all stale locks, then
re-run "make update" manually.  See the INSTALL and UPGRADE files for details.
''' % {'lockdir': mm_cfg.LOCK_DIR}