apw | 2366d99 | 2007-03-12 20:35:57 +0000 | [diff] [blame^] | 1 | # |
| 2 | # kernel_versions.py -- linux kernel version comparisons |
| 3 | # |
| 4 | __author__ = """Copyright Andy Whitcroft 2007""" |
| 5 | |
| 6 | import sys,re |
| 7 | |
| 8 | # |
| 9 | # Sort key for ordering versions chronologically. The key ordering |
| 10 | # problem is between that introduced by -rcN. These come _before_ |
| 11 | # their accompanying version. |
| 12 | # |
| 13 | # 2.6.0 -> 2.6.1-rc1 -> 2.6.1 |
| 14 | # |
| 15 | # In order to sort them we convert all non-rc releases to a pseudo |
| 16 | # -rc99 release. We also convert all numbers to two digits. The |
| 17 | # result is then sortable textually. |
| 18 | # |
| 19 | # 02.06.00-rc99 -> 02.06.01-rc01 -> 02.06.01-rc99 |
| 20 | # |
| 21 | encode_sep = re.compile('(\D+)') |
| 22 | def version_encode(version): |
| 23 | bits = encode_sep.split(version) |
| 24 | if len(bits) < 6 or bits[5] != '-rc': |
| 25 | bits.insert(5, '99') |
| 26 | bits.insert(5, '-rc') |
| 27 | for n in range(0, len(bits), 2): |
| 28 | if len(bits[n]) < 2: |
| 29 | bits[n] = '0' + bits[n] |
| 30 | |
| 31 | return ''.join(bits) |
| 32 | |
| 33 | |
| 34 | def version_limit(version, n): |
| 35 | bits = encode_sep.split(version) |
| 36 | return ''.join(bits[0:n]) |
| 37 | |
| 38 | |
| 39 | def version_len(version): |
| 40 | return len(encode_sep.split(version)) |
| 41 | |
| 42 | # |
| 43 | # Given a list of versions find the nearest version which is deemed |
| 44 | # less than or equal to the target. Versions are in linux order |
| 45 | # as follows: |
| 46 | # |
| 47 | # 2.6.0 -> 2.6.1 -> 2.6.2-rc1 -> 2.6.2-rc2 -> 2.6.2 -> 2.6.3-rc1 |
| 48 | # | |\ |
| 49 | # | | 2.6.2-rc1-mm1 -> 2.6.2-rc1-mm2 |
| 50 | # | \ |
| 51 | # | 2.6.2-rc1-ac1 -> 2.6.2-rc1-ac2 |
| 52 | # \ |
| 53 | # 2.6.1-mm1 -> 2.6.1-mm2 |
| 54 | # |
| 55 | # Note that a 2.6.1-mm1 is not a predecessor of 2.6.2-rc1-mm1. |
| 56 | # |
| 57 | def version_choose_config(version, candidates): |
| 58 | # Check if we have an exact match ... if so magic |
| 59 | if version in candidates: |
| 60 | return version |
| 61 | |
| 62 | # Sort the search key into the list ordered by 'age' |
| 63 | deco = [ (version_encode(v), i, v) for i, v in |
| 64 | enumerate(candidates + [ version ]) ] |
| 65 | deco.sort() |
| 66 | versions = [ v for _, _, v in deco ] |
| 67 | |
| 68 | # Everything sorted below us is of interst. |
| 69 | for n in range(len(versions) - 1, -1, -1): |
| 70 | if versions[n] == version: |
| 71 | break |
| 72 | n -= 1 |
| 73 | |
| 74 | # Try ever shorter 'prefixes' 2.6.20-rc3-mm, 2.6.20-rc, 2.6. etc |
| 75 | # to match against the ordered list newest to oldest. |
| 76 | length = version_len(version) - 1 |
| 77 | version = version_limit(version, length) |
| 78 | while length > 1: |
| 79 | for o in range(n, -1, -1): |
| 80 | if version_len(versions[o]) == (length + 1) and \ |
| 81 | version_limit(versions[o], length) == version: |
| 82 | return versions[o] |
| 83 | length -= 2 |
| 84 | version = version_limit(version, length) |
| 85 | |
| 86 | return None |