diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 1 | ||||
-rwxr-xr-x | src/moz-version-compare | 145 |
2 files changed, 146 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile index 23f324d..6e6b360 100644 --- a/src/Makefile +++ b/src/Makefile @@ -47,6 +47,7 @@ subst_files = \ extra_files = \ xpi.mk \ lp-locale-export.mk \ + moz-version-compare \ $(NULL) extra_dirs = \ diff --git a/src/moz-version-compare b/src/moz-version-compare new file mode 100755 index 0000000..6301053 --- /dev/null +++ b/src/moz-version-compare @@ -0,0 +1,145 @@ +#!/usr/bin/python + +# Copyright (c) 2009 Benjamin Drung <bdrung@ubuntu.com> +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE 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] in ("-"): + 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] in ("-")): + 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] in ("-"): + 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): + """Decodes a version string like 1.1pre1a""" + parts = version.split(".") + decoded_parts = map(decode_part, parts) + return decoded_parts + +def compare((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, zip(x, y))) + if compared_subparts: + return compared_subparts[0] + else: + return 0 + +def compare_versions(a, b): + 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 moz_compare_versions(version1, operator, version2): + """Return true if the expression version1 operator version2 is valid, otherwise false""" + operators = ("lt", "le", "eq", "ne", "ge", "gt") + if operator not in operators: + print "E: The operator " + operator + " is not valid. It should one of " + ", ".join(operators) + "." + sys.exit(2) + + a = decode_version(version1) + b = decode_version(version2) + + if operator == "lt": + return compare_versions(a, b) < 0 + elif operator == "le": + return compare_versions(a, b) <= 0 + elif operator == "eq": + return compare_versions(a, b) == 0 + elif operator == "ne": + return compare_versions(a, b) != 0 + elif operator == "ge": + return compare_versions(a, b) >= 0 + elif operator == "gt": + return compare_versions(a, b) > 0 + +if __name__ == "__main__": + if moz_compare_versions(sys.argv[1], sys.argv[2], sys.argv[3]): + sys.exit(0) + else: + sys.exit(1) + |