From 6ef01a7b84ce9ae8c80e9442ffb7edab3db15dd3 Mon Sep 17 00:00:00 2001 From: tigro Date: Fri, 22 Dec 2023 13:14:11 +0300 Subject: [PATCH] - templates: Fixed UTF-8 handling in replace command - templates: Added iconv command to convert from UTF-8 to another encoding --- ...01-Fix-replace-command-utf8-handling.patch | 51 +++++++++++++++++++ SOURCES/0002-Add-iconv-template-command.patch | 38 ++++++++++++++ SPECS/lorax.spec | 6 +++ 3 files changed, 95 insertions(+) create mode 100644 SOURCES/0001-Fix-replace-command-utf8-handling.patch create mode 100644 SOURCES/0002-Add-iconv-template-command.patch diff --git a/SOURCES/0001-Fix-replace-command-utf8-handling.patch b/SOURCES/0001-Fix-replace-command-utf8-handling.patch new file mode 100644 index 0000000..9bc3945 --- /dev/null +++ b/SOURCES/0001-Fix-replace-command-utf8-handling.patch @@ -0,0 +1,51 @@ +From fa48ddd7ca1d8a4f24f2e1de6880fd5bd6c770f5 Mon Sep 17 00:00:00 2001 +From: Eugene Zamriy +Date: Wed, 26 Jul 2023 22:45:32 +0300 +Subject: [PATCH 1/2] 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 209757a..21b9698 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.43.0 + diff --git a/SOURCES/0002-Add-iconv-template-command.patch b/SOURCES/0002-Add-iconv-template-command.patch new file mode 100644 index 0000000..6126b84 --- /dev/null +++ b/SOURCES/0002-Add-iconv-template-command.patch @@ -0,0 +1,38 @@ +From 432c907e8f5d306611e84cd6fe56087362794f68 Mon Sep 17 00:00:00 2001 +From: tigro +Date: Fri, 22 Dec 2023 13:06:59 +0300 +Subject: [PATCH 2/2] Add iconv template command + +--- + src/pylorax/ltmpl.py | 15 +++++++++++++++ + 1 file changed, 15 insertions(+) + +diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py +index ccd297c..a06534a 100644 +--- a/src/pylorax/ltmpl.py ++++ b/src/pylorax/ltmpl.py +@@ -275,6 +275,21 @@ class LoraxTemplateRunner(TemplateRunner): + for pkg in debug_pkgs: + f.write("%s\n" % pkg) + ++ 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.43.0 + diff --git a/SPECS/lorax.spec b/SPECS/lorax.spec index dc5235a..5425e93 100644 --- a/SPECS/lorax.spec +++ b/SPECS/lorax.spec @@ -15,6 +15,8 @@ URL: https://github.com/weldr/lorax # git checkout -b archive-branch lorax-%%{version}-%%{release} # tito build --tgz Source0: %{name}-%{version}.tar.gz +Patch1: 0001-Fix-replace-command-utf8-handling.patch +Patch2: 0002-Add-iconv-template-command.patch BuildRequires: python3-devel @@ -261,6 +263,10 @@ getent passwd weldr >/dev/null 2>&1 || useradd -r -g weldr -d / -s /sbin/nologin %{_mandir}/man1/composer-cli.1* %changelog +* Fri Dec 22 2023 Arkady L. Shane - 28.14.70-1.inferit +- templates: Fixed UTF-8 handling in replace command +- templates: Added iconv command to convert from UTF-8 to another encoding + * Wed Jul 26 2023 MSVSphere Packaging Team - 28.14.70-1 - Rebuilt for MSVSphere 8.8