1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
#!/usr/bin/env python2.4
import os, os.path, re, sys
from warnings import warn
_default_home = "/usr/src/kernel-patches/all/2.6.18/debian"
_default_revisions = "1 2 3 4 5 6 7 8 9 10 11 12 12etch1 12etch2 13 13etch1 13etch2 13etch3 13etch4 13etch5 13etch6 14 15 16 17 17etch1 18 18etch1 18etch2 18etch3 18etch4 18etch5 18etch6 19 20 21 22 22etch1 22etch2 22etch3 23 23etch1 24 24etch1 24etch2 24etch3 24etch4 25 26 26etch1 26etch2"
_default_source = "2.6.18-26etch2"
class series(list):
def __init__(self, name, home, reverse = False):
self.name = name
self.reverse = reverse
filename = os.path.join(home, 'series', name)
if not os.path.exists(filename):
raise RuntimeError, "Can't find series file for %s" % name
f = file(filename)
for line in f.readlines():
line = line.strip()
if len(line) == 0 or line[0] == '#':
continue
items = line.split(' ')
if len(items) != 2:
raise RuntimeError, "Line '%s' in file %s malformed." % (line, filename)
else:
operation, patch = items
if operation in ('+', '-'):
patchfile = os.path.join(home, patch)
for suffix, type in (('', 0), ('.bz2', 1), ('.gz', 2)):
if os.path.exists(patchfile + suffix):
patchinfo = patchfile + suffix, type
break
else:
raise RuntimeError, "Can't find patch %s for series %s" % (patchfile, name)
else:
raise RuntimeError, 'Undefined operation "%s" in series %s' % (operation, name)
self.append((operation, patch, patchinfo))
def __repr__(self):
return '<%s object for %s>' % (self.__class__.__name__, self.name)
def apply(self):
if self.reverse:
for operation, patch, patchinfo in self[::-1]:
if operation == '.':
print """\
(.) IGNORED %s\
""" % patch
elif operation == '+':
self.patch_deapply(patch, patchinfo)
elif operation == '-':
self.patch_apply(patch, patchinfo)
print "--> %s fully unapplied." % self.name
else:
for operation, patch, patchinfo in self:
if operation == '.':
print """\
(.) IGNORED %s\
""" % patch
elif operation == '+':
self.patch_apply(patch, patchinfo)
elif operation == '-':
self.patch_deapply(patch, patchinfo)
print "--> %s fully applied." % self.name
def patch_apply(self, patch, patchinfo):
ret = self.patch_call(patchinfo, '--fuzz=1')
if ret == 0:
print """\
(+) OK %s\
""" % patch
else:
print """\
(+) FAIL %s\
""" % patch
raise SystemExit, 1
def patch_call(self, patchinfo, patchargs):
patchfile, type = patchinfo
cmdline = []
if type == 0:
cmdline.append('cat')
elif type == 1:
cmdline.append('bzcat')
elif type == 2:
cmdline.append('zcat')
cmdline.append(patchfile + ' | patch -p1 -f -s -t --no-backup-if-mismatch')
cmdline.append(patchargs)
return os.spawnv(os.P_WAIT, '/bin/sh', ['sh', '-c', ' '.join(cmdline)])
def patch_deapply(self, patch, patchinfo):
ret = self.patch_call(patchinfo, '-R')
if ret == 0:
print """\
(-) OK %s\
""" % patch
else:
print """\
(-) FAIL %s\
""" % patch
raise SystemExit, 1
@staticmethod
def read_all(s, home, reverse = False):
ret = []
for name in s:
filename = os.path.join(home, 'series', name)
if not os.path.exists(filename):
warn("Can't find series file for %s" % name)
else:
i = series(name, home, reverse)
ret.append(i)
return ret
class series_extra(series):
def __init__(self, name, home, extra, reverse = False):
self.extra = extra
self.extra_used = ()
self.name = name
self.reverse = reverse
filename = os.path.join(home, 'series', name + '-extra')
if not os.path.exists(filename):
raise RuntimeError, "Can't find series file for %s" % name
f = file(filename)
for line in f.readlines():
line = line.strip()
if len(line) == 0 or line[0] == '#':
continue
items = line.split(' ')
operation, patch = items[:2]
if operation in ('+', '-'):
patchfile = os.path.join(home, patch)
for suffix, type in (('', 0), ('.bz2', 1), ('.gz', 2)):
if os.path.exists(patchfile + suffix):
patchinfo = patchfile + suffix, type
break
else:
raise RuntimeError, "Can't find patch %s for series %s" % (patchfile, name)
else:
raise RuntimeError, 'Undefined operation "%s" in series %s' % (operation, name)
extra = {}
for s in items[2:]:
s = tuple(s.split('_'))
if len(s) > 3:
raise RuntimeError, "parse error"
if len(s) == 3:
raise RuntimeError, "Patch per flavour is not supported currently"
extra[s] = True
if not self._check_extra(extra):
operation = '.'
self.append((operation, patch, patchinfo))
def _check_extra(self, extra):
for i in (1, 2, 3):
t = self.extra[:i]
if extra.has_key(t):
if i > len(self.extra_used):
self.extra_used = t
return True
for i in (2, 3):
t = ('*',) + self.extra[1:i]
if extra.has_key(t):
if i > len(self.extra_used):
self.extra_used = t
return True
return False
@staticmethod
def read_all(s, home, extra, reverse = False):
ret = []
for name in s:
filename = os.path.join(home, 'series', name + '-extra')
if not os.path.exists(filename):
warn("Can't find series file for %s" % name)
else:
i = series_extra(name, home, extra, reverse)
ret.append(i)
return ret
class series_orig(series):
def __init__(self, name, home):
self.name = name
filename = os.path.join(home, 'series', 'orig-' + name)
if not os.path.exists(filename):
raise RuntimeError, "Can't find series file for %s" % name
f = file(filename)
for line in f.readlines():
line = line.strip()
if len(line) == 0 or line[0] == '#':
continue
items = line.split(' ')
operation, patch = items[:2]
if operation == '+':
patchfile = os.path.join(home, patch)
for suffix, type in (('', 0), ('.bz2', 1), ('.gz', 2)):
if os.path.exists(patchfile + suffix):
patchinfo = patchfile + suffix, type
break
else:
raise RuntimeError, "Can't find patch %s for series %s" % (patchfile, name)
elif operation == 'X':
patchfile = os.path.join(home, patch)
if os.path.exists(patchfile):
patchinfo = patchfile
else:
raise RuntimeError, "Can't find patch %s for series %s" % (patchfile, name)
else:
raise RuntimeError, 'Undefined operation "%s" in series %s' % (operation, name)
self.append((operation, patch, patchinfo))
def apply(self):
for operation, patch, patchinfo in self:
if operation == '+':
self.patch_apply(patch, patchinfo)
elif operation == 'X':
self.files_remove(patch, patchinfo)
print "--> %s fully applied." % self.name
def files_remove(self, patch, patchinfo):
for line in file(patchinfo):
line = line.strip()
if len(line) == 0 or line[0] == '#':
continue
os.unlink(line)
print """\
(X) OK %s\
""" % patch
class version(object):
__slots__ = "upstream", "revision"
def __init__(self, string = None):
if string is not None:
self.upstream, self.revision = self.parse(string)
def __str__(self):
return "%s-%s" % (self.upstream, self.revision)
_re = r"""
^
(
(?:
\d+\.\d+\.\d+\+
)?
\d+\.\d+\.\d+
(?:
-.+?
)?
)
-
([^-]+)
$
"""
def parse(self, version):
match = re.match(self._re, version, re.X)
if match is None:
raise ValueError
return match.groups()
class version_file(object):
_file = 'version.Debian'
extra = ()
in_progress = False
def __init__(self, ver = None, overwrite = False):
if overwrite:
self._read(ver)
elif os.path.exists(self._file):
s = file(self._file).readline().strip()
self._read(s)
elif ver:
warn('No %s file, assuming pristine Linux %s' % (self._file, ver.upstream))
self.version = version()
self.version.upstream = ver.upstream
self.version.revision = '0'
else:
raise RuntimeError, "Not possible to determine version"
def __str__(self):
if self.in_progress:
return "unstable"
if self.extra:
return "%s %s" % (self.version, '_'.join(self.extra))
return str(self.version)
def _read(self, s):
list = s.split(' ')
if len(list) > 2:
raise RuntimeError, "Can't parse %s" % self._file
try:
self.version = version(list[0])
except ValueError:
raise RuntimeError, 'Can\'t read version in %s: "%s"' % (self._file, list[0])
if len(list) == 2:
self.extra = tuple(list[1].split('_'))
def _write(self):
if os.path.lexists(self._file):
os.unlink(self._file)
file(self._file, 'w').write('%s\n' % self)
def begin(self):
self.in_progress = True
self._write()
def commit(self, version = None, extra = None):
self.in_progress = False
if version is not None:
self.version = version
if extra is not None:
self.extra = extra
self._write()
def main():
options, args = parse_options()
if len(args) > 1:
print "Too much arguments"
return
if options.orig:
do_orig(options)
else:
do(options, args)
def do(options, args):
home = options.home
revisions = ['0'] + options.revisions.split()
source = version(options.source)
if len(args) == 1:
target = version(args[0])
else:
target = source
if options.current is not None:
vfile = version_file(options.current, True)
else:
vfile = version_file(source)
current = vfile.version
current_extra = vfile.extra
target_extra = []
if options.arch: target_extra.append(options.arch)
if options.subarch: target_extra.append(options.subarch)
if options.flavour: target_extra.append(options.flavour)
target_extra = tuple(target_extra)
if current.revision not in revisions:
raise RuntimeError, "Current revision is not in our list of revisions"
if target.revision not in revisions:
raise RuntimeError, "Target revision is not in our list of revisions"
if current.revision == target.revision and current_extra == target_extra:
print "Nothing to do"
return
current_index = revisions.index(current.revision)
source_index = revisions.index(source.revision)
target_index = revisions.index(target.revision)
if current_extra:
if current_index != source_index:
raise RuntimeError, "Can't patch from %s with options %s" % (current, ' '.join(current_extra))
consider = revisions[current_index:0:-1]
s = series_extra.read_all(consider, home, current_extra, reverse = True)
vfile.begin()
for i in s:
i.apply()
vfile.commit(current, ())
if current_index < target_index:
consider = revisions[current_index + 1:target_index + 1]
s = series.read_all(consider, home)
vfile.begin()
for i in s:
i.apply()
vfile.commit(target, ())
elif current_index > target_index:
consider = revisions[current_index:target_index:-1]
s = series.read_all(consider, home, reverse = True)
vfile.begin()
for i in s:
i.apply()
vfile.commit(target, ())
if target_extra:
consider = revisions[1:target_index + 1]
s = series_extra.read_all(consider, home, target_extra)
real_extra = ()
for i in s:
t = i.extra_used
if len(t) > len(real_extra):
real_extra = t
vfile.begin()
for i in s:
i.apply()
vfile.commit(target, real_extra)
def do_orig(options):
series_orig(options.orig, options.home).apply()
def parse_options():
from optparse import OptionParser
parser = OptionParser(
usage = "%prog [OPTION]... [TARGET]",
)
parser.add_option(
'-a', '--arch',
dest = 'arch',
help = "arch",
)
parser.add_option(
'-f', '--flavour',
dest = 'flavour',
help = "flavour",
)
parser.add_option(
'-s', '--subarch',
dest = 'subarch',
help = "subarch",
)
parser.add_option(
'-C', '--overwrite-current',
dest = 'current',
help = "overwrite current",
)
parser.add_option(
'-H', '--overwrite-home',
default = _default_home, dest = 'home',
help = "overwrite home [default: %default]",
)
parser.add_option(
'-R', '--overwrite-revisions',
default = _default_revisions, dest = 'revisions',
help = "overwrite revisions [default: %default]",
)
parser.add_option(
'-S', '--overwrite-source',
default = _default_source, dest = 'source',
help = "overwrite source [default: %default]",
)
parser.add_option(
'--orig',
dest = 'orig',
)
options, args = parser.parse_args()
if options.arch is None and options.subarch is not None:
raise RuntimeError('You specified a subarch without an arch, this is not really working')
if options.subarch is None and options.flavour is not None:
raise RuntimeError('You specified a flavour without a subarch, this is not really working')
return options, args
if __name__ == '__main__':
def showwarning(message, category, filename, lineno):
sys.stderr.write("Warning: %s\n" % message)
import warnings
warnings.showwarning = showwarning
try:
main()
except RuntimeError, e:
sys.stderr.write("Error: %s\n" % e)
raise SystemExit, 1
|