-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrediff-patches.py
More file actions
executable file
·216 lines (178 loc) · 7.11 KB
/
rediff-patches.py
File metadata and controls
executable file
·216 lines (178 loc) · 7.11 KB
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
#!/usr/bin/python3
# rediff-patches.py name.spec
import argparse
import collections
import logging
import os
import re
import rpm
import shutil
import subprocess
import sys
import tempfile
RPMBUILD_ISPATCH = (1<<1)
re_new_echo = re.compile(r'^echo\s+"Patch\s+#(?P<patch_number>\d+)')
re_patch_cmd = re.compile(r'^\s*/?\S*patch\s+(?P<patch_args>.+) <')
def prepare_spec(r, patch_nr, before=False):
tempspec = tempfile.NamedTemporaryFile()
lines = r.parsed.split('\n')
i=0
i_break=None
while i < len(lines):
line = lines[i]
line = line.replace('--fuzz=0', '')
m = re_new_echo.match(line)
if m:
patch_number = int(m.group('patch_number'))
if patch_nr == patch_number:
i_break = i + 2
if before:
tempspec.write(b"exit 0\n# here was patch%d\n" % patch_nr)
if i_break and i == i_break:
tempspec.write(b"exit 0\n")
break
tempspec.write(b"%s\n" % line.encode('utf-8'))
i += 1
tempspec.flush()
return tempspec
def unpack(spec, appsourcedir, builddir):
cmd = [ 'rpmbuild', '-bp',
'--define', '_builddir %s' % builddir,
'--define', '_specdir %s' % appsourcedir,
'--define', '_sourcedir %s' % appsourcedir,
'--define', '_enable_debug_packages 0',
'--define', '_default_patch_fuzz 2',
'--nodeps',
spec ]
logging.debug("running %s" % repr(cmd))
try:
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True,
env={'LC_ALL': 'C.UTF-8'}, timeout=600)
except subprocess.CalledProcessError as err:
logging.error("unpacking exited with status code %d." % err.returncode)
logging.error("STDOUT:")
if err.stdout:
for line in err.stdout.decode('utf-8').split("\n"):
logging.error(line)
logging.error("STDERR:")
if err.stderr:
for line in err.stderr.decode('utf-8').split("\n"):
logging.error(line)
raise
else:
logging.debug("unpacking exited with status code %d." % res.returncode)
logging.debug("STDOUT/STDERR:")
if res.stdout:
for line in res.stdout.decode('utf-8').split("\n"):
logging.debug(line)
def patch_comment_get(patch):
patch_comment = ""
patch_got = False
with open(patch, 'rt') as f:
for line in f:
if line.startswith('diff ') or line.startswith('--- '):
patch_got = True
break
patch_comment += line
return patch_comment if patch_got else ""
def diff(diffdir_org, diffdir, builddir, patch_comment, output):
with open(output, 'wt') as f:
if patch_comment:
f.write(patch_comment)
f.flush()
cmd = [ 'diff', '-urNp', '-x', '*.orig', diffdir_org, diffdir ]
logging.debug("running %s" % repr(cmd))
try:
subprocess.check_call(cmd, cwd=builddir, stdout=f, stderr=sys.stderr,
env={'LC_ALL': 'C.UTF-8'}, timeout=600)
except subprocess.CalledProcessError as err:
if err.returncode != 1:
raise
logging.info("rediff generated as %s" % output)
def diffstat(patch):
cmd = [ 'diffstat', patch ]
logging.info("running diffstat for: %s" % patch)
try:
subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr,
env={'LC_ALL': 'C.UTF-8'}, timeout=60)
except subprocess.CalledProcessError as err:
logging.error("running diffstat failed: %s" % err)
except FileNotFoundError as err:
logging.error("running diffstat failed: %s, install diffstat package?" % err)
def main():
parser = parser = argparse.ArgumentParser(description='rediff patches to avoid fuzzy hunks')
parser.add_argument('spec', type=str, help='spec file name')
parser.add_argument('-p', '--patches', type=str, help='comma separated list of patch numbers to rediff')
parser.add_argument('-s', '--skip-patches', type=str, help='comma separated list of patch numbers to skip rediff')
parser.add_argument('-v', '--verbose', help='increase output verbosity', action='store_true')
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
rpm.setVerbosity(rpm.RPMLOG_ERR)
if args.verbose:
logging.basicConfig(level=logging.DEBUG, force=True)
rpm.setVerbosity(rpm.RPMLOG_DEBUG)
if args.patches:
args.patches = [int(x) for x in args.patches.split(',')]
if args.skip_patches:
args.skip_patches = [int(x) for x in args.skip_patches.split(',')]
specfile = args.spec
appsourcedir = os.path.dirname(os.path.abspath(specfile))
try:
tempdir = tempfile.TemporaryDirectory(dir="/dev/shm")
except FileNotFoundError as e:
tempdir = tempfile.TemporaryDirectory(dir="/tmp")
topdir = tempdir.name
builddir = os.path.join(topdir, 'BUILD')
rpm.addMacro("_builddir", builddir)
r = rpm.spec(specfile)
patches = {}
for (name, nr, flags) in r.sources:
if flags & RPMBUILD_ISPATCH:
patches[nr] = name
applied_patches = collections.OrderedDict()
lines = r.parsed.split('\n')
i=0
while i < len(lines):
line = lines[i]
m = re_new_echo.match(line)
if not m:
i += 1
continue
patch_nr = int(m.group('patch_number'))
patch_args = ''
if i + 1 < len(lines):
m2 = re_patch_cmd.match(lines[i + 1])
if m2:
patch_args = m2.group('patch_args').strip()
i += 1
applied_patches[patch_nr] = patch_args
i += 1
appbuilddir = rpm.expandMacro("%{_builddir}")
appbuildsubdir = rpm.expandMacro("%{?buildsubdir}")
for patch_nr in applied_patches.keys():
if args.patches and patch_nr not in args.patches:
continue
if args.skip_patches and patch_nr in args.skip_patches:
continue
patch_name = os.path.basename(patches[patch_nr])
logging.info("*** patch %d: %s" % (patch_nr, patch_name))
tempspec = prepare_spec(r, patch_nr, before=True)
unpack(tempspec.name, appsourcedir, builddir)
tempspec.close()
os.rename(appbuilddir, appbuilddir + ".org")
tempspec = prepare_spec(r, patch_nr, before=False)
unpack(tempspec.name, appsourcedir, builddir)
tempspec.close()
os.rename(os.path.join(appbuilddir + ".org", appbuildsubdir), os.path.join(appbuilddir, appbuildsubdir + ".org"))
patch_comment = patch_comment_get(patch_name)
diff(appbuildsubdir + ".org",
appbuildsubdir,
appbuilddir,
patch_comment,
os.path.join(topdir, os.path.join(appsourcedir, patch_name + ".rediff")))
diffstat(os.path.join(topdir, os.path.join(appsourcedir, patch_name)))
diffstat(os.path.join(topdir, os.path.join(appsourcedir, patch_name + ".rediff")))
shutil.rmtree(builddir)
tempdir.cleanup()
if __name__ == '__main__':
main()