From 6af0f4b63f45b7293e633b7c4af895b6a8ff2425 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: Tue, 24 Nov 2020 15:20:54 +0300 Subject: [PATCH] Warnings from template executor is added. --- calculate/templates/template_processor.py | 31 ++++++++++++++----- .../calculate/config-archive/etc/dir_0/file | 4 +++ 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir_0/file diff --git a/calculate/templates/template_processor.py b/calculate/templates/template_processor.py index c084e0f..58c4680 100644 --- a/calculate/templates/template_processor.py +++ b/calculate/templates/template_processor.py @@ -205,6 +205,7 @@ class TemplateWrapper: self.template_text = template_text self.contents_matching = True + self.ca_is_missed = False # Флаг, указывающий, что нужно удалить файл из target_path перед # применением шаблона. @@ -527,7 +528,6 @@ class TemplateWrapper: self.contents_matching = self.target_package.check_contents_data( self.target_path, symlink=self.target_is_link) - print("matching:", self.contents_matching) # Если по целевому пути файл не относящийся к какому-либо пакету и # присутствует параметр autoupdate -- удаляем этот файл. @@ -541,7 +541,11 @@ class TemplateWrapper: if self.parameters.source: self.input_path = self.parameters.source elif self.cfg_list and not self.parameters.unbound: - self.input_path = self.archive_path + if os.path.exists(self.archive_path): + self.input_path = self.archive_path + else: + self.input_path = self.target_path + self.ca_is_missed = True else: self.input_path = self.target_path @@ -551,7 +555,11 @@ class TemplateWrapper: if self.parameters.source: self.input_path = self.parameters.source else: - self.input_path = self.archive_path + if os.path.exists(self.archive_path): + self.input_path = self.archive_path + else: + self.input_path = self.target_path + self.ca_is_missed = True self.output_path = self._get_cfg_path(self.target_path) @@ -598,7 +606,6 @@ class TemplateWrapper: def add_to_contents(self, file_md5=None) -> None: '''Метод для добавления целевого файла в CONTENTS.''' - print("add to contents") if self.target_package is None: return @@ -747,7 +754,8 @@ class TemplateExecutor: # Словарь с данными о результате работы исполнительного метода. self.executor_output = {'target_path': None, 'stdout': None, - 'stderr': None} + 'stderr': None, + 'warning': None} if parameters.append == 'skip': return self.executor_output @@ -814,6 +822,11 @@ class TemplateExecutor: self.executor_output['target_path'] =\ template_object.target_path + if template_object.ca_is_missed: + self.executor_output['warning'] = ( + "archive file is missed," + " target file was used instead") + return self.executor_output def save_changes(self): @@ -952,7 +965,6 @@ class TemplateExecutor: join_before=False, replace=False) -> None: '''Метод описывающий действия при append = "join", если шаблон -- файл. Объединяет шаблон с целевым файлом.''' - print("join file") input_path = template_object.input_path output_path = template_object.output_path @@ -1148,8 +1160,7 @@ class TemplateExecutor: input_text = input_file.read() return input_text except UnicodeDecodeError: - raise TemplateExecutorError( - "can not join binary files.") + raise TemplateExecutorError("can not join binary files.") return '' def _remove_cfg_files(self, template_object: TemplateWrapper) -> None: @@ -2645,6 +2656,10 @@ class DirectoryProcessor: if output['target_path'] is not None: target_path = output['target_path'] + if output['warning'] is not None: + self.output.set_warning(f"{output['warning']}." + f" Template: {template_path}") + # Если есть вывод от параметра run -- выводим как info. if output['stdout'] is not None: self.output.set_info("stdout from template: {}:\n{}\n".format( diff --git a/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir_0/file b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir_0/file new file mode 100644 index 0000000..83825f2 --- /dev/null +++ b/tests/templates/testfiles/test_wrapper_root/var/lib/calculate/config-archive/etc/dir_0/file @@ -0,0 +1,4 @@ +options { + parameter_1 no; + parameter_2 yes; +};