~mozillateam/mozilla-devscripts/mozilla-devscripts

359 by Benjamin Drung
Update and fix my email addresses.
1
# Copyright (c) 2009 Benjamin Drung <bdrung@debian.org>
286 by Benjamin Drung
- Move compare_versions from moz-version into separate module
2
#
3
# Permission is hereby granted, free of charge, to any person obtaining a copy
4
# of this software and associated documentation files (the "Software"), to deal
5
# in the Software without restriction, including without limitation the rights
6
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
# copies of the Software, and to permit persons to whom the Software is
8
# furnished to do so, subject to the following conditions:
9
#
10
# The above copyright notice and this permission notice shall be included in
11
# all copies or substantial portions of the Software.
12
# 
13
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
# THE SOFTWARE.
20
21
# Reference: https://developer.mozilla.org/en/Toolkit_version_format
22
23
import sys
24
25
def decode_part(part):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
26
    """Decodes a version part (like 5pre4) to
27
       <number-a><string-b><number-c><string-d>"""
28
    subpart = [0, "", 0, ""]
29
30
    # Split <number-a>
31
    length = 0
32
    for i in xrange(len(part)):
33
        if part[i].isdigit() or part[i] in ("-"):
34
            length += 1
35
        else:
36
            break
37
    if length > 0:
38
        subpart[0] = int(part[0:length])
39
    part = part[length:]
40
41
    # Split <string-b>
42
    length = 0
43
    for i in xrange(len(part)):
44
        if not (part[i].isdigit() or part[i] in ("-")):
45
            length += 1
46
        else:
47
            break
48
    subpart[1] = part[0:length]
49
    part = part[length:]
50
51
    # Split <number-c>
52
    length = 0
53
    for i in xrange(len(part)):
54
        if part[i].isdigit() or part[i] in ("-"):
55
            length += 1
56
        else:
57
            break
58
    if length > 0:
59
        subpart[2] = int(part[0:length])
60
    subpart[3] = part[length:]
61
62
    # if string-b is a plus sign, number-a is incremented to be compatible with
63
    # the Firefox 1.0.x version format: 1.0+ is the same as 1.1pre
64
    if subpart[1] == "+":
65
        subpart[0] += 1
66
        subpart[1] = "pre"
67
68
    # if the version part is a single asterisk, it is interpreted as an
69
    # infinitely-large number: 1.5.0.* is the same as 1.5.0.(infinity)
70
    if subpart[1] == "*":
71
        subpart[0] = sys.maxint
72
        subpart[1] = ""
73
74
    return subpart
286 by Benjamin Drung
- Move compare_versions from moz-version into separate module
75
76
def decode_version(version, verbose=False):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
77
    """Decodes a version string like 1.1pre1a"""
78
    parts = version.split(".")
79
    decoded_parts = map(decode_part, parts)
80
    if verbose:
81
        print "I: Split %s up into %s." % (version, decoded_parts)
82
    return decoded_parts
286 by Benjamin Drung
- Move compare_versions from moz-version into separate module
83
84
def compare_subpart((a, b)):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
85
    # A string-part that exists is always less-then a nonexisting string-part
86
    if a == "":
87
        if b == "":
88
            return 0
89
        else:
90
            return 1
91
    elif b == "":
92
        if a == "":
93
            return 0
94
        else:
95
            return -1
96
    else:
97
        return cmp(a, b)
286 by Benjamin Drung
- Move compare_versions from moz-version into separate module
98
99
def compare_part((x, y)):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
100
    compared_subparts = filter(lambda x: x != 0,
101
                               map(compare_subpart, zip(x, y)))
102
    if compared_subparts:
103
        return compared_subparts[0]
104
    else:
105
        return 0
286 by Benjamin Drung
- Move compare_versions from moz-version into separate module
106
107
def compare_versions(version1, version2, verbose=False):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
108
    a = decode_version(version1, verbose)
109
    b = decode_version(version2, verbose)
110
111
    if len(a) < len(b):
112
        a.extend((len(b) - len(a)) * [[0, "", 0, ""]])
113
    if len(b) < len(a):
114
        b.extend((len(a) - len(b)) * [[0, "", 0, ""]])
115
116
    result = filter(lambda x: x != 0, map(compare_part, zip(a, b)))
117
    if result:
118
        return result[0]
119
    else:
120
        return 0
286 by Benjamin Drung
- Move compare_versions from moz-version into separate module
121
122
def extract_upstream_version(debian_version):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
123
    # remove last part separated by a dash (1.0-2 -> 1.0)
124
    parts = debian_version.split('-')
125
    if len(parts) > 1:
126
        del parts[-1]
127
    upstream_version = '-'.join(parts)
128
129
    # remove epoch
130
    parts = upstream_version.split(':')
131
    if len(parts) > 1:
132
        del parts[0]
133
    upstream_version = ':'.join(parts)
134
135
    return upstream_version
303 by Benjamin Drung
* moz-version:
136
137
def convert_debian_to_moz_version(debian_version, verbose=False):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
138
    upstream_version = extract_upstream_version(debian_version)
139
140
    # compatibility: strip +nobinonly and +build
141
    parts = upstream_version.split('+')
142
    if len(parts) > 1 and parts[-1] == "nobinonly":
143
        del parts[-1]
144
    if len(parts) > 1 and parts[-1].startswith("build"):
145
        del parts[-1]
146
    upstream_version = '+'.join(parts)
147
148
    moz_version = upstream_version.replace("~", "")
149
    return moz_version
303 by Benjamin Drung
* moz-version:
150
151
def convert_moz_to_debian_version(moz_version, epoch=0, verbose=False):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
152
    parts = decode_version(moz_version, verbose)
153
    # tranform parts
154
    for i in xrange(len(parts)):
155
        (number_a, string_b, number_c, string_d) = parts[i]
156
        part = ""
157
        if string_d != "":
158
            part = "~" + string_d
159
        if number_c != 0 or string_d != "":
160
            part = str(number_c) + part
161
        if string_b != "":
162
            part = "~" + string_b + part
163
        parts[i] = str(number_a) + part
164
    debian_version = ".".join(parts)
165
    if epoch != 0:
166
        debian_version = str(epoch) + ":" + debian_version
167
    return debian_version