]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - rediff-patches.py
_default_patch_fuzz 2
[packages/rpm-build-tools.git] / rediff-patches.py
CommitLineData
7eacbddd
AM
1#!/usr/bin/python3
2
3# rediff-patches.py name.spec
4
5# TODO:
6# - handle rediff of last patch (need some way to terminate build just after that patch is applied)
7# - or maybe apply patches on our own instead of using rpmbuild for that
8# - argparse fro arguments
9# - cleanup
10
11import os
12import re
13import rpm
14import shutil
15import subprocess
16import sys
17import tempfile
18
19RPMBUILD_ISPATCH = (1<<1)
20
21def unpack(spec, builddir, until_patch=None):
2addb8ce 22 cmd = [ 'rpmbuild', '-bp', '--define', '_builddir %s' % builddir, '--define', '_default_patch_fuzz 2' ]
7eacbddd
AM
23 if until_patch is not None:
24 cmd += [ '--define', '%%patch%d exit; #' % until_patch ]
25 cmd += [ spec ]
26 subprocess.check_call(cmd, stdout=sys.stderr, stderr=sys.stderr, timeout=600)
27
28def diff(diffdir_org, diffdir, builddir, output):
29 diffdir_org = os.path.basename(diffdir_org)
30 diffdir = os.path.basename(diffdir)
31
32 with open(output, 'wt') as f:
33 cmd = [ 'diff', '-urNp', '-x', '*.orig', diffdir_org, diffdir ]
34 try:
35 subprocess.check_call(cmd, cwd=builddir, stdout=f, stderr=sys.stderr, timeout=600)
36 except subprocess.CalledProcessError as err:
37 if err.returncode != 1:
38 raise
39
40specfile = sys.argv[1]
41
42tempdir = tempfile.TemporaryDirectory(dir="/dev/shm")
43topdir = tempdir.name
44builddir = os.path.join(topdir, 'BUILD')
45
46rpm.addMacro("_builddir", builddir)
47
48r = rpm.spec(specfile)
49
50patches = {}
51
52for (name, nr, flags) in r.sources:
53 if flags & RPMBUILD_ISPATCH:
54 patches[nr] = name
55
56applied_patches = {}
57re_patch = re.compile(r'^%patch(?P<patch_number>\d+)\w*(?P<patch_args>.*)')
58for line in r.parsed.split('\n'):
59 m = re_patch.match(line)
60 if not m:
61 continue
62 patch_nr = int(m.group('patch_number'))
63 patch_args = m.group('patch_args')
64 applied_patches[patch_nr] = patch_args
65
66appsourcedir = rpm.expandMacro("%{_sourcedir}")
67appbuilddir = rpm.expandMacro("%{_builddir}/%{?buildsubdir}")
68
69for (patch_nr, patch_name) in sorted(patches.items()):
70 if patch_nr not in applied_patches:
71 continue
72 print("*** patch %d: %s" % (patch_nr, patch_name), file=sys.stderr)
73 unpack(specfile, builddir, patch_nr)
74 os.rename(appbuilddir, appbuilddir + ".org")
75 unpack(specfile, builddir, patch_nr + 1)
76 diff(appbuilddir + ".org", appbuilddir, builddir, os.path.join(topdir, os.path.join(appsourcedir, patch_name + ".rediff")))
77 shutil.rmtree(builddir)
78tempdir.cleanup()
This page took 0.131576 seconds and 4 git commands to generate.