]>
Commit | Line | Data |
---|---|---|
7eacbddd AM |
1 | #!/usr/bin/python3 |
2 | ||
3 | # rediff-patches.py name.spec | |
4 | ||
1e57e977 AM |
5 | import argparse |
6 | import collections | |
7 | import logging | |
7eacbddd AM |
8 | import os |
9 | import re | |
10 | import rpm | |
11 | import shutil | |
12 | import subprocess | |
13 | import sys | |
14 | import tempfile | |
15 | ||
16 | RPMBUILD_ISPATCH = (1<<1) | |
17 | ||
ce75a2bd AM |
18 | def prepare_spec(r, patch_nr, before=False): |
19 | tempspec = tempfile.NamedTemporaryFile() | |
20 | re_patch = re.compile(r'^%patch(?P<patch_number>\d+)\w*') | |
21 | for line in r.parsed.split('\n'): | |
22 | m = re_patch.match(line) | |
23 | if m: | |
24 | patch_number = int(m.group('patch_number')) | |
25 | if patch_nr == patch_number: | |
26 | if before: | |
27 | tempspec.write(b"exit 0\n# here was patch%d\n" % patch_nr) | |
28 | else: | |
29 | line = re.sub(r'#.*', "", line) | |
30 | tempspec.write(b"%s\nexit 0\n" % line.encode('utf-8')) | |
31 | continue | |
32 | tempspec.write(b"%s\n" % line.encode('utf-8')) | |
33 | tempspec.flush() | |
34 | return tempspec | |
35 | ||
98607168 | 36 | def unpack(spec, appsourcedir, builddir): |
ce75a2bd AM |
37 | cmd = [ 'rpmbuild', '-bp', |
38 | '--define', '_builddir %s' % builddir, | |
98607168 AM |
39 | '--define', '_specdir %s' % appsourcedir, |
40 | '--define', '_sourcedir %s' % appsourcedir, | |
ce75a2bd AM |
41 | '--define', '_enable_debug_packages 0', |
42 | '--define', '_default_patch_fuzz 2', | |
43 | spec ] | |
1e57e977 | 44 | logging.debug("running %s" % repr(cmd)) |
98607168 AM |
45 | try: |
46 | res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True, | |
47 | env={'LC_ALL': 'C.UTF-8'}, timeout=600) | |
48 | except subprocess.CalledProcessError as err: | |
49 | logging.error("unpacking exited with status code %d." % err.returncode) | |
50 | logging.error("STDOUT:") | |
51 | if err.stdout: | |
52 | for line in err.stdout.decode('utf-8').split("\n"): | |
53 | logging.error(line) | |
54 | logging.error("STDERR:") | |
55 | if err.stderr: | |
56 | for line in err.stderr.decode('utf-8').split("\n"): | |
57 | logging.error(line) | |
58 | raise | |
59 | else: | |
60 | logging.debug("unpacking exited with status code %d." % res.returncode) | |
61 | logging.debug("STDOUT/STDERR:") | |
62 | if res.stdout: | |
63 | for line in res.stdout.decode('utf-8').split("\n"): | |
64 | logging.debug(line) | |
65 | ||
7eacbddd AM |
66 | |
67 | def diff(diffdir_org, diffdir, builddir, output): | |
68 | diffdir_org = os.path.basename(diffdir_org) | |
69 | diffdir = os.path.basename(diffdir) | |
70 | ||
71 | with open(output, 'wt') as f: | |
72 | cmd = [ 'diff', '-urNp', '-x', '*.orig', diffdir_org, diffdir ] | |
1e57e977 | 73 | logging.debug("running %s" % repr(cmd)) |
7eacbddd | 74 | try: |
1e57e977 AM |
75 | subprocess.check_call(cmd, cwd=builddir, stdout=f, stderr=sys.stderr, |
76 | env={'LC_ALL': 'C.UTF-8'}, timeout=600) | |
7eacbddd AM |
77 | except subprocess.CalledProcessError as err: |
78 | if err.returncode != 1: | |
79 | raise | |
1e57e977 AM |
80 | logging.info("rediff generated as %s" % output) |
81 | ||
0f726ca6 AM |
82 | def diffstat(patch): |
83 | cmd = [ 'diffstat', patch ] | |
84 | logging.info("running diffstat for: %s" % patch) | |
85 | try: | |
86 | subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr, | |
87 | env={'LC_ALL': 'C.UTF-8'}, timeout=60) | |
88 | except subprocess.CalledProcessError as err: | |
89 | logging.error("running diffstat failed: %s" % err) | |
90 | except FileNotFoundError as err: | |
91 | logging.error("running diffstat failed: %s, install diffstat package?" % err) | |
92 | ||
c65b587f | 93 | def main(): |
1e57e977 AM |
94 | parser = parser = argparse.ArgumentParser(description='rediff patches to avoid fuzzy hunks') |
95 | parser.add_argument('spec', type=str, help='spec file name') | |
96 | parser.add_argument('-p', '--patches', type=str, help='comma separated list of patch numbers to rediff') | |
1cae170b | 97 | parser.add_argument('-s', '--skip-patches', type=str, help='comma separated list of patch numbers to skip rediff') |
1e57e977 AM |
98 | parser.add_argument('-v', '--verbose', help='increase output verbosity', action='store_true') |
99 | args = parser.parse_args() | |
100 | ||
101 | logging.basicConfig(level=logging.INFO) | |
102 | rpm.setVerbosity(rpm.RPMLOG_ERR) | |
103 | ||
104 | if args.verbose: | |
105 | logging.basicConfig(level=logging.DEBUG) | |
106 | rpm.setVerbosity(rpm.RPMLOG_DEBUG) | |
107 | ||
108 | if args.patches: | |
109 | args.patches = [int(x) for x in args.patches.split(',')] | |
110 | ||
1cae170b AM |
111 | if args.skip_patches: |
112 | args.skip_patches = [int(x) for x in args.skip_patches.split(',')] | |
113 | ||
1e57e977 | 114 | specfile = args.spec |
98607168 | 115 | appsourcedir = os.path.dirname(os.path.abspath(specfile)) |
c65b587f AM |
116 | |
117 | tempdir = tempfile.TemporaryDirectory(dir="/dev/shm") | |
118 | topdir = tempdir.name | |
119 | builddir = os.path.join(topdir, 'BUILD') | |
120 | ||
121 | rpm.addMacro("_builddir", builddir) | |
122 | ||
123 | r = rpm.spec(specfile) | |
124 | ||
125 | patches = {} | |
126 | ||
127 | for (name, nr, flags) in r.sources: | |
128 | if flags & RPMBUILD_ISPATCH: | |
129 | patches[nr] = name | |
130 | ||
1e57e977 | 131 | applied_patches = collections.OrderedDict() |
c65b587f AM |
132 | re_patch = re.compile(r'^%patch(?P<patch_number>\d+)\w*(?P<patch_args>.*)') |
133 | for line in r.parsed.split('\n'): | |
134 | m = re_patch.match(line) | |
135 | if not m: | |
136 | continue | |
137 | patch_nr = int(m.group('patch_number')) | |
138 | patch_args = m.group('patch_args') | |
139 | applied_patches[patch_nr] = patch_args | |
140 | ||
c65b587f AM |
141 | appbuilddir = rpm.expandMacro("%{_builddir}/%{?buildsubdir}") |
142 | ||
ce75a2bd | 143 | for patch_nr in applied_patches.keys(): |
1e57e977 | 144 | if args.patches and patch_nr not in args.patches: |
c65b587f | 145 | continue |
1cae170b AM |
146 | if args.skip_patches and patch_nr in args.skip_patches: |
147 | continue | |
1e57e977 AM |
148 | patch_name = patches[patch_nr] |
149 | logging.info("*** patch %d: %s" % (patch_nr, patch_name)) | |
ce75a2bd AM |
150 | |
151 | tempspec = prepare_spec(r, patch_nr, before=True) | |
98607168 | 152 | unpack(tempspec.name, appsourcedir, builddir) |
ce75a2bd | 153 | tempspec.close() |
c65b587f | 154 | os.rename(appbuilddir, appbuilddir + ".org") |
ce75a2bd AM |
155 | |
156 | tempspec = prepare_spec(r, patch_nr, before=False) | |
98607168 | 157 | unpack(tempspec.name, appsourcedir, builddir) |
ce75a2bd AM |
158 | tempspec.close() |
159 | ||
c65b587f | 160 | diff(appbuilddir + ".org", appbuilddir, builddir, os.path.join(topdir, os.path.join(appsourcedir, patch_name + ".rediff"))) |
ce75a2bd | 161 | |
0f726ca6 AM |
162 | diffstat(os.path.join(topdir, os.path.join(appsourcedir, patch_name))) |
163 | diffstat(os.path.join(topdir, os.path.join(appsourcedir, patch_name + ".rediff"))) | |
164 | ||
c65b587f AM |
165 | shutil.rmtree(builddir) |
166 | tempdir.cleanup() | |
167 | ||
168 | if __name__ == '__main__': | |
169 | main() |