public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@siemens.com>
To: isar-users <isar-users@googlegroups.com>
Subject: [PATCH 2/4] Make patch task repeatedly applicable
Date: Tue,  2 Apr 2019 08:20:49 +0200	[thread overview]
Message-ID: <7f8a9c9bf7040cb3fd3b72d4aaf5920b8c68927c.1554186051.git.jan.kiszka@siemens.com> (raw)
In-Reply-To: <cover.1554186051.git.jan.kiszka@siemens.com>
In-Reply-To: <cover.1554186051.git.jan.kiszka@siemens.com>

From: Jan Kiszka <jan.kiszka@siemens.com>

This allows to rerun the patch task even if it was applied already. This
is achieved by recording the executed patch commands and storing them,
along with the original patches, in ${S}/.applied_patches and applying
them in reverse order before applying new patches. If unpack ran prior
to that, .applied_patches was deleted, and nothing is incorrectly
reverted.

Alternatives to this self-written solution would have been using quilt,
which would have added almost 30 MB additional dependencies to our build
environment, or even using the OE implementation for the patch task,
which would have required importing and adjusting a significant amount
of code with only little gain.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 meta/classes/patch.bbclass | 54 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 48 insertions(+), 6 deletions(-)

diff --git a/meta/classes/patch.bbclass b/meta/classes/patch.bbclass
index 4c08193..bb203b7 100644
--- a/meta/classes/patch.bbclass
+++ b/meta/classes/patch.bbclass
@@ -1,13 +1,38 @@
 # This software is a part of ISAR.
-# Copyright (c) Siemens AG, 2018
+# Copyright (c) Siemens AG, 2018-2019
+
+def clean_applied_patches(applied_patches_dir):
+    import shutil
+    import subprocess
+
+    if not os.path.exists(applied_patches_dir):
+        return
+
+    cmds_file = applied_patches_dir + ".patch-commands"
+    if os.path.exists(cmds_file):
+        with open(cmds_file, "r") as cmds:
+            patch_commands = cmds.readlines()
+        patch_commands.reverse()
+        for patch in patch_commands:
+            cmd = patch.split()
+            cmd.append("-R")
+            cmdline = " ".join(cmd)
+            bb.note("Reverting: " + cmdline)
+            if subprocess.call(cmd) != 0:
+                bb.fatal("patch reverting failed")
+
+    shutil.rmtree(applied_patches_dir)
 
 python do_patch() {
+    import shutil
     import subprocess
 
-    workdir = d.getVar("WORKDIR", True) + "/"
-    src_dir = d.getVar("S", True)
+    workdir = d.getVar("WORKDIR") + "/"
+    src_dir = d.getVar("S")
+
+    applied_patches_dirs = []
 
-    for src_uri in (d.getVar("SRC_URI", True) or "").split():
+    for src_uri in (d.getVar("SRC_URI") or "").split():
         try:
             fetcher = bb.fetch2.Fetch([src_uri], d)
 
@@ -20,6 +45,18 @@ python do_patch() {
                 continue
 
             patchdir = fetcher.ud[src_uri].parm.get("patchdir") or src_dir
+            applied_patches_dir = patchdir + "/.applied_patches/"
+
+            if applied_patches_dir not in applied_patches_dirs:
+                clean_applied_patches(applied_patches_dir)
+                bb.utils.mkdirhier(applied_patches_dir)
+                applied_patches_dirs.append(applied_patches_dir)
+
+            cmds = open(applied_patches_dir + ".patch-commands", "a")
+
+            patch_file = applied_patches_dir + basename
+            shutil.copyfile(workdir + basename, patch_file)
+
             striplevel = fetcher.ud[src_uri].parm.get("striplevel") or "1"
 
             cmd = [
@@ -27,11 +64,16 @@ python do_patch() {
                 "--no-backup-if-mismatch",
                 "-p", striplevel,
                 "--directory", patchdir,
-                "--input", workdir + basename,
+                "--input", patch_file,
             ]
-            bb.note(" ".join(cmd))
+            cmdline = " ".join(cmd)
+
+            bb.note("Applying: " + cmdline)
             if subprocess.call(cmd) != 0:
                 bb.fatal("patching failed")
+
+            cmds.write(cmdline + "\n")
+            cmds.close()
         except bb.fetch2.BBFetchException as e:
             raise bb.build.FuncFailed(e)
 }
-- 
2.16.4


  parent reply	other threads:[~2019-04-02  6:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-02  6:20 [PATCH 0/4] Patch improvements, more templating fallouts, linux-libc-dev fix Jan Kiszka
2019-04-02  6:20 ` [PATCH 1/4] patch: Add support for patchdir parameter Jan Kiszka
2019-04-02  6:20 ` Jan Kiszka [this message]
2019-04-02  6:20 ` [PATCH 3/4] template: Remove KERNEL_NAME from default TEMPLATE_VARS Jan Kiszka
2019-04-02  6:20 ` [PATCH 4/4] linux-custom: Fix linux-libc-dev version check Jan Kiszka
2019-04-03 18:36 ` [PATCH 0/4] Patch improvements, more templating fallouts, linux-libc-dev fix Maxim Yu. Osipov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7f8a9c9bf7040cb3fd3b72d4aaf5920b8c68927c.1554186051.git.jan.kiszka@siemens.com \
    --to=jan.kiszka@siemens.com \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox