diff options
Diffstat (limited to 'src/moz-version')
-rwxr-xr-x | src/moz-version | 209 |
1 files changed, 110 insertions, 99 deletions
diff --git a/src/moz-version b/src/moz-version index cea49b7..85ec81a 100755 --- a/src/moz-version +++ b/src/moz-version @@ -24,49 +24,55 @@ import getopt import os import sys -from moz_version import compare_versions, convert_debian_to_moz_version, convert_moz_to_debian_version +from moz_version import (compare_versions, convert_debian_to_moz_version, + convert_moz_to_debian_version) # error codes COMMAND_LINE_SYNTAX_ERROR = 2 INVALID_COMPARATOR = 3 EMPTY_VERSION_STRING = 4 -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) - - if comparator == "lt": - return compare_versions(version1, version2, verbose) < 0 - elif comparator == "le": - return compare_versions(version1, version2, verbose) <= 0 - elif comparator == "eq": - return compare_versions(version1, version2, verbose) == 0 - elif comparator == "ne": - return compare_versions(version1, version2, verbose) != 0 - elif comparator == "ge": - return compare_versions(version1, version2, verbose) >= 0 - elif comparator == "gt": - return compare_versions(version1, version2, verbose) > 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) + + if comparator == "lt": + return compare_versions(version1, version2, verbose) < 0 + elif comparator == "le": + return compare_versions(version1, version2, verbose) <= 0 + elif comparator == "eq": + return compare_versions(version1, version2, verbose) == 0 + elif comparator == "ne": + return compare_versions(version1, version2, verbose) != 0 + elif comparator == "ge": + return compare_versions(version1, version2, verbose) >= 0 + elif comparator == "gt": + return compare_versions(version1, version2, verbose) > 0 def usage(output): - name = os.path.basename(sys.argv[0]) - print >> output, """Usage: %s [options] action + name = os.path.basename(sys.argv[0]) + print >> output, """Usage: %s [options] action Actions: -c, --compare version1 comparator version2 @@ -80,70 +86,75 @@ Options: -s, --silent do not print anything and die silent on errors -v, --verbose print more information -See %s(1) for more info.""" % (name, ", ".join(comparators), name) - +See %s(1) for more info.""" % (name, ", ".join(COMPARATORS), name) + + +def main(): + try: + long_opts = ["compare", "help", "silent", "to-deb", "to-moz", "verbose"] + opts, args = getopt.gnu_getopt(sys.argv[1:], "cdhmsv", long_opts) + except getopt.GetoptError, e: + # print help information and exit: + print >> sys.stderr, str(e) + usage(sys.stderr) + sys.exit(COMMAND_LINE_SYNTAX_ERROR) + + actions = set() + silent = False + verbose = False + + for o, _ in opts: + if o in ("-c", "--compare"): + actions.add("compare") + elif o in ("-d", "--to-deb"): + actions.add("to-deb") + elif o in ("-h", "--help"): + usage(sys.stdout) + sys.exit() + elif o in ("-m", "--to-moz"): + actions.add("to-moz") + elif o in ("-s", "--silent"): + silent = True + elif o in ("-v", "--verbose"): + verbose = True + else: + assert False, "unhandled option" + + if len(actions) != 1: + if not silent: + print >> sys.stderr, "E: You must specify an action." + usage(sys.stderr) + sys.exit(COMMAND_LINE_SYNTAX_ERROR) + + action = actions.pop() + + if action == "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) + elif action == "to-deb": + if len(args) != 1: + if not silent: + print >> sys.stderr, "E: The action --to-deb takes exactly " + \ + "one argument." + sys.exit(COMMAND_LINE_SYNTAX_ERROR) + print convert_moz_to_debian_version(args[0], 0, verbose) + elif action == "to-moz": + if len(args) != 1: + if not silent: + print >> sys.stderr, "E: The action --to-moz takes exactly " + \ + "one argument." + sys.exit(COMMAND_LINE_SYNTAX_ERROR) + print convert_debian_to_moz_version(args[0], verbose) if __name__ == "__main__": - try: - long_opts = ["compare", "help", "silent", "to-deb", "to-moz", "verbose"] - opts, args = getopt.gnu_getopt(sys.argv[1:], "cdhmsv", 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) - - actions = set() - silent = False - verbose = False - - for o, a in opts: - if o in ("-c", "--compare"): - actions.add("compare") - elif o in ("-d", "--to-deb"): - actions.add("to-deb") - elif o in ("-h", "--help"): - usage(sys.stdout) - sys.exit() - elif o in ("-m", "--to-moz"): - actions.add("to-moz") - elif o in ("-s", "--silent"): - silent = True - elif o in ("-v", "--verbose"): - verbose = True - else: - assert False, "unhandled option" - - if len(actions) != 1: - if not silent: - print >> sys.stderr, "E: You must specify an action." - usage(sys.stderr) - sys.exit(COMMAND_LINE_SYNTAX_ERROR) - - action = actions.pop() - - if action == "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) - elif action == "to-deb": - if len(args) != 1: - if not silent: - print >> sys.stderr, "E: The action --to-deb takes exactly one argument." - sys.exit(COMMAND_LINE_SYNTAX_ERROR) - print convert_moz_to_debian_version(args[0], 0, verbose) - elif action == "to-moz": - if len(args) != 1: - if not silent: - print >> sys.stderr, "E: The action --to-moz takes exactly one argument." - sys.exit(COMMAND_LINE_SYNTAX_ERROR) - print convert_debian_to_moz_version(args[0], verbose) + main() |