summaryrefslogtreecommitdiff
path: root/moz_version.py
blob: 86ed727f924c95be2752163a66a6ba2630f34d76 (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
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
# Copyright (c) 2009-2011, Benjamin Drung <bdrung@debian.org>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Reference: https://developer.mozilla.org/en/Toolkit_version_format

import sys


def decode_part(part):
    """Decodes a version part (like 5pre4) to
       <number-a><string-b><number-c><string-d>"""
    subpart = [0, "", 0, ""]

    # Split <number-a>
    length = 0
    for i in xrange(len(part)):
        if part[i].isdigit() or part[i] == "-":
            length += 1
        else:
            break
    if length > 0:
        subpart[0] = int(part[0:length])
    part = part[length:]

    # Split <string-b>
    length = 0
    for i in xrange(len(part)):
        if not (part[i].isdigit() or part[i] == "-"):
            length += 1
        else:
            break
    subpart[1] = part[0:length]
    part = part[length:]

    # Split <number-c>
    length = 0
    for i in xrange(len(part)):
        if part[i].isdigit() or part[i] == "-":
            length += 1
        else:
            break
    if length > 0:
        subpart[2] = int(part[0:length])
    subpart[3] = part[length:]

    # if string-b is a plus sign, number-a is incremented to be compatible with
    # the Firefox 1.0.x version format: 1.0+ is the same as 1.1pre
    if subpart[1] == "+":
        subpart[0] += 1
        subpart[1] = "pre"

    # if the version part is a single asterisk, it is interpreted as an
    # infinitely-large number: 1.5.0.* is the same as 1.5.0.(infinity)
    if subpart[1] == "*":
        subpart[0] = sys.maxint
        subpart[1] = ""

    return subpart


def decode_version(version, verbose=False):
    """Decodes a version string like 1.1pre1a"""
    parts = version.split(".")
    decoded_parts = map(decode_part, parts)
    if verbose:
        print "I: Split %s up into %s." % (version, decoded_parts)
    return decoded_parts


def compare_subpart((a, b)):
    # A string-part that exists is always less-then a nonexisting string-part
    if a == "":
        if b == "":
            return 0
        else:
            return 1
    elif b == "":
        if a == "":
            return 0
        else:
            return -1
    else:
        return cmp(a, b)


def compare_part((x, y)):
    compared_subparts = filter(lambda x: x != 0,
                               map(compare_subpart, zip(x, y)))
    if compared_subparts:
        return compared_subparts[0]
    else:
        return 0


def compare_versions(version1, version2, verbose=False):
    a = decode_version(version1, verbose)
    b = decode_version(version2, verbose)

    if len(a) < len(b):
        a.extend((len(b) - len(a)) * [[0, "", 0, ""]])
    if len(b) < len(a):
        b.extend((len(a) - len(b)) * [[0, "", 0, ""]])

    result = filter(lambda x: x != 0, map(compare_part, zip(a, b)))
    if result:
        return result[0]
    else:
        return 0


def extract_upstream_version(debian_version):
    # remove last part separated by a dash (1.0-2 -> 1.0)
    parts = debian_version.split('-')
    if len(parts) > 1:
        del parts[-1]
    upstream_version = '-'.join(parts)

    # remove epoch
    parts = upstream_version.split(':')
    if len(parts) > 1:
        del parts[0]
    upstream_version = ':'.join(parts)

    return upstream_version


def _convert_part_to_debian(part):
    """Converts a Mozilla version part (like 5pre4) to a Debian version."""
    (number_a, string_b, number_c, string_d) = part
    debian_version = ""
    if string_d != "":
        debian_version = "~" + string_d
    if number_c != 0 or string_d != "":
        debian_version = str(number_c) + debian_version
    if string_b != "":
        debian_version = "~" + string_b + debian_version
    debian_version = str(number_a) + debian_version
    return debian_version


def convert_debian_to_moz_version(debian_version):
    upstream_version = extract_upstream_version(debian_version)

    # compatibility: strip +nobinonly and +build
    parts = upstream_version.split('+')
    if len(parts) > 1 and parts[-1] == "nobinonly":
        del parts[-1]
    if len(parts) > 1 and parts[-1].startswith("build"):
        del parts[-1]
    upstream_version = '+'.join(parts)

    moz_version = upstream_version.replace("~", "")
    return moz_version


def convert_moz_to_debian_version(moz_version, epoch=0, verbose=False):
    parts = decode_version(moz_version, verbose)
    # tranform parts
    parts = [_convert_part_to_debian(p) for p in parts]
    debian_version = ".".join(parts)
    if epoch != 0:
        debian_version = str(epoch) + ":" + debian_version
    return debian_version


def moz_to_next_debian_version(moz_version, epoch=0, verbose=False):
    """Convert a given Mozilla version to the next Debian version.

    Compared to convert_moz_to_debian_version it does following:
    * append 0 to a trailing letter, or
    * append + to a trailing number, or
    * replace a trailing * with +.

    Examples:
    9.0a => 9.0~a0
    9.0a1 => 9.0~a1+
    9.0 => 9.0+
    9.0.* => 9.0.+
    """
    parts = decode_version(moz_version, verbose)
    # tranform last parts
    (number_a, string_b, number_c, string_d) = parts[-1]
    last_part = ""
    if string_d != "":
        last_part = "~" + string_d + "0"
    if number_c != 0 or string_d != "":
        if last_part:
            last_part = str(number_c) + last_part
        else:
            if number_c == sys.maxint:
                last_part = "+"
            else:
                last_part = str(number_c) + "+"
    if string_b != "":
        if last_part:
            last_part = "~" + string_b + last_part
        else:
            last_part = "~" + string_b + "0"
    if last_part:
        last_part = str(number_a) + last_part
    else:
        if number_a == sys.maxint:
            last_part = "+"
        else:
            last_part = str(number_a) + "+"

    parts = [_convert_part_to_debian(p) for p in parts[:-1]] + [last_part]
    debian_version = ".".join(parts)
    if epoch != 0:
        debian_version = str(epoch) + ":" + debian_version
    return debian_version