root / lib / utils / version.py @ 1eda3dd3
History | View | Annotate | Download (2.4 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Version utilities."""
|
23 |
|
24 |
import re |
25 |
|
26 |
_FULL_VERSION_RE = re.compile(r"(\d+)\.(\d+)\.(\d+)")
|
27 |
_SHORT_VERSION_RE = re.compile(r"(\d+)\.(\d+)")
|
28 |
|
29 |
# Format for CONFIG_VERSION:
|
30 |
# 01 03 0123 = 01030123
|
31 |
# ^^ ^^ ^^^^
|
32 |
# | | + Configuration version/revision
|
33 |
# | + Minor version
|
34 |
# + Major version
|
35 |
#
|
36 |
# It is stored as an integer. Make sure not to write an octal number.
|
37 |
|
38 |
# BuildVersion and SplitVersion must be in here because we can't import other
|
39 |
# modules. The cfgupgrade tool must be able to read and write version numbers
|
40 |
# and thus requires these functions. To avoid code duplication, they're kept in
|
41 |
# here.
|
42 |
|
43 |
|
44 |
def BuildVersion(major, minor, revision): |
45 |
"""Calculates int version number from major, minor and revision numbers.
|
46 |
|
47 |
Returns: int representing version number
|
48 |
|
49 |
"""
|
50 |
assert isinstance(major, int) |
51 |
assert isinstance(minor, int) |
52 |
assert isinstance(revision, int) |
53 |
return (1000000 * major + |
54 |
10000 * minor +
|
55 |
1 * revision)
|
56 |
|
57 |
|
58 |
def SplitVersion(version): |
59 |
"""Splits version number stored in an int.
|
60 |
|
61 |
Returns: tuple; (major, minor, revision)
|
62 |
|
63 |
"""
|
64 |
assert isinstance(version, int) |
65 |
|
66 |
(major, remainder) = divmod(version, 1000000) |
67 |
(minor, revision) = divmod(remainder, 10000) |
68 |
|
69 |
return (major, minor, revision)
|
70 |
|
71 |
|
72 |
def ParseVersion(versionstring): |
73 |
"""Parses a version string.
|
74 |
|
75 |
@param versionstring: the version string to parse
|
76 |
@type versionstring: string
|
77 |
@rtype: tuple or None
|
78 |
@return: (major, minor, revision) if parsable, None otherwise.
|
79 |
|
80 |
"""
|
81 |
m = _FULL_VERSION_RE.match(versionstring) |
82 |
if m is not None: |
83 |
return (int(m.group(1)), int(m.group(2)), int(m.group(3))) |
84 |
|
85 |
m = _SHORT_VERSION_RE.match(versionstring) |
86 |
if m is not None: |
87 |
return (int(m.group(1)), int(m.group(2)), 0) |
88 |
|
89 |
return None |