diff mbox series

[swugenerator,1/2] generator: do not break escaped backslashes in sw-description

Message ID 20261102133740.974143-2-stefano.babic@swupdate.org
State Accepted
Headers show
Series [swugenerator,1/2] generator: do not break escaped backslashes in sw-description | expand

Commit Message

Stefano Babic Nov. 2, 2026, 1:37 p.m. UTC
After writing sw-description in libconfig, the escapes \n and \t are
replaced with a newline and a tab, so that an embedded-script keeps
its lines. The replacement also matches an escaped backslash followed
by n or t: Lua code as print("C:\\dir") or "a\\nb" becomes a backslash
followed by a newline, and SWUpdate gets a different script without
any error.

Replace only \n and \t that follow an even number of backslashes.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 swugenerator/generator.py | 12 ++++++------
 tests/test_generator.py   | 15 +++++++++++++--
 2 files changed, 19 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/swugenerator/generator.py b/swugenerator/generator.py
index 9850a74..8624f71 100644
--- a/swugenerator/generator.py
+++ b/swugenerator/generator.py
@@ -333,12 +333,12 @@  class SWUGenerator:
         else:
             swdesc = libconf.dumps(self.conf)
 
-            # libconf mishandle special character if they are part
-            # of an attribute. This happens to the embedded-script
-            # and the script results to be in just one line.
-            # Reinsert \n and \t that was removed by libconf
-            swdesc = re.sub(r"\\n", "\n", swdesc)
-            swdesc = re.sub(r"\\t", "\t", swdesc)
+            # libconf writes newlines and tabs in a string as \n and \t,
+            # and an embedded-script results in a single line. Write them
+            # as they are, as libconfig accepts them in a string, but not
+            # a "\\n" or "\\t" for an escaped backslash followed by n or t
+            swdesc = re.sub(r"(?<!\\)((?:\\\\)*)\\n", "\\1\n", swdesc)
+            swdesc = re.sub(r"(?<!\\)((?:\\\\)*)\\t", "\\1\t", swdesc)
 
         swdesc_filename = os.path.join(self.temp.name, sw.filename)
         self.save_swdescription(swdesc_filename, swdesc)
diff --git a/tests/test_generator.py b/tests/test_generator.py
index d3a2e90..df35e7c 100644
--- a/tests/test_generator.py
+++ b/tests/test_generator.py
@@ -40,10 +40,11 @@  def test_functions_missing_artifact(generator, function):
         generator._exec_functions()  # pylint: disable=protected-access
 
 
-def build(tmp_path, images, dirs=None):
+def build(tmp_path, images, dirs=None, extra=""):
     """Builds a SWU with the images and returns its files and sw-description"""
     (tmp_path / "sw-description").write_text(
-        'software = { version = "1.0"; images: ( ' + ", ".join(images) + " ); };\n"
+        'software = { version = "1.0"; ' + extra
+        + "images: ( " + ", ".join(images) + " ); };\n"
     )
     gen = SWUGenerator(
         tmp_path / "sw-description", tmp_path / "out.swu", {}, dirs or [tmp_path],
@@ -83,3 +84,13 @@  def test_plain_filename(tmp_path):
 
     assert files == ["sw-description", "image.bin.zlib"]
     assert swdesc.software.images[0].filename == "image.bin.zlib"
+
+
+def test_escaped_backslash_in_template(tmp_path):
+    # an escaped backslash before n or t is kept, \t after it is a tab
+    (tmp_path / "image.bin").write_bytes(b"0123456789")
+    _, swdesc = build(
+        tmp_path, ['{ filename = "image.bin"; }'],
+        extra=r'embedded-script = "print(\"a\\nb\\\\\tc\")";',
+    )
+    assert swdesc.software["embedded-script"] == r'print("a\nb\\' + "\t" + r'c")'