~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to python/plugins/plugin_installer/version_compare.py

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
This is a Python module to compare version numbers. It's case insensitive
 
3
and recognizes all major notations, prefixes (ver. and version), delimiters
 
4
(. - and _) and suffixes (alpha, beta, rc, preview and trunk).
 
5
 
 
6
Usage: compareVersions(version1, version2)
 
7
 
 
8
The function accepts arguments of any type convertable to unicode string
 
9
and returns integer value:
 
10
0 - the versions are equal
 
11
1 - version 1 is higher
 
12
2 - version 2 is higher
 
13
 
 
14
-----------------------------------------------------------------------------
 
15
HOW IT WORKS...
 
16
First, both arguments are converted to uppercase unicode and stripped of
 
17
'VERSION' or 'VER.' prefix. Then they are chopped into a list of particular
 
18
numeric and alphabetic elements. The dots, dashes and underlines are recognized
 
19
as delimiters. Also numbers and non numbers are separated. See example below:
 
20
 
 
21
'Ver 0.03-120_rc7foo' is converted to ['0','03','120','RC','7','FOO']
 
22
 
 
23
Then every pair of elements, from left to right, is compared as string
 
24
or as number to provide the best result (you know, 11>9 but also '03'>'007').
 
25
The comparing stops when one of elements is greater. If comparing achieves
 
26
the end of the shorter list and the matter is still unresolved, the longer
 
27
list is usually recognized as higher, except following suffixes:
 
28
ALPHA, BETA, RC, PREVIEW and TRUNK which make the version number lower.
 
29
 
 
30
/***************************************************************************
 
31
 *                                                                         *
 
32
 *   Copyright (C) 2008-11-24 Borys Jurgiel                                *
 
33
 *                                                                         *
 
34
 ***************************************************************************
 
35
 *                                                                         *
 
36
 *   This program is free software; you can redistribute it and/or modify  *
 
37
 *   it under the terms of the GNU General Public License as published by  *
 
38
 *   the Free Software Foundation; either version 2 of the License, or     *
 
39
 *   (at your option) any later version.                                   *
 
40
 *                                                                         *
 
41
 ***************************************************************************/
 
42
"""
 
43
 
 
44
# ------------------------------------------------------------------------ #
 
45
def normalizeVersion(s):
 
46
  """ remove possible prefix from given string and convert to uppercase """
 
47
  prefixes = ['VERSION','VER.','VER','V.','V','REVISION','REV.','REV','R.','R']
 
48
  if not s:
 
49
    return unicode()
 
50
  s = unicode(s).upper()
 
51
  for i in prefixes:
 
52
    if s[:len(i)] == i:
 
53
      s = s.replace(i,'')
 
54
  s = s.strip()
 
55
  return s
 
56
 
 
57
 
 
58
# ------------------------------------------------------------------------ #
 
59
def classifyCharacter(c):
 
60
  """ return 0 for delimiter, 1 for digit and 2 for alphabetic character """
 
61
  if c in [".","-","_"," "]:
 
62
    return 0
 
63
  if c.isdigit():
 
64
    return 1
 
65
  else:
 
66
    return 2
 
67
 
 
68
 
 
69
# ------------------------------------------------------------------------ #
 
70
def chopString(s):
 
71
  """ convert string to list of numbers and words """
 
72
  l = [s[0]]
 
73
  for i in range(1,len(s)):
 
74
    if classifyCharacter(s[i]) == 0:
 
75
      pass
 
76
    elif classifyCharacter(s[i]) == classifyCharacter(s[i-1]):
 
77
      l[len(l)-1] += s[i]
 
78
    else:
 
79
      l += [s[i]]
 
80
  return l
 
81
 
 
82
 
 
83
# ------------------------------------------------------------------------ #
 
84
def compareElements(s1,s2):
 
85
  """ compare two particular elements """
 
86
  # check if the matter is easy solvable:
 
87
  if s1 == s2:
 
88
    return 0
 
89
  # try to compare as numeric values (but only if the first character is not 0):
 
90
  if s1 and s2 and s1.isnumeric() and s2.isnumeric() and s1[0] != '0' and s2[0] != '0':
 
91
    if float(s1) == float(s2):
 
92
      return 0
 
93
    elif float(s1) > float(s2):
 
94
      return 1
 
95
    else:
 
96
      return 2
 
97
  # if the strings aren't numeric or start from 0, compare them as a strings:
 
98
  # but first, set ALPHA < BETA < PREVIEW < RC < TRUNK < [NOTHING] < [ANYTHING_ELSE]
 
99
  if not s1 in ['ALPHA','BETA','PREVIEW','RC','TRUNK']:
 
100
    s1 = 'Z' + s1
 
101
  if not s2 in ['ALPHA','BETA','PREVIEW','RC','TRUNK']:
 
102
    s2 = 'Z' + s2
 
103
  # the final test:
 
104
  if s1 > s2:
 
105
    return 1
 
106
  else:
 
107
    return 2
 
108
 
 
109
 
 
110
# ------------------------------------------------------------------------ #
 
111
def compareVersions(a,b):
 
112
  """ Compare two version numbers. Return 0 if a==b or error, 1 if a<b and 2 if b>a """
 
113
  if not a or not b:
 
114
    return 0
 
115
  a = normalizeVersion(a)
 
116
  b = normalizeVersion(b)
 
117
  if a == b:
 
118
    return 0
 
119
  # convert the strings to the lists
 
120
  v1 = chopString(a)
 
121
  v2 = chopString(b)
 
122
  # set the shorter string as a base
 
123
  l = len(v1)
 
124
  if l > len(v2):
 
125
    l = len(v2)
 
126
  # try to determine within the common length
 
127
  for i in range(l):
 
128
    if compareElements(v1[i],v2[i]):
 
129
      return compareElements(v1[i],v2[i])
 
130
  # if the lists are identical till the end of the shorther string, try to compare the odd tail
 
131
  #with the simple space (because the 'alpha', 'beta', 'preview' and 'rc' are LESS then nothing)
 
132
  if len(v1) > l:
 
133
    return compareElements(v1[l],u' ')
 
134
  if len(v2) > l:
 
135
    return compareElements(u' ',v2[l])
 
136
  # if everything else fails...
 
137
  if a > b:
 
138
    return 1
 
139
  else:
 
140
    return 2