xref: /aosp_15_r20/external/autotest/client/common_lib/kernel_versions.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2#
3# kernel_versions.py -- linux kernel version comparisons
4#
5from __future__ import absolute_import
6from __future__ import division
7from __future__ import print_function
8__author__ = """Copyright Andy Whitcroft 2007"""
9
10import sys,re
11
12from six.moves import range
13
14#
15# Sort key for ordering versions chronologically.  The key ordering
16# problem is between that introduced by -rcN.  These come _before_
17# their accompanying version.
18#
19#   2.6.0 -> 2.6.1-rc1 -> 2.6.1
20#
21# In order to sort them we convert all non-rc releases to a pseudo
22# -rc99 release.  We also convert all numbers to two digits.  The
23# result is then sortable textually.
24#
25#   02.06.00-rc99 -> 02.06.01-rc01 -> 02.06.01-rc99
26#
27encode_sep = re.compile(r'(\D+)')
28
29def version_encode(version):
30    bits = encode_sep.split(version)
31    n = 9
32    if len(bits[0]) == 0:
33        n += 2
34    if len(bits) == n or (len(bits) > n and bits[n] != '_rc'):
35        # Insert missing _rc99 after 2 . 6 . 18 -smp- 220 . 0
36        bits.insert(n, '_rc')
37        bits.insert(n+1, '99')
38    n = 5
39    if len(bits[0]) == 0:
40        n += 2
41    if len(bits) <= n or bits[n] != '-rc':
42        bits.insert(n, '-rc')
43        bits.insert(n+1, '99')
44    for n in range(0, len(bits), 2):
45        if len(bits[n]) == 1:
46            bits[n] = '0' + bits[n]
47
48    return ''.join(bits)
49
50
51def version_limit(version, n):
52    bits = encode_sep.split(version)
53    return ''.join(bits[0:n])
54
55
56def version_len(version):
57    return len(encode_sep.split(version))
58
59#
60# Given a list of versions find the nearest version which is deemed
61# less than or equal to the target.  Versions are in linux order
62# as follows:
63#
64#   2.6.0 -> 2.6.1 -> 2.6.2-rc1 -> 2.6.2-rc2 -> 2.6.2 -> 2.6.3-rc1
65#              |        |\
66#              |        | 2.6.2-rc1-mm1 -> 2.6.2-rc1-mm2
67#              |        \
68#              |         2.6.2-rc1-ac1 -> 2.6.2-rc1-ac2
69#              \
70#               2.6.1-mm1 -> 2.6.1-mm2
71#
72# Note that a 2.6.1-mm1 is not a predecessor of 2.6.2-rc1-mm1.
73#
74def version_choose_config(version, candidates):
75    # Check if we have an exact match ... if so magic
76    if version in candidates:
77        return version
78
79    # Sort the search key into the list ordered by 'age'
80    deco = [ (version_encode(v), i, v) for i, v in
81                                    enumerate(candidates + [ version ]) ]
82    deco.sort()
83    versions = [ v for _, _, v in deco ]
84
85    # Everything sorted below us is of interst.
86    for n in range(len(versions) - 1, -1, -1):
87        if versions[n] == version:
88            break
89    n -= 1
90
91    # Try ever shorter 'prefixes' 2.6.20-rc3-mm, 2.6.20-rc, 2.6. etc
92    # to match against the ordered list newest to oldest.
93    length = version_len(version) - 1
94    version = version_limit(version, length)
95    while length > 1:
96        for o in range(n, -1, -1):
97            if version_len(versions[o]) == (length + 1) and \
98                                version_limit(versions[o], length) == version:
99                return versions[o]
100        length -= 2
101        version = version_limit(version, length)
102
103    return None
104
105
106def is_released_kernel(version):
107    # True if version name suggests a released kernel,
108    #   not some release candidate or experimental kernel name
109    #   e.g.  2.6.18-smp-200.0  includes no other text, underscores, etc
110    version = version.strip('01234567890.-')
111    return version in ['', 'smp', 'smpx', 'pae']
112
113
114def is_release_candidate(version):
115    # True if version names a released kernel or release candidate,
116    #   not some experimental name containing arbitrary text
117    #   e.g.  2.6.18-smp-220.0_rc3  but not  2.6.18_patched
118    version = re.sub(r'[_-]rc\d+', '', version)
119    return is_released_kernel(version)
120