diff options
author | Benjamin Drung <bdrung@ubuntu.com> | 2009-08-04 01:37:38 +0200 |
---|---|---|
committer | Benjamin Drung <bdrung@ubuntu.com> | 2009-08-04 01:37:38 +0200 |
commit | 8cc86c290dcc62d179af9d5ab2c3b356db189eae (patch) | |
tree | 67a49a7e2f82c0a29812e81b821a909dfa34ae0e /src/moz-version | |
parent | 19d4b597f47fbac866f02ebc4228d8562561f32a (diff) |
rename moz-version-compare to moz-version. To compare two versions you have
to run moz-version with the --compare parameter. Add --help parameter to
show the usage help.
- rename src/moz-version-compare to src/moz-version
- update src/Makefile
Diffstat (limited to 'src/moz-version')
-rwxr-xr-x | src/moz-version | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/src/moz-version b/src/moz-version new file mode 100755 index 0000000..512ab8d --- /dev/null +++ b/src/moz-version @@ -0,0 +1,215 @@ +#!/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 getopt +import sys + +# error codes +COMMAND_LINE_SYNTAX_ERROR = 2 +INVALID_COMPARATOR = 3 +EMPTY_VERSION_STRING = 4 + +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, 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(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 + +comparators = ("lt", "le", "eq", "ne", "ge", "gt") + +def moz_version_compare(version1, comparator, version2, silent=False, verbose=False): + """Return true if the expression version1 comparator version2 is valid, otherwise false""" + if comparator not in comparators: + if not silent: + print >> sys.stderr, "E: The comparator " + comparator + \ + " is not valid. It should one of " + ", ".join(comparators) + "." + sys.exit(INVALID_COMPARATOR) + + if version1.strip() == "" or version2.strip() == "": + if not silent: + print >> sys.stderr, "E: At least one version string is empty." + sys.exit(EMPTY_VERSION_STRING) + + if verbose: + symbol = {"lt": "<", "le": "<=", "eq": "=", "ne": "!=", "ge": ">=", "gt": ">"} + print "I: Comparing %s %s %s." % (version1, symbol[comparator], version2) + + a = decode_version(version1, verbose) + b = decode_version(version2, verbose) + + if comparator == "lt": + return compare_versions(a, b) < 0 + elif comparator == "le": + return compare_versions(a, b) <= 0 + elif comparator == "eq": + return compare_versions(a, b) == 0 + elif comparator == "ne": + return compare_versions(a, b) != 0 + elif comparator == "ge": + return compare_versions(a, b) >= 0 + elif comparator == "gt": + return compare_versions(a, b) > 0 + + +def usage(output): + print >> output, """Usage: %s --compare version1 comparator version2 + + -h, --help display this help and exit + -s, --silent do not print anything and die silent on errors + -v, --verbose print more information + +comparator must be one of %s.""" % (sys.argv[0], ", ".join(comparators)) + + +if __name__ == "__main__": + try: + long_opts = ["compare", "help", "silent", "verbose"] + opts, args = getopt.gnu_getopt(sys.argv[1:], "chsv", long_opts) + except getopt.GetoptError, e: + # print help information and exit: + print >> sys.stderr, str(e) # will print something like "option -a not recognized" + usage(sys.stderr) + sys.exit(COMMAND_LINE_SYNTAX_ERROR) + + compare = False + silent = False + verbose = False + + for o, a in opts: + if o in ("-c", "--compare"): + compare = True + elif o in ("-h", "--help"): + usage(sys.stdout) + sys.exit() + elif o in ("-s", "--silent"): + silent = True + elif o in ("-v", "--verbose"): + verbose = True + else: + assert False, "unhandled option" + + if compare: + if len(args) != 3: + if not silent: + usage(sys.stderr) + sys.exit(COMMAND_LINE_SYNTAX_ERROR) + if moz_version_compare(args[0], args[1], args[2], silent, verbose): + if verbose: + print "I: Compare expression true." + sys.exit(0) + else: + if verbose: + print "I: Compare expression false." + sys.exit(1) + else: + if not silent: + print >> sys.stderr, "E: You should specify the command --compare." + usage(sys.stderr) + sys.exit(COMMAND_LINE_SYNTAX_ERROR) |