From f6a5e3f1eca3b640935caf77aa4cc6501242be44 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: Fri, 20 Nov 2020 13:52:43 +0300 Subject: [PATCH] Fixed append = 'replace' usage. --- calculate/templates/template_processor.py | 50 +++++++++++---------- tests/templates/test_directory_processor.py | 9 ---- tests/templates/test_template_executor.py | 4 +- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/calculate/templates/template_processor.py b/calculate/templates/template_processor.py index 4c585dd..a84f021 100644 --- a/calculate/templates/template_processor.py +++ b/calculate/templates/template_processor.py @@ -253,13 +253,15 @@ class TemplateWrapper: self.target_is_link = os.path.islink(target_file_path) + # Удаляем целевой файл, если append = 'replace' + if self.parameters.append and self.parameters.append == "replace": + self.remove_original = True + # Если установлен параметр mirror и есть параметр source, # содержащий несуществующий путь -- удаляем целевой файл. if self.parameters.source is True and self.parameters.mirror: self.remove_original = True else: - if self.parameters.append and self.parameters.append == "replace": - self.remove_original = True self.target_type = None if self.format_class is not None and self.format_class.EXECUTABLE: @@ -979,23 +981,23 @@ class TemplateExecutor: template_object.template_path, ignore_comments=True) - if (not parsed_template and not template_object.parameters.source - and template_object.format_class.EXECUTABLE): - # Если шаблон пуст, параметром source не задан входной файл, - # а шаблон является исполнительным - if template_object.target_type is DIR: - self._remove_directory(template_object.target_path) - shutil.copytree(input_path, - template_object.target_path) - elif template_object.target_type is FILE: - self._remove_file(template_object.target_path) - shutil.copy(input_path, - template_object.template_path) + if (not parsed_template and template_object.parameters.source + and template_object.parameters.append == "replace" + and template_object.parameters.format == "raw"): + # Если шаблон пуст, параметром source задан входной файл, + # и при этом формат шаблона raw и append = 'replace' + # значит этот шаблон предназначен для копирования. + shutil.copy(input_path, output_path) if chown: - self._chown_file(template_object.target_path, chown) + self._chown_file(template_object.output_path, chown) if chmod: - self._chmod_file(template_object.target_path, chmod) + self._chmod_file(template_object.output_path, chmod) + + input_file_md5 = None + with open(output_path, "rb") as output_file: + output_file_data = output_file.read() + output_file_md5 = hashlib.md5(output_file_data).hexdigest() elif not template_object.format_class.EXECUTABLE: # Действия для шаблонов не являющихся исполнительными. @@ -1039,8 +1041,8 @@ class TemplateExecutor: # Удаляем форматный объект входного файла. del(parsed_input) - output_text_md5 = hashlib.md5(output_text.encode()).hexdigest() - input_text_md5 = hashlib.md5(input_text.encode()).hexdigest() + output_file_md5 = hashlib.md5(output_text.encode()).hexdigest() + input_file_md5 = hashlib.md5(input_text.encode()).hexdigest() for save_path in output_paths: if not os.path.exists(os.path.dirname(save_path)): @@ -1089,7 +1091,7 @@ class TemplateExecutor: template_object.update_contents_from_list(changed_files) return - if input_text_md5 != output_text_md5: + if input_file_md5 != output_file_md5: if template_object.target_type is None: self.changed_files[ template_object.target_path] = 'N' @@ -1111,7 +1113,7 @@ class TemplateExecutor: template_object.remove_from_contents() else: template_object.add_to_contents( - file_md5=output_text_md5) + file_md5=output_file_md5) else: if template_object.target_type is not None and not replace: if (not input_path.startswith(self.cl_config_archive_path) or @@ -1141,11 +1143,11 @@ class TemplateExecutor: # Удаляем форматный объект входного файла. del(parsed_input) - output_text_md5 = hashlib.md5(output_text.encode()).hexdigest() + output_file_md5 = hashlib.md5(output_text.encode()).hexdigest() if not self.calculate_config_file.compare_md5( template_object.target_path, - output_text_md5): + output_file_md5): if not os.path.exists(os.path.dirname(output_path)): self._create_directory( template_object, @@ -1164,11 +1166,11 @@ class TemplateExecutor: # Обновляем CL. self.calculate_config_file.set_files_md5( template_object.target_path, - output_text_md5) + output_file_md5) self.changed_files[template_object.target_path] = 'C' # Обновляем CONTENTS. - template_object.add_to_contents(file_md5=output_text_md5) + template_object.add_to_contents(file_md5=output_file_md5) else: # Действия если CL совпало. Hичего не делаем. pass diff --git a/tests/templates/test_directory_processor.py b/tests/templates/test_directory_processor.py index 20f42cb..3da945c 100644 --- a/tests/templates/test_directory_processor.py +++ b/tests/templates/test_directory_processor.py @@ -1392,15 +1392,6 @@ class TestDirectoryProcessor: directory_processor.process_template_directories() assert os.path.exists(join_paths(CHROOT_PATH, '/etc/copy.gif')) - # def test_for_new_file(self): - # datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, - # 'templates_43') - # directory_processor = DirectoryProcessor( - # 'merge', - # datavars_module=datavars, - # package='test-category/test-package' - # ) - def test_view_tree(self): list_path = join_paths(CHROOT_PATH, '/etc') show_tree(list_path) diff --git a/tests/templates/test_template_executor.py b/tests/templates/test_template_executor.py index 96670de..23b30f5 100644 --- a/tests/templates/test_template_executor.py +++ b/tests/templates/test_template_executor.py @@ -2979,13 +2979,14 @@ AttributeError: module 'os' has no attribute 'suspicious_attribute' def test_using_mirror_for_coping_files(self): target_path = join_paths( CHROOT_PATH, - '/etc/append_replace_file_testfiles/file_2.png') + '/etc/append_replace_file_testfiles/file_1.png') source_path = join_paths( CHROOT_PATH, '/etc/append_replace_file_testfiles/logo.png') parameters_object = ParametersContainer({'package': test_package_name, 'append': 'replace', + 'source': source_path, 'format': 'raw', 'mirror': True}) @@ -2997,6 +2998,7 @@ AttributeError: module 'os' has no attribute 'suspicious_attribute' chroot_path=CHROOT_PATH, config_archive_path=CONFIG_ARCHIVE_PATH) template_executor._append_join_file(template_wrapper) + assert os.path.exists(target_path) def test_raw_format_error(self): pass