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