From ff97850ba2908933e0c21f02cc38fb065c645f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=81?= Date: Wed, 20 Jan 2021 10:19:32 +0300 Subject: [PATCH] Now files permissions are saved after using templates without chmod value. fixed #66 --- calculate/templates/template_processor.py | 14 +++++++++---- tests/templates/test_directory_processor.py | 20 +++++++++++++++++++ .../etc.backup/file_23 | 1 + .../templates_62/install/.calculate_directory | 2 ++ .../templates_62/install/file_23 | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 tests/templates/testfiles/test_dir_processor_root/etc.backup/file_23 create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_62/install/.calculate_directory create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_62/install/file_23 diff --git a/calculate/templates/template_processor.py b/calculate/templates/template_processor.py index 153ee49..b75b815 100644 --- a/calculate/templates/template_processor.py +++ b/calculate/templates/template_processor.py @@ -308,10 +308,11 @@ class TemplateWrapper: self._check_user_changes() - if self.target_type is not None and self.contents_matching: - # Удаляем целевой файл, если append = 'replace' - if self.parameters.append and self.parameters.append == "replace": - self.remove_original = True + # if self.target_type is not None and self.contents_matching: + # # Удаляем целевой файл, если append = 'replace' + # if (self.parameters.append and + # self.parameters.append == "replace"): + # self.remove_original = True def _check_type_conflicts(self) -> NoReturn: '''Метод для проверки конфликтов типов.''' @@ -1034,6 +1035,8 @@ class TemplateExecutor: # Если шаблон пуст, параметром source задан входной файл, # и при этом формат шаблона raw и append = 'replace' # значит этот шаблон предназначен для копирования. + if template_object.target_type is not None: + self._remove_file(template_object.target_path) output_file_md5 = self._copy_from_source(template_object, chown=chown, chmod=chmod) @@ -1049,6 +1052,9 @@ class TemplateExecutor: input_text = self._get_input_text(template_object, replace=replace) + if (replace and template_object.target_type is not None and + os.path.exists(output_path)): + self._clear_file(output_path) # Если шаблон не исполнительный разбираем входной текст. parsed_input = template_format( diff --git a/tests/templates/test_directory_processor.py b/tests/templates/test_directory_processor.py index 2acbe38..9cc5fd4 100644 --- a/tests/templates/test_directory_processor.py +++ b/tests/templates/test_directory_processor.py @@ -1788,6 +1788,26 @@ class TestDirectoryProcessor: '/etc/dir_78/file_1')).st_mode assert int(mode) == 0o100655 + def test_saving_permissions_after_file_replace(self): + if os.getuid() == 0: + datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, + 'templates_62') + mode = os.stat(join_paths(CHROOT_PATH, '/etc/file_23')).st_mode + if mode != 0o100645: + os.chmod(join_paths(CHROOT_PATH, '/etc/file_23'), 0o100645) + + directory_processor = DirectoryProcessor('install', + datavars_module=datavars) + directory_processor.process_template_directories() + + with open(join_paths(CHROOT_PATH, + '/etc/file_23'), 'r') as target_file: + target_text = target_file.read() + assert target_text == "joy division -- new dawn fades" + + mode = os.stat(join_paths(CHROOT_PATH, '/etc/file_23')).st_mode + assert int(mode) == 0o100645 + def test_using_file_templates_from_base_directory(self): datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, 'templates_61') diff --git a/tests/templates/testfiles/test_dir_processor_root/etc.backup/file_23 b/tests/templates/testfiles/test_dir_processor_root/etc.backup/file_23 new file mode 100644 index 0000000..fcb2c26 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/etc.backup/file_23 @@ -0,0 +1 @@ +joy division -- love will tear us apart diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_62/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_62/install/.calculate_directory new file mode 100644 index 0000000..7785159 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_62/install/.calculate_directory @@ -0,0 +1,2 @@ +{% calculate action = "install", append = 'skip', +package = "test-category/test-package" %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_62/install/file_23 b/tests/templates/testfiles/test_dir_processor_root/templates_62/install/file_23 new file mode 100644 index 0000000..d6a73ec --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_62/install/file_23 @@ -0,0 +1 @@ +joy division -- new dawn fades