]> git.pld-linux.org Git - packages/rpm-build-tools.git/blobdiff - rediff-patches.py
Fallback to /tmp.
[packages/rpm-build-tools.git] / rediff-patches.py
index 875d0ea4ff414fef974b6476328c0487cc853ab9..d09c9ee93be8a4aac21e01088491f6acf1e118ee 100755 (executable)
@@ -33,22 +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))
-    res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True,
-                          env={'LC_ALL': 'C.UTF-8'}, timeout=600)
-    logging.debug("unpacking exited with %d status code. STDOUT/STDERR: %s" % (res.returncode, res.stdout))
-
-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:
@@ -74,6 +108,7 @@ 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()
 
@@ -87,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')
 
@@ -113,25 +155,31 @@ 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")))
This page took 0.031278 seconds and 4 git commands to generate.