mbligh | e1ea015 | 2009-07-11 01:02:52 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 2 | # |
| 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 | |
| 11 | import sys, re, os |
| 12 | |
| 13 | kernel = 'http://www.kernel.org/pub/linux/kernel/' |
| 14 | mappings = [ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 15 | [ 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 | ]] |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 36 | ]; |
| 37 | |
| 38 | def decompose_kernel_once(kernel): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | ##print "S<" + kernel + ">" |
| 40 | for mapping in mappings: |
| 41 | (suffix, becomes, is_full, patch_templates) = mapping |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 42 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 43 | params = {} |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 44 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 45 | match = re.search(r'^(.*)' + suffix, kernel) |
| 46 | if not match: |
| 47 | continue |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 48 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 49 | # 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) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 57 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 58 | 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) |
mbligh | 8b35285 | 2008-06-07 01:07:08 +0000 | [diff] [blame] | 63 | params['minor-prev'] = match.group(2) + '.%d' % (int(match.group(3)) -1) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 64 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 65 | # 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) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 70 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 71 | return (is_full, new_kernel, patch_list) |
| 72 | |
| 73 | return (True, kernel, None) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def decompose_kernel(kernel): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 77 | kernel_patches = [] |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 78 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 79 | 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)) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 86 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 87 | return kernel_patches |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | # Look for and add potential mirrors. |
| 91 | def mirror_kernel_components(mirrors, components): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 92 | 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): |
mbligh | 8b35285 | 2008-06-07 01:07:08 +0000 | [diff] [blame] | 99 | new_patch = local + patch[len(prefix):] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 100 | 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 |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def url_accessible(url): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 109 | status = os.system("wget --spider -q '%s'" % (url)) |
| 110 | #print url + ": status=%d" % (status) |
| 111 | |
| 112 | return status == 0 |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def select_kernel_components(components): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 116 | 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 |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 127 | |
| 128 | |
| 129 | def expand_classic(kernel, mirrors): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 130 | components = decompose_kernel(kernel) |
| 131 | if mirrors: |
| 132 | components = mirror_kernel_components(mirrors, components) |
| 133 | components = select_kernel_components(components) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 134 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 135 | patches = [] |
| 136 | for component in components: |
| 137 | patches.append(component[0]) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 138 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 139 | return patches |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 143 | from optparse import OptionParser |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 144 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 145 | parser = OptionParser() |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 146 | |
mbligh | 8b35285 | 2008-06-07 01:07:08 +0000 | [diff] [blame] | 147 | parser.add_option("-m", "--mirror", type="string", dest="mirror", |
| 148 | action="append", nargs=2, help="mirror prefix") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 149 | parser.add_option("-v", "--no-validate", dest="validate", |
mbligh | 8b35285 | 2008-06-07 01:07:08 +0000 | [diff] [blame] | 150 | action="store_false", default=True, help="prune invalid entries") |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 151 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 152 | def usage(): |
| 153 | parser.print_help() |
| 154 | sys.exit(1) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 155 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 156 | options, args = parser.parse_args() |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 157 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 158 | # Check for a kernel version |
| 159 | if len(args) != 1: |
| 160 | usage() |
| 161 | kernel = args[0] |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 162 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 163 | #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 |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 172 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 173 | try: |
| 174 | components = decompose_kernel(kernel) |
| 175 | except NameError, e: |
| 176 | sys.stderr.write(e.args[0] + "\n") |
| 177 | sys.exit(1) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 178 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 179 | if mirrors: |
| 180 | components = mirror_kernel_components(mirrors, components) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 181 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 182 | if options.validate: |
| 183 | components = select_kernel_components(components) |
apw | 7bae90e | 2008-03-05 12:18:11 +0000 | [diff] [blame] | 184 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 185 | # Dump them out. |
| 186 | for component in components: |
| 187 | print " ".join(component) |