summaryrefslogtreecommitdiffstats
path: root/BuildManager/rpmver.py
blob: cb3269cf184fbb4e385e691e81b29fa2497a65ea (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
# compare alpha and numeric segments of two versions
# return 1: first is newer than second
#        0: first and second are the same version
#       -1: second is newer than first
def rpmVersionCompare(e1, v1, r1, e2, v2, r2):
	if e1 and not e2:
		return 1
	if not e1 and e2:
		return -1
	if e1 and e2:
		if e1 < e2:
			return -1
		if e1 > e2:
			return 1
	rc = rpmvercmp(v1, v2)
	if rc: return rc
	return rpmvercmp(r1, r2)

# compare alpha and numeric segments of two versions
# return 1: a is newer than b
#        0: a and b are the same version
#       -1: b is newer than a
def rpmvercmp(a, b):
	if a == b:
		return 0
	ai = 0
	bi = 0
	la = len(a)
	lb = len(b)
	while ai < la and bi < lb:
		while ai < la and not a[ai].isalnum(): ai += 1
		while bi < lb and not b[bi].isalnum(): bi += 1
		aj = ai
		bj = bi
		if a[aj].isdigit():
			while aj < la and a[aj].isdigit(): aj += 1
			while bj < lb and b[bj].isdigit(): bj += 1
			isnum = 1
		else:
			while aj < la and a[aj].isalpha(): aj += 1
			while bj < lb and b[bj].isalpha(): bj += 1
			isnum = 0
		if aj == ai or bj == bi:
			return -1
		if isnum:
			while ai < la and a[ai] == '0': ai += 1
			while bi < lb and b[bi] == '0': bi += 1
			if aj-ai > bj-bi: return 1
			if bj-bi > aj-ai: return -1
		rc = cmp(a[ai:aj], b[bi:bj])
		if rc:
			return rc
		ai = aj
		bi = bj
	if ai == la and bi == lb:
		return 0
	if ai == la:
		return -1
	else:
		return 1

# vim:ts=4:sw=4