summaryrefslogtreecommitdiff
path: root/src/moz-version
blob: ddf4ac5a7b88b9a892b55f115a1c6d78bec31abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/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.

import getopt
import sys

from moz_version import compare_versions

# 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


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)