From 4272faead6ba6bf913fd0a026cf2e2042d7d43c5 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 15:48:43 +0300 Subject: [PATCH] Added default processing of templates without calculate header. fixed #26 --- calculate/templates/template_processor.py | 12 ++++-- tests/templates/test_directory_processor.py | 42 +++++++++++++++++++ tests/templates/test_template_executor.py | 3 -- .../etc.backup/dir_60/file_0 | 1 + .../etc.backup/dir_61/file_0 | 1 + .../templates_43/install/.calculate_directory | 1 + .../templates_43/install/dir_60/file_0 | 1 + .../templates_44/install/.calculate_directory | 1 + .../templates_44/install/dir_61/file_0 | 1 + .../test-category/test-package-1.0/CONTENTS | 2 + 10 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_60/file_0 create mode 100644 tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_61/file_0 create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_43/install/.calculate_directory create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_43/install/dir_60/file_0 create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_44/install/.calculate_directory create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_44/install/dir_61/file_0 diff --git a/calculate/templates/template_processor.py b/calculate/templates/template_processor.py index e4acb45..ace9ee4 100644 --- a/calculate/templates/template_processor.py +++ b/calculate/templates/template_processor.py @@ -204,6 +204,8 @@ class TemplateWrapper: self.template_type = template_type self.template_text = template_text + self.contents_matching = True + # Флаг, указывающий, что нужно удалить файл из target_path перед # применением шаблона. self.remove_original = False @@ -253,10 +255,6 @@ 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: @@ -291,6 +289,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 + def _check_type_conflicts(self) -> None: '''Метод для проверки конфликтов типов.''' if self.parameters.append == 'link': @@ -524,6 +527,7 @@ 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 -- удаляем этот файл. diff --git a/tests/templates/test_directory_processor.py b/tests/templates/test_directory_processor.py index 3da945c..3abbcb1 100644 --- a/tests/templates/test_directory_processor.py +++ b/tests/templates/test_directory_processor.py @@ -1392,6 +1392,48 @@ class TestDirectoryProcessor: directory_processor.process_template_directories() assert os.path.exists(join_paths(CHROOT_PATH, '/etc/copy.gif')) + def test_default_raw_and_append_replace(self): + datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, + 'templates_43') + directory_processor = DirectoryProcessor( + 'install', + datavars_module=datavars, + package='test-category/test-package' + ) + directory_processor.process_template_directories() + assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_60/file_0')) + assert not os.path.exists(join_paths(CHROOT_PATH, + '/etc/dir_60/._cfg0000_file_0')) + + def test_default_raw_and_append_replace_contents_not_matching(self): + template_path = join_paths(CHROOT_PATH, + "templates_44/install/dir_61/file_0") + expected_new_text = ('#' + '-' * 79 + '\n' + + '# Modified by Calculate Utilities 4.0\n' + + '# Processed template files:\n' + + f'# { template_path }\n' + + '#' + '-' * 79 + '\n\nnew content') + datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, + 'templates_44') + directory_processor = DirectoryProcessor( + 'install', + datavars_module=datavars, + package='test-category/test-package' + ) + directory_processor.process_template_directories() + assert os.path.exists(join_paths(CHROOT_PATH,'/etc/dir_61/file_0')) + with open(join_paths(CHROOT_PATH, + '/etc/dir_61/file_0'), 'r') as original_file: + original_text = original_file.read() + assert original_text == "old content\n" + assert os.path.exists(join_paths(CHROOT_PATH, + '/etc/dir_61/._cfg0000_file_0')) + with open(join_paths(CHROOT_PATH, + '/etc/dir_61/._cfg0000_file_0'), + 'r') as new_file: + new_text = new_file.read() + assert new_text == expected_new_text + 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 23b30f5..1869f8f 100644 --- a/tests/templates/test_template_executor.py +++ b/tests/templates/test_template_executor.py @@ -3000,9 +3000,6 @@ AttributeError: module 'os' has no attribute 'suspicious_attribute' template_executor._append_join_file(template_wrapper) assert os.path.exists(target_path) - def test_raw_format_error(self): - pass - def test_to_remove_changed_testfiles(self): shutil.rmtree(os.path.join(CHROOT_PATH, 'etc')) shutil.rmtree(os.path.join(CHROOT_PATH, 'unprotected')) diff --git a/tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_60/file_0 b/tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_60/file_0 new file mode 100644 index 0000000..33194a0 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_60/file_0 @@ -0,0 +1 @@ +old content diff --git a/tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_61/file_0 b/tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_61/file_0 new file mode 100644 index 0000000..33194a0 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/etc.backup/dir_61/file_0 @@ -0,0 +1 @@ +old content diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_43/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_43/install/.calculate_directory new file mode 100644 index 0000000..e1f74a4 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_43/install/.calculate_directory @@ -0,0 +1 @@ +{% calculate action = "install", append = "skip", package = "test-category/test-package" %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_43/install/dir_60/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_43/install/dir_60/file_0 new file mode 100644 index 0000000..b66ba06 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_43/install/dir_60/file_0 @@ -0,0 +1 @@ +new content diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_44/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_44/install/.calculate_directory new file mode 100644 index 0000000..e1f74a4 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_44/install/.calculate_directory @@ -0,0 +1 @@ +{% calculate action = "install", append = "skip", package = "test-category/test-package" %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_44/install/dir_61/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_44/install/dir_61/file_0 new file mode 100644 index 0000000..b66ba06 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_44/install/dir_61/file_0 @@ -0,0 +1 @@ +new content diff --git a/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS b/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS index 0423bad..14e726e 100644 --- a/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS +++ b/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS @@ -4,3 +4,5 @@ obj /etc/dir_17/file_0 c585be6f171462940b44af994a54040d 1593525253 dir /etc/dir_17/dir_0 obj /etc/dir_17/dir_0/file_0 c585be6f171462940b44af994a54040d 1593525253 obj /etc/file_12 ee090b452dbf92d697124eb424f5de5b 1592574626 +dir /etc/dir_60 +obj /etc/dir_60/file_0 c9a9459e4266ea35a612b90dc3653112 1593525253