summaryrefslogtreecommitdiff
path: root/Mailman/versions.py
blob: 8ff02d1e6df63dc49e30a912d9cb2fad98d294e6 (plain)
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
# Copyright (C) 1998 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 0211-1307, USA.


"""Routines which rectify an old maillist with current maillist structure.

The maillist .CheckVersion() method looks for an old .data_version
setting in the loaded maillist structure, and if found calls the
Update() routine from this module, supplying the list and the state
last loaded from storage.  (Th state is necessary to distinguish from
default assignments done in the .InitVars() methods, before
.CheckVersion() is called.)

For new versions you should add sections to the UpdateOldVars() and the
UpdateOldUsers() sections, to preserve the sense of settings across
structural changes.  Note that the routines have only one pass - when
.CheckVersions() finds a version change it runs this routine and then
updates the data_version number of the list, and then does a .Save(), so
the transformations won't be run again until another version change is
detected."""

__version__ = "$Revision: 539 $"

import re, string, types
import mm_cfg

def Update(l, stored_state):
    "Dispose of old vars and user options, mapping to new ones when suitable."
    # No worry about entirely new vars because InitVars() takes care of them.
    UpdateOldVars(l, stored_state)
    UpdateOldUsers(l)

def UpdateOldVars(l, stored_state):
    """Transform old variable values into new ones, deleting old ones.
    stored_state is last snapshot from file, as opposed to from InitVars()."""

    def PreferStored(oldname, newname, l=l, state=stored_state):
        "Use specified value if new value does not come from stored state."
        if hasattr(l, oldname):
            if not state.has_key(newname):
                setattr(l, newname, getattr(l, oldname))
            delattr(l, oldname)

    #                  Pre 1.0b1.2, klm 04/11/1998.
    #  - migrated vars:
    PreferStored('auto_subscribe', 'open_subscribe')
    PreferStored('closed', 'private_roster')
    PreferStored('mimimum_post_count_before_removal',
                 'mimimum_post_count_before_bounce_action')
    PreferStored('bad_posters', 'forbidden_posters')
    PreferStored('automatically_remove', 'automatic_bounce_action')
    #  - dropped vars:
    for a in ['archive_retain_text_copy',
              'archive_update_frequency',
              'archive_volume_frequency']:
        if hasattr(l, a): delattr(l, a)

def UpdateOldUsers(l):
    """Transform sense of changed user options."""
    if older(l.data_version, "1.0b1.2"):
        # Mime-digest bitfield changed from Enable to Disable after 1.0b1.1.
        for m in l.members + l.digest_members:
            was = l.GetUserOption(m, mm_cfg.DisableMime)
            l.SetUserOption(m, mm_cfg.DisableMime, not was)

def older(version, reference):
    """True if version is older than current.

    Different numbering systems imply version is older."""

    # Iterate over the repective contiguous sections of letters and digits
    # until a section from the reference is found to be different than the
    # corresponding version section, and return the sense of the
    # difference.  If no differences are found, then 0 is returned.
    for v, r in map(None, section(version), section(reference)):
        if r == None:
            # Reference is a full release and version is an interim - eg,
            # alpha or beta - which precede full, are older:
            return 1
        if type(v) != type(r):
            # Numbering system changed.
            return 1
        if v < r:
            return 1
        if v > r:
            return 0
    return 0
    
def section(s):
    """Split string into contiguous sequences of letters and digits."""
    section = ""
    got = []
    wasat = ""
    for c in s:
        if c in string.letters:
            at = string.letters; add = c
        elif c in string.digits:
            at = string.digits; add = c
        else:
            at = ""; add = ""

        if at == wasat:                 # In continuous sequence.
            section = section + add
        else:                           # Switching.
            if section:
                if wasat == string.digits:
                    section = int(section)
                got.append(section)
            section = add
            wasat = at
    if section:                         # Get trailing stuff.
        if wasat == string.digits:
            section = int(section)
        got.append(section)
    return got