summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Drung <bdrung@ubuntu.com>2009-08-04 01:37:38 +0200
committerBenjamin Drung <bdrung@ubuntu.com>2009-08-04 01:37:38 +0200
commit8cc86c290dcc62d179af9d5ab2c3b356db189eae (patch)
tree67a49a7e2f82c0a29812e81b821a909dfa34ae0e
parent19d4b597f47fbac866f02ebc4228d8562561f32a (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
-rw-r--r--src/Makefile2
-rwxr-xr-xsrc/moz-version (renamed from src/moz-version-compare)112
2 files changed, 92 insertions, 22 deletions
diff --git a/src/Makefile b/src/Makefile
index 09421f3..51e8ede 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -53,7 +53,7 @@ extra_files = \
bindir_files = \
med-xpi-pack \
med-xpi-unpack \
- moz-version-compare
+ moz-version
extra_dirs = \
mozclient \
diff --git a/src/moz-version-compare b/src/moz-version
index 6301053..512ab8d 100755
--- a/src/moz-version-compare
+++ b/src/moz-version
@@ -22,8 +22,14 @@
# 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,""]
@@ -74,13 +80,15 @@ def decode_part(part):
return subpart
-def decode_version(version):
+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((a, b)):
+def compare_subpart((a, b)):
# A string-part that exists is always less-then a nonexisting string-part
if a == "":
if b == "":
@@ -96,7 +104,7 @@ def compare((a, b)):
return cmp(a, b)
def compare_part((x, y)):
- compared_subparts = filter(lambda x: x != 0, map(compare, zip(x, y)))
+ compared_subparts = filter(lambda x: x != 0, map(compare_subpart, zip(x, y)))
if compared_subparts:
return compared_subparts[0]
else:
@@ -114,32 +122,94 @@ def compare_versions(a, b):
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)
+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)
- a = decode_version(version1)
- b = decode_version(version2)
+ if verbose:
+ symbol = {"lt": "<", "le": "<=", "eq": "=", "ne": "!=", "ge": ">=", "gt": ">"}
+ print "I: Comparing %s %s %s." % (version1, symbol[comparator], version2)
- if operator == "lt":
+ a = decode_version(version1, verbose)
+ b = decode_version(version2, verbose)
+
+ if comparator == "lt":
return compare_versions(a, b) < 0
- elif operator == "le":
+ elif comparator == "le":
return compare_versions(a, b) <= 0
- elif operator == "eq":
+ elif comparator == "eq":
return compare_versions(a, b) == 0
- elif operator == "ne":
+ elif comparator == "ne":
return compare_versions(a, b) != 0
- elif operator == "ge":
+ elif comparator == "ge":
return compare_versions(a, b) >= 0
- elif operator == "gt":
+ 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__":
- if moz_compare_versions(sys.argv[1], sys.argv[2], sys.argv[3]):
- sys.exit(0)
+ 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:
- sys.exit(1)
-
+ if not silent:
+ print >> sys.stderr, "E: You should specify the command --compare."
+ usage(sys.stderr)
+ sys.exit(COMMAND_LINE_SYNTAX_ERROR)