X-Git-Url: http://git.pld-linux.org/?a=blobdiff_plain;ds=sidebyside;f=rediff-patches.py;h=d09c9ee93be8a4aac21e01088491f6acf1e118ee;hb=792039e3278035e979dbd47925a56193ead451d1;hp=ca7a0a8fe2249a62b1d97d9457284b2e21ea7b59;hpb=ce75a2bdaa00439ef78e846dde0017ebe6c366f5;p=packages%2Frpm-build-tools.git diff --git a/rediff-patches.py b/rediff-patches.py index ca7a0a8..d09c9ee 100755 --- a/rediff-patches.py +++ b/rediff-patches.py @@ -33,21 +33,56 @@ def prepare_spec(r, patch_nr, before=False): tempspec.flush() return tempspec -def unpack(spec, builddir): +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', spec ] logging.debug("running %s" % repr(cmd)) - subprocess.check_call(cmd, stdout=sys.stderr, stderr=sys.stderr, - env={'LC_ALL': 'C.UTF-8'}, timeout=600) - -def diff(diffdir_org, diffdir, builddir, output): + 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): diffdir_org = os.path.basename(diffdir_org) diffdir = os.path.basename(diffdir) 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: @@ -58,10 +93,22 @@ def diff(diffdir_org, diffdir, builddir, output): 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() @@ -75,9 +122,16 @@ def main(): 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)) - tempdir = tempfile.TemporaryDirectory(dir="/dev/shm") + 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') @@ -101,25 +155,34 @@ def main(): patch_args = m.group('patch_args') applied_patches[patch_nr] = patch_args - appsourcedir = rpm.expandMacro("%{_sourcedir}") appbuilddir = rpm.expandMacro("%{_builddir}/%{?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 = patches[patch_nr] logging.info("*** patch %d: %s" % (patch_nr, patch_name)) tempspec = prepare_spec(r, patch_nr, before=True) - unpack(tempspec.name, builddir) + unpack(tempspec.name, appsourcedir, builddir) tempspec.close() os.rename(appbuilddir, appbuilddir + ".org") tempspec = prepare_spec(r, patch_nr, before=False) - unpack(tempspec.name, builddir) + unpack(tempspec.name, appsourcedir, builddir) tempspec.close() - diff(appbuilddir + ".org", appbuilddir, builddir, os.path.join(topdir, os.path.join(appsourcedir, patch_name + ".rediff"))) + patch_comment = patch_comment_get(patch_name) + diff(appbuilddir + ".org", + appbuilddir, + builddir, + 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()