[IMP] Update the copyright to 2009
[odoo/odoo.git] / bin / tools / parse_version.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 ## this functions are taken from the setuptools package (version 0.6c8)
24 ## http://peak.telecommunity.com/DevCenter/PkgResources#parsing-utilities
25
26 import re
27
28 component_re = re.compile(r'(\d+ | [a-z]+ | \.| -)', re.VERBOSE)
29 replace = {'pre':'c', 'preview':'c','-':'final-','_':'final-','rc':'c','dev':'@'}.get
30
31 def _parse_version_parts(s):
32     for part in component_re.split(s):
33         part = replace(part,part)
34         if not part or part=='.':
35             continue
36         if part[:1] in '0123456789':
37             yield part.zfill(8)    # pad for numeric comparison
38         else:
39             yield '*'+part
40
41     yield '*final'  # ensure that alpha/beta/candidate are before final
42
43 def parse_version(s):
44     """Convert a version string to a chronologically-sortable key
45
46     This is a rough cross between distutils' StrictVersion and LooseVersion;
47     if you give it versions that would work with StrictVersion, then it behaves
48     the same; otherwise it acts like a slightly-smarter LooseVersion. It is
49     *possible* to create pathological version coding schemes that will fool
50     this parser, but they should be very rare in practice.
51
52     The returned value will be a tuple of strings.  Numeric portions of the
53     version are padded to 8 digits so they will compare numerically, but
54     without relying on how numbers compare relative to strings.  Dots are
55     dropped, but dashes are retained.  Trailing zeros between alpha segments
56     or dashes are suppressed, so that e.g. "2.4.0" is considered the same as
57     "2.4". Alphanumeric parts are lower-cased.
58
59     The algorithm assumes that strings like "-" and any alpha string that
60     alphabetically follows "final"  represents a "patch level".  So, "2.4-1"
61     is assumed to be a branch or patch of "2.4", and therefore "2.4.1" is
62     considered newer than "2.4-1", whic in turn is newer than "2.4".
63
64     Strings like "a", "b", "c", "alpha", "beta", "candidate" and so on (that
65     come before "final" alphabetically) are assumed to be pre-release versions,
66     so that the version "2.4" is considered newer than "2.4a1".
67
68     Finally, to handle miscellaneous cases, the strings "pre", "preview", and
69     "rc" are treated as if they were "c", i.e. as though they were release
70     candidates, and therefore are not as new as a version string that does not
71     contain them.
72     """
73     parts = []
74     for part in _parse_version_parts((s or '0.1').lower()):
75         if part.startswith('*'):
76             if part<'*final':   # remove '-' before a prerelease tag
77                 while parts and parts[-1]=='*final-': parts.pop()
78             # remove trailing zeros from each series of numeric parts
79             while parts and parts[-1]=='00000000':
80                 parts.pop()
81         parts.append(part)
82     return tuple(parts)
83
84 if __name__ == '__main__':
85         pvs = []
86         for v in ('0', '4.2', '4.2.3.4', '5.0.0-alpha', '5.0.0-rc1', '5.0.0-rc1.1', '5.0.0_rc2', '5.0.0_rc3', '5.0.0'):
87                 pv = parse_version(v)
88                 print v, pv
89                 pvs.append(pv)
90
91         def cmp(a, b):
92                 print a, b
93                 assert(a < b)
94                 return b
95
96         reduce(cmp, pvs)
97