From be4f916cc56bc8eca4b482b710d51ac8eba521ed Mon Sep 17 00:00:00 2001 From: Sean Li <22557931+sean0407@users.noreply.github.com> Date: Wed, 21 Oct 2026 06:19:04 +0800 Subject: [PATCH] Read config and unattend files as UTF-8 --- glazier/lib/actions/sysprep.py | 4 ++-- glazier/lib/actions/sysprep_test.py | 37 +++++++++++++++++++++++++++++ glazier/lib/config/files.py | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/glazier/lib/actions/sysprep.py b/glazier/lib/actions/sysprep.py index cae96ef9..e3de8eab 100644 --- a/glazier/lib/actions/sysprep.py +++ b/glazier/lib/actions/sysprep.py @@ -37,13 +37,13 @@ def _EditUnattend(self, """ lines = [] try: - with open(unattend_path) as unattend: + with open(unattend_path, encoding='utf-8') as unattend: lines = unattend.readlines() lines = [ re.sub('.*?', '%s' % zone, l) for l in lines ] - with open(unattend_path, 'w') as unattend: + with open(unattend_path, 'w', encoding='utf-8') as unattend: unattend.write(''.join(lines)) except IOError as e: raise ActionError('Unable to set time zone in unattend.xml') from e diff --git a/glazier/lib/actions/sysprep_test.py b/glazier/lib/actions/sysprep_test.py index 030f28be..a7fdf467 100644 --- a/glazier/lib/actions/sysprep_test.py +++ b/glazier/lib/actions/sysprep_test.py @@ -14,11 +14,20 @@ """Tests for glazier.lib.actions.sysprep.""" +import builtins from unittest import mock from absl.testing import absltest from glazier.lib import test_utils from glazier.lib.actions import sysprep +from glazier.lib.config import files + +_REAL_OPEN = builtins.open + + +def _open_with_cp1252_default(*args, **kwargs): + kwargs.setdefault('encoding', 'cp1252') + return _REAL_OPEN(*args, **kwargs) UNATTEND_XML = r""" @@ -57,6 +66,34 @@ class SysprepTest(test_utils.GlazierTestCase): + def test_config_uses_utf8(self): + yaml_path = self.create_tempfile(file_path='utf8.yaml').full_path + with _REAL_OPEN(yaml_path, 'wb') as handle: + handle.write(b'name: caf\xc3\xa9') + + with mock.patch('builtins.open', side_effect=_open_with_cp1252_default): + config = files.Read(yaml_path) + + self.assertEqual(config['name'], 'caf\u00e9') + + def test_unattend_uses_utf8(self): + unattend_path = self.create_tempfile( + file_path='utf8-unattend.xml', content=UNATTEND_XML).full_path + xml_data = UNATTEND_XML.encode('ascii').replace( + b'Employee', + b'cafe\xcc\x81') + with _REAL_OPEN(unattend_path, 'wb') as handle: + handle.write(xml_data) + + st = sysprep.SetUnattendTimeZone([], mock.Mock()) + with mock.patch('builtins.open', side_effect=_open_with_cp1252_default): + st._EditUnattend('Yakutsk Standard Time', unattend_path) + + with _REAL_OPEN(unattend_path, 'rb') as handle: + updated_xml = handle.read().decode('utf-8') + self.assertIn('cafe\u0301', updated_xml) + self.assertIn('Yakutsk Standard Time', updated_xml) + @mock.patch('glazier.lib.buildinfo.BuildInfo', autospec=True) def test_set_unattend_time_zone_edit_unattend(self, mock_buildinfo): diff --git a/glazier/lib/config/files.py b/glazier/lib/config/files.py index 7e1423c1..f3ec1129 100644 --- a/glazier/lib/config/files.py +++ b/glazier/lib/config/files.py @@ -149,7 +149,7 @@ def _YamlReader(path: str) -> Any: The parsed content of the yaml file. """ try: - with open(path, 'r') as yaml_file: + with open(path, 'r', encoding='utf-8') as yaml_file: yaml_config = yaml.safe_load(yaml_file) except IOError as e: raise FileReadError(path) from e