diff --git a/calculate/templates/template_processor.py b/calculate/templates/template_processor.py index 89f98b5..96a14d4 100644 --- a/calculate/templates/template_processor.py +++ b/calculate/templates/template_processor.py @@ -1961,6 +1961,9 @@ class DirectoryProcessor: else: self._add_package_to_group(group_name, package_name) + # Создаем переменную для указания текущих шаблонов, если ее еще нет. + self._make_current_template_var() + # Инициализируем шаблонизатор. self.template_engine = TemplateEngine( datavars_module=self.datavars_module, @@ -2089,6 +2092,29 @@ class DirectoryProcessor: continue return True + def _make_current_template_var(self) -> None: + var_path = ['main', 'cl'] + namespace = self.datavars_module + print("namespace:", namespace) + + for field in var_path: + if field not in namespace: + if isinstance(self.datavars_module, (Datavars, NamespaceNode)): + namespace = NamespaceNode(field, parent=namespace) + else: + namespace[field] = Variables() + namespace = namespace[field] + else: + namespace = namespace[field] + + if 'current_template' not in namespace: + if isinstance(self.datavars_module, (Datavars, NamespaceNode)): + variable = VariableNode('current_template', namespace, + variable_type=StringType, + source="") + else: + namespace['current_template'] = "" + def process_template_directories(self) -> None: '''Метод для обхода шаблонов, содержащихся в каталогах из main.cl_template.path.''' @@ -2429,7 +2455,8 @@ class DirectoryProcessor: # Удаляем все параметры, которые не наследуются и используем # полученный контейнер для сбора параметров файлов шаблонов. template_parameters.remove_not_inheritable() - template_path = os.path.join(current_directory_path, template_name) + template_path = os.path.join(current_directory_path, + template_name) # Применяем к файлу шаблона шаблонизатор. template_text = self._parse_template(template_parameters, @@ -2625,6 +2652,10 @@ class DirectoryProcessor: else: template_path = join_paths(template_directory, template_name) + if isinstance(self.datavars_module, (Datavars, NamespaceNode)): + self.datavars_module.main.cl['current_template'].set(template_path) + else: + self.datavars_module.main.cl['current_template'] = template_path try: self.template_engine.process_template(template_name, template_type, diff --git a/calculate/vars/main/cl/__init__.py b/calculate/vars/main/cl/__init__.py index 038cf07..0ee1a23 100644 --- a/calculate/vars/main/cl/__init__.py +++ b/calculate/vars/main/cl/__init__.py @@ -32,6 +32,9 @@ Variable("chroot_status", type=BooleanType, Variable("system_boot_set", type=BooleanType, source=Calculate(is_system_boot)) +# Переменная, в которую будут помещаться пути к текущим шаблонам. +Variable("current_template", type=StringType, source="") + class CmdlineParams(object): """ Параметры опции загрузки ядра calculate= diff --git a/tests/templates/test_directory_processor.py b/tests/templates/test_directory_processor.py index d5a5be8..c65b83b 100644 --- a/tests/templates/test_directory_processor.py +++ b/tests/templates/test_directory_processor.py @@ -1436,6 +1436,29 @@ class TestDirectoryProcessor: new_text = new_file.read() assert new_text == expected_new_text + def test_current_template_variable(self): + datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, + 'templates_45') + template_path = join_paths(CHROOT_PATH, + "templates_45/install/dir_62/file_0") + expected_output = ('#' + '-' * 79 + '\n' + + '# Modified by Calculate Utilities 4.0\n' + + '# Processed template files:\n' + + f'# {template_path}\n' + + '#' + '-' * 79 + + f'\n\n\ntemplate = {template_path}') + 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_62/file_0')) + with open(join_paths(CHROOT_PATH, + '/etc/dir_62/file_0'), 'r') as output_file: + output_text = output_file.read() + assert output_text == expected_output + def test_view_tree(self): list_path = join_paths(CHROOT_PATH, '/etc') show_tree(list_path) diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_45/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_45/install/.calculate_directory new file mode 100644 index 0000000..e1f74a4 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_45/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_45/install/dir_62/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_45/install/dir_62/file_0 new file mode 100644 index 0000000..159adf6 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_45/install/dir_62/file_0 @@ -0,0 +1,2 @@ +{% calculate append = 'join', format = 'raw' %} +template = {{ main.cl.current_template }}