blob: 871b0536caa0e2402a1fa9639542267574c6291c [file] [log] [blame]
mblighe1ea0152009-07-11 01:02:52 +00001#!/usr/bin/python
apw7bae90e2008-03-05 12:18:11 +00002#
3# (C) International Business Machines 2008
4# Author: Andy Whitcroft
5#
6# Inspired by kernelexpand by:
7# (C) Martin J. Bligh 2003
8#
9# Released under the GPL, version 2
10
11import sys, re, os
12
13kernel = 'http://www.kernel.org/pub/linux/kernel/'
14mappings = [
jadmanski0afbb632008-06-06 21:10:57 +000015 [ r'^\d+\.\d+\.\d+$', '', True, [
16 kernel + 'v%(major)s/linux-%(full)s.tar.bz2'
17 ]],
18 [ r'^\d+\.\d+\.\d+\.\d+$', '', True, [
19 kernel + 'v%(major)s/linux-%(full)s.tar.bz2'
20 ]],
21 [ r'-rc\d+$', '%(minor-prev)s', True, [
22 kernel + 'v%(major)s/testing/v%(minor)s/linux-%(full)s.tar.bz2',
23 kernel + 'v%(major)s/testing/linux-%(full)s.tar.bz2',
24 ]],
25 [ r'-(git|bk)\d+$', '%(base)s', False, [
26 kernel + 'v%(major)s/snapshots/old/patch-%(full)s.bz2',
27 kernel + 'v%(major)s/snapshots/patch-%(full)s.bz2',
28 ]],
29 [ r'-mm\d+$', '%(base)s', False, [
30 kernel + 'people/akpm/patches/' +
31 '%(major)s/%(base)s/%(full)s/%(full)s.bz2'
32 ]],
33 [ r'-mjb\d+$', '%(base)s', False, [
34 kernel + 'people/mbligh/%(base)s/patch-%(full)s.bz2'
35 ]]
apw7bae90e2008-03-05 12:18:11 +000036];
37
38def decompose_kernel_once(kernel):
jadmanski0afbb632008-06-06 21:10:57 +000039 ##print "S<" + kernel + ">"
40 for mapping in mappings:
41 (suffix, becomes, is_full, patch_templates) = mapping
apw7bae90e2008-03-05 12:18:11 +000042
jadmanski0afbb632008-06-06 21:10:57 +000043 params = {}
apw7bae90e2008-03-05 12:18:11 +000044
jadmanski0afbb632008-06-06 21:10:57 +000045 match = re.search(r'^(.*)' + suffix, kernel)
46 if not match:
47 continue
apw7bae90e2008-03-05 12:18:11 +000048
jadmanski0afbb632008-06-06 21:10:57 +000049 # Generate the parameters for the patches:
50 # full => full kernel name
51 # base => all but the matches suffix
52 # minor => 2.n.m
53 # major => 2.n
54 # minor-prev => 2.n.m-1
55 params['full'] = kernel
56 params['base'] = match.group(1)
apw7bae90e2008-03-05 12:18:11 +000057
jadmanski0afbb632008-06-06 21:10:57 +000058 match = re.search(r'^((\d+\.\d+)\.(\d+))', kernel)
59 if not match:
60 raise "unable to determine major/minor version"
61 params['minor'] = match.group(1)
62 params['major'] = match.group(2)
mbligh8b352852008-06-07 01:07:08 +000063 params['minor-prev'] = match.group(2) + '.%d' % (int(match.group(3)) -1)
apw7bae90e2008-03-05 12:18:11 +000064
jadmanski0afbb632008-06-06 21:10:57 +000065 # Build the new kernel and patch list.
66 new_kernel = becomes % params
67 patch_list = []
68 for template in patch_templates:
69 patch_list.append(template % params)
apw7bae90e2008-03-05 12:18:11 +000070
jadmanski0afbb632008-06-06 21:10:57 +000071 return (is_full, new_kernel, patch_list)
72
73 return (True, kernel, None)
apw7bae90e2008-03-05 12:18:11 +000074
75
76def decompose_kernel(kernel):
jadmanski0afbb632008-06-06 21:10:57 +000077 kernel_patches = []
apw7bae90e2008-03-05 12:18:11 +000078
jadmanski0afbb632008-06-06 21:10:57 +000079 done = False
80 while not done:
81 (done, kernel, patch_list) = decompose_kernel_once(kernel)
82 if patch_list:
83 kernel_patches.insert(0, patch_list)
84 if not len(kernel_patches):
85 raise NameError('kernelexpand: %s: unknown kernel' % (kernel))
apw7bae90e2008-03-05 12:18:11 +000086
jadmanski0afbb632008-06-06 21:10:57 +000087 return kernel_patches
apw7bae90e2008-03-05 12:18:11 +000088
89
90# Look for and add potential mirrors.
91def mirror_kernel_components(mirrors, components):
jadmanski0afbb632008-06-06 21:10:57 +000092 new_components = []
93 for component in components:
94 new_patches = []
95 for mirror in mirrors:
96 (prefix, local) = mirror
97 for patch in component:
98 if patch.startswith(prefix):
mbligh8b352852008-06-07 01:07:08 +000099 new_patch = local + patch[len(prefix):]
jadmanski0afbb632008-06-06 21:10:57 +0000100 new_patches.append(new_patch)
101 for patch in component:
102 new_patches.append(patch)
103 new_components.append(new_patches)
104
105 return new_components
apw7bae90e2008-03-05 12:18:11 +0000106
107
108def url_accessible(url):
jadmanski0afbb632008-06-06 21:10:57 +0000109 status = os.system("wget --spider -q '%s'" % (url))
110 #print url + ": status=%d" % (status)
111
112 return status == 0
apw7bae90e2008-03-05 12:18:11 +0000113
114
115def select_kernel_components(components):
jadmanski0afbb632008-06-06 21:10:57 +0000116 new_components = []
117 for component in components:
118 new_patches = []
119 for patch in component:
120 if url_accessible(patch):
121 new_patches.append(patch)
122 break
123 if not len(new_patches):
124 new_patches.append(component[-1])
125 new_components.append(new_patches)
126 return new_components
apw7bae90e2008-03-05 12:18:11 +0000127
128
129def expand_classic(kernel, mirrors):
jadmanski0afbb632008-06-06 21:10:57 +0000130 components = decompose_kernel(kernel)
131 if mirrors:
132 components = mirror_kernel_components(mirrors, components)
133 components = select_kernel_components(components)
apw7bae90e2008-03-05 12:18:11 +0000134
jadmanski0afbb632008-06-06 21:10:57 +0000135 patches = []
136 for component in components:
137 patches.append(component[0])
apw7bae90e2008-03-05 12:18:11 +0000138
jadmanski0afbb632008-06-06 21:10:57 +0000139 return patches
apw7bae90e2008-03-05 12:18:11 +0000140
141
142if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000143 from optparse import OptionParser
apw7bae90e2008-03-05 12:18:11 +0000144
jadmanski0afbb632008-06-06 21:10:57 +0000145 parser = OptionParser()
apw7bae90e2008-03-05 12:18:11 +0000146
mbligh8b352852008-06-07 01:07:08 +0000147 parser.add_option("-m", "--mirror", type="string", dest="mirror",
148 action="append", nargs=2, help="mirror prefix")
jadmanski0afbb632008-06-06 21:10:57 +0000149 parser.add_option("-v", "--no-validate", dest="validate",
mbligh8b352852008-06-07 01:07:08 +0000150 action="store_false", default=True, help="prune invalid entries")
apw7bae90e2008-03-05 12:18:11 +0000151
jadmanski0afbb632008-06-06 21:10:57 +0000152 def usage():
153 parser.print_help()
154 sys.exit(1)
apw7bae90e2008-03-05 12:18:11 +0000155
jadmanski0afbb632008-06-06 21:10:57 +0000156 options, args = parser.parse_args()
apw7bae90e2008-03-05 12:18:11 +0000157
jadmanski0afbb632008-06-06 21:10:57 +0000158 # Check for a kernel version
159 if len(args) != 1:
160 usage()
161 kernel = args[0]
apw7bae90e2008-03-05 12:18:11 +0000162
jadmanski0afbb632008-06-06 21:10:57 +0000163 #mirrors = [
164 # [ 'http://www.kernel.org/pub/linux/kernel/v2.4',
165 # 'http://kernel.beaverton.ibm.com/mirror/v2.4' ],
166 # [ 'http://www.kernel.org/pub/linux/kernel/v2.6',
167 # 'http://kernel.beaverton.ibm.com/mirror/v2.6' ],
168 # [ 'http://www.kernel.org/pub/linux/kernel/people/akpm/patches',
169 # 'http://kernel.beaverton.ibm.com/mirror/akpm' ],
170 #]
171 mirrors = options.mirror
apw7bae90e2008-03-05 12:18:11 +0000172
jadmanski0afbb632008-06-06 21:10:57 +0000173 try:
174 components = decompose_kernel(kernel)
175 except NameError, e:
176 sys.stderr.write(e.args[0] + "\n")
177 sys.exit(1)
apw7bae90e2008-03-05 12:18:11 +0000178
jadmanski0afbb632008-06-06 21:10:57 +0000179 if mirrors:
180 components = mirror_kernel_components(mirrors, components)
apw7bae90e2008-03-05 12:18:11 +0000181
jadmanski0afbb632008-06-06 21:10:57 +0000182 if options.validate:
183 components = select_kernel_components(components)
apw7bae90e2008-03-05 12:18:11 +0000184
jadmanski0afbb632008-06-06 21:10:57 +0000185 # Dump them out.
186 for component in components:
187 print " ".join(component)