From c39e53722537139ad6772922ae8f9d4de6d3da20 Mon Sep 17 00:00:00 2001 From: Eugene Zamriy Date: Wed, 26 Jul 2023 23:10:24 +0300 Subject: [PATCH] Fixed UTF-8 handling in replace and added iconv template command --- ...01-Backport-dracut-chroot-umount-fix.patch | 6 +-- ...02-Fix-replace-command-utf8-handling.patch | 51 +++++++++++++++++++ SOURCES/0003-Add-iconv-template-command.patch | 41 +++++++++++++++ SPECS/lorax.spec | 8 ++- 4 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 SOURCES/0002-Fix-replace-command-utf8-handling.patch create mode 100644 SOURCES/0003-Add-iconv-template-command.patch diff --git a/SOURCES/0001-Backport-dracut-chroot-umount-fix.patch b/SOURCES/0001-Backport-dracut-chroot-umount-fix.patch index d08a6cb..fb2c5b5 100644 --- a/SOURCES/0001-Backport-dracut-chroot-umount-fix.patch +++ b/SOURCES/0001-Backport-dracut-chroot-umount-fix.patch @@ -1,7 +1,7 @@ -From 4969d3ffd744c9f63be41fbccf54bae121073d6e Mon Sep 17 00:00:00 2001 +From f04d4c94d77f644d4a9cfe25d708789c4334c25a Mon Sep 17 00:00:00 2001 From: Eugene Zamriy Date: Thu, 6 Apr 2023 21:16:26 +0300 -Subject: [PATCH] Backport dracut chroot umount fix +Subject: [PATCH 1/3] Backport dracut chroot umount fix --- src/pylorax/imgutils.py | 8 +++++--- @@ -30,5 +30,5 @@ index d713e4c..80e8c2f 100644 def Run(self, args): runcmd(["dracut"] + args, root=self.root) -- -2.39.2 +2.40.1 diff --git a/SOURCES/0002-Fix-replace-command-utf8-handling.patch b/SOURCES/0002-Fix-replace-command-utf8-handling.patch new file mode 100644 index 0000000..797cce1 --- /dev/null +++ b/SOURCES/0002-Fix-replace-command-utf8-handling.patch @@ -0,0 +1,51 @@ +From de825e84964d0145c46d77433cb08ee8c4640843 Mon Sep 17 00:00:00 2001 +From: Eugene Zamriy +Date: Wed, 26 Jul 2023 22:45:32 +0300 +Subject: [PATCH 2/3] Fix replace command utf8 handling + +This change allows using non-latin UTF-8 symbols in files modified +from Lorax templates using the "replace" command. + +Note: fileinput.input doesn't support encoding argument in the +in-place mode, so we replaced it with StringIO. +--- + src/pylorax/sysutils.py | 15 +++++++-------- + 1 file changed, 7 insertions(+), 8 deletions(-) + +diff --git a/src/pylorax/sysutils.py b/src/pylorax/sysutils.py +index 35f8b0f..81dc20a 100644 +--- a/src/pylorax/sysutils.py ++++ b/src/pylorax/sysutils.py +@@ -25,7 +25,7 @@ __all__ = ["joinpaths", "touch", "replace", "chown_", "chmod_", "remove", + import sys + import os + import re +-import fileinput ++import io + import pwd + import grp + import glob +@@ -50,14 +50,13 @@ def touch(fname): + + + def replace(fname, find, sub): +- fin = fileinput.input(fname, inplace=1) + pattern = re.compile(find) +- +- for line in fin: +- line = pattern.sub(sub, line) +- sys.stdout.write(line) +- +- fin.close() ++ with open(fname, "r", encoding="utf-8") as fd: ++ fin = io.StringIO(fd.read()) ++ with open(fname, "w", encoding="utf-8") as fd: ++ for line in fin: ++ line = pattern.sub(sub, line) ++ fd.write(line) + + + def chown_(path, user=None, group=None, recursive=False): +-- +2.40.1 + diff --git a/SOURCES/0003-Add-iconv-template-command.patch b/SOURCES/0003-Add-iconv-template-command.patch new file mode 100644 index 0000000..ca2fe21 --- /dev/null +++ b/SOURCES/0003-Add-iconv-template-command.patch @@ -0,0 +1,41 @@ +From 9b34c5ad852edd1eb68d78363e3be12ac49b44ce Mon Sep 17 00:00:00 2001 +From: Eugene Zamriy +Date: Wed, 26 Jul 2023 22:55:04 +0300 +Subject: [PATCH 3/3] Add iconv template command + +Adds the "iconv" template command that converts file(s) from UTF-8 +to a specified encoding. This change was required to add Russian +localization to the BIOS mode bootloader. +--- + src/pylorax/ltmpl.py | 15 +++++++++++++++ + 1 file changed, 15 insertions(+) + +diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py +index 36e09e9..7c5b56f 100644 +--- a/src/pylorax/ltmpl.py ++++ b/src/pylorax/ltmpl.py +@@ -273,6 +273,21 @@ class LoraxTemplateRunner(TemplateRunner): + f.write("\n".join(sorted(debug_pkgs))) + f.write("\n") + ++ def iconv(self, encoding, *fileglobs): ++ ''' ++ iconv ENCODING [FILEGLOB ...] ++ Convert given files from UTF-8 to a specified encoding. ++ ++ Examples: ++ iconv 'cp866' ${BOOTDIR}/isolinux.cfg ++ ''' ++ for g in fileglobs: ++ for f in rglob(self._out(g)): ++ with open(f, "r", encoding="utf-8") as fd: ++ content = fd.read() ++ with open(f, "wb") as fd: ++ fd.write(content.encode(encoding)) ++ + def install(self, srcglob, dest): + ''' + install SRC DEST +-- +2.40.1 + diff --git a/SPECS/lorax.spec b/SPECS/lorax.spec index 8b3516b..cfcc0d9 100644 --- a/SPECS/lorax.spec +++ b/SPECS/lorax.spec @@ -4,7 +4,7 @@ Name: lorax Version: 34.9.23 -Release: 1%{?dist}.inferit +Release: 1%{?dist}.inferit.1 Summary: Tool for creating the anaconda install images License: GPLv2+ @@ -17,6 +17,8 @@ Source0: %{name}-%{version}.tar.gz # MSVSphere patches Patch1001: 0001-Backport-dracut-chroot-umount-fix.patch +Patch1002: 0002-Fix-replace-command-utf8-handling.patch +Patch1003: 0003-Add-iconv-template-command.patch BuildRequires: python3-devel BuildRequires: make @@ -192,6 +194,10 @@ make DESTDIR=$RPM_BUILD_ROOT mandir=%{_mandir} install %{_datadir}/lorax/templates.d/* %changelog +* Wed Jul 26 2023 Eugene Zamriy - 34.9.20-1.inferit.1 +- templates: Fixed UTF-8 handling in replace command +- templates: Added iconv command to convert from UTF-8 to another encoding + * Thu Apr 06 2023 Eugene Zamriy - 34.9.20-1.inferit - Backported DracutChroot dirs umount patch from upstream Without that patch pungi fails on building an EL9 image