Added script to extract linux stable kernel versions from git
[lttng-ci.git] / automation / kernel-seed.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2015 - Michael Jeanson <mjeanson@efficios.com>
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 3 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,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU 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, see <http://www.gnu.org/licenses/>.
18
19 """ This script is used to """
20
21 import os
22 import re
23 from distutils.version import Version
24 from git import Repo
25 import yaml
26
27
28 class KernelVersion(Version):
29 """ Kernel version class """
30
31 re26 = re.compile(r'^(2\.\d+) \. (\d+) (\. (\d+))? (\-rc(\d+))?$',
32 re.VERBOSE)
33 re30 = re.compile(r'^(\d+) \. (\d+) (\. (\d+))? (\-rc(\d+))?$', re.VERBOSE)
34
35
36 def __init__(self, vstring=None):
37 self._rc = None
38 self._version = None
39 if vstring:
40 self.parse(vstring)
41
42
43 def parse(self, vstring):
44 """ Parse version string """
45
46 self._vstring = vstring
47
48 if self._vstring.startswith("2"):
49 match = self.re26.match(self._vstring)
50 else:
51 match = self.re30.match(self._vstring)
52
53 if not match:
54 raise ValueError("invalid version number '%s'" % self._vstring)
55
56 (major, minor, patch, rc_num) = match.group(1, 2, 4, 6)
57
58 major = int(float(major) * 10)
59
60 if patch:
61 self._version = tuple(map(int, [major, minor, patch]))
62 else:
63 self._version = tuple(map(int, [major, minor])) + (0,)
64
65 if rc_num:
66 self._rc = int(rc_num)
67 else:
68 self._rc = None
69
70
71 def isrc(self):
72 """ Is this version an RC """
73 return self._rc is not None
74
75
76 def __str__(self):
77 return self._vstring
78
79
80 def __repr__(self):
81 return "KernelVersion ('%s')" % str(self)
82
83
84 def _cmp(self, other):
85 if isinstance(other, str):
86 other = KernelVersion(other)
87
88 if self._version != other._version:
89 # numeric versions don't match
90 # prerelease stuff doesn't matter
91 if self._version < other._version:
92 return -1
93 else:
94 return 1
95
96 # have to compare rc
97 # case 1: neither has rc; they're equal
98 # case 2: self has rc, other doesn't; other is greater
99 # case 3: self doesn't have rc, other does: self is greater
100 # case 4: both have rc: must compare them!
101
102 if (not self._rc and not other._rc):
103 return 0
104 elif (self._rc and not other._rc):
105 return -1
106 elif (not self._rc and other._rc):
107 return 1
108 elif (self._rc and other._rc):
109 if self._rc == other._rc:
110 return 0
111 elif self._rc < other._rc:
112 return -1
113 else:
114 return 1
115 else:
116 assert False, "never get here"
117
118
119
120
121 KERNCUTOFF = KernelVersion("2.6.36")
122
123 LINUX_GIT = "git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git"
124 LINUX_PATH = "/home/mjeanson/tmp/toto/"
125
126
127
128
129 def main():
130 """ Main """
131
132 versions = []
133
134 # Open or create the local repository
135 if os.path.isdir(LINUX_PATH):
136 linux_repo = Repo(LINUX_PATH)
137 else:
138 linux_repo = Repo.clone_from(LINUX_GIT, LINUX_PATH)
139
140 # Pull the latest
141 linux_repo.remote().pull()
142
143 # First get all valid versions
144 for tag in linux_repo.tags:
145 try:
146 version = KernelVersion(tag.name.lstrip('v'))
147
148 # Add only those who are superior to the cutoff version
149 if version >= KERNCUTOFF:
150 versions.append(version)
151 except ValueError:
152 #print(tag.name)
153 continue
154
155 # Sort the list by version order
156 versions.sort()
157
158 # Keep only one rc if it's the latest version
159 last = True
160 for version in reversed(versions):
161 if version.isrc() and not last:
162 versions.remove(version)
163 last = False
164
165 #for version in versions:
166 # print(version)
167
168 # Build yaml object
169 yversions = []
170
171 for version in versions:
172 yversions.append(version.__str__())
173
174 print(yaml.dump(yversions, default_flow_style=False))
175
176
177
178
179 if __name__ == "__main__":
180 main()
181
182 # EOF
This page took 0.04406 seconds and 5 git commands to generate.