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
|
from __future__ import print_function
import glob
import os
import shutil
import subprocess
class Operation(object):
def __init__(self, name, data):
self.name, self.data = name, data
def __call__(self, dir='.', reverse=False):
try:
if not reverse:
self.do(dir)
else:
self.do_reverse(dir)
self._log(True)
except:
self._log(False)
raise
def _log(self, result):
if result:
s = "OK"
else:
s = "FAIL"
print(""" (%s) %-4s %s""" % (self.operation, s, self.name))
def do(self, dir):
raise NotImplementedError
def do_reverse(self, dir):
raise NotImplementedError
class OperationPatch(Operation):
def __init__(self, name, filename, data):
super(OperationPatch, self).__init__(name, data)
self.filename = filename
def _call(self, dir, *extraargs):
with open(self.filename) as f:
subprocess.check_call(
("patch", "-p1", "-f", "-s", "-t", "--no-backup-if-mismatch") + extraargs,
cwd=dir,
stdin=f,
)
def patch_push(self, dir):
self._call(dir, '--fuzz=1')
def patch_pop(self, dir):
self._call(dir, '-R')
class OperationPatchPush(OperationPatch):
operation = '+'
do = OperationPatch.patch_push
do_reverse = OperationPatch.patch_pop
class OperationPatchPop(OperationPatch):
operation = '-'
do = OperationPatch.patch_pop
do_reverse = OperationPatch.patch_push
class SubOperation(Operation):
def _log(self, result):
if result:
s = "OK"
else:
s = "FAIL"
print(""" %-10s %-4s %s""" % ('(%s)' % self.operation, s, self.name))
class SubOperationFilesRemove(SubOperation):
operation = "remove"
def do(self, dir):
name = os.path.join(dir, self.name)
for n in glob.iglob(name):
if os.path.isdir(n):
shutil.rmtree(n)
else:
os.unlink(n)
class SubOperationFilesUnifdef(SubOperation):
operation = "unifdef"
def do(self, dir):
filename = os.path.join(dir, self.name)
ret = subprocess.call(("unifdef", "-o", filename, filename) + tuple(self.data))
if ret == 0:
raise RuntimeError("unifdef of %s removed nothing" % self.name)
elif ret != 1:
raise RuntimeError("unifdef failed")
class OperationFiles(Operation):
operation = 'X'
suboperations = {
'remove': SubOperationFilesRemove,
'rm': SubOperationFilesRemove,
'unifdef': SubOperationFilesUnifdef,
}
def __init__(self, name, filename, data):
super(OperationFiles, self).__init__(name, data)
ops = []
with open(filename) as f:
for line in f:
line = line.strip()
if not line or line[0] == '#':
continue
items = line.split()
operation, filename = items[:2]
data = items[2:]
if operation not in self.suboperations:
raise RuntimeError('Undefined operation "%s" in series %s' % (operation, name))
ops.append(self.suboperations[operation](filename, data))
self.ops = ops
def do(self, dir):
for i in self.ops:
i(dir=dir)
class PatchSeries(list):
operations = {
'+': OperationPatchPush,
'-': OperationPatchPop,
'X': OperationFiles,
}
def __init__(self, name, root, fp):
self.name, self.root = name, root
for line in fp:
line = line.strip()
if not len(line) or line[0] == '#':
continue
items = line.split(' ')
operation, filename = items[:2]
data = items[2:]
if operation in self.operations:
f = os.path.join(self.root, filename)
if os.path.exists(f):
self.append(self.operations[operation](filename, f, data))
else:
raise RuntimeError("Can't find patch %s for series %s" % (filename, self.name))
else:
raise RuntimeError('Undefined operation "%s" in series %s' % (operation, name))
def __call__(self, cond=bool, dir='.', reverse=False):
if not reverse:
l = self
else:
l = self[::-1]
for i in l:
if cond(i):
i(dir=dir, reverse=reverse)
def __repr__(self):
return '<%s object for %s>' % (self.__class__.__name__, self.name)
|