diff --git a/calculate/templates/template_engine.py b/calculate/templates/template_engine.py index 04a106c..930c1d1 100644 --- a/calculate/templates/template_engine.py +++ b/calculate/templates/template_engine.py @@ -17,6 +17,7 @@ from pprint import pprint import copy import re import os +import stat from ..utils.package import ( PackageAtomParser, @@ -972,6 +973,7 @@ class CalculateExtension(Extension): self.environment.globals.update({'pkg': self.pkg}) self.environment.globals.update({'grep': self.grep}) + self.environment.globals.update({'exists': self.exists}) self._datavars = datavars_module self.parameters_processor = parameters_processor @@ -1473,15 +1475,7 @@ class CalculateExtension(Extension): return Version() return package.version - @contextfunction - def grep(self, context, fname, regpattern) -> str: - ''' - Метод реализующий функция grep - ''' - try: - reg = re.compile(regpattern) - except re.error: - raise TemplateSyntaxError(_("Wrong regular expression")) + def get_full_filepath(self, fname): # TODO: добавить получение домашней директории пользователя #if fname[0] == "~": # # Получаем директорию пользователя @@ -1491,6 +1485,18 @@ class CalculateExtension(Extension): fname = os.path.join( self.parameters_processor.chroot_path, fname.lstrip("/")) + return fname + + @contextfunction + def grep(self, context, fname, regpattern) -> str: + ''' + Метод реализующий функцию grep + ''' + fname = self.get_full_filepath(fname) + try: + reg = re.compile(regpattern) + except re.error: + raise TemplateSyntaxError(_("Wrong regular expression")) fileContent = readFile(fname) if not fileContent: return "" @@ -1504,6 +1510,31 @@ class CalculateExtension(Extension): else: return "" + @contextfunction + def exists(self, context, fname) -> str: + ''' + Метод реализующий функцию exists + ''' + fname = self.get_full_filepath(fname) + + try: + check_map = ( + ('f', stat.S_ISREG), + ('d', stat.S_ISDIR), + ('l', stat.S_ISLNK), + ('b', stat.S_ISBLK), + ('c', stat.S_ISCHR), + ('p', stat.S_ISFIFO), + ('s', stat.S_ISSOCK)) + fmode = os.lstat(fname) + for t, func in check_map: + if func(fmode.st_mode): + return t + else: + return 'f' + except OSError: + return "" + class TemplateEngine: def __init__(self, directory_path=None, datavars_module=Variables(), diff --git a/tests/templates/test_template_engine.py b/tests/templates/test_template_engine.py index ebf073b..e6d3ef2 100644 --- a/tests/templates/test_template_engine.py +++ b/tests/templates/test_template_engine.py @@ -307,3 +307,48 @@ parameter_2 = {{ vars_1.var_1 }}''' text = template_engine.template_text assert text == output_text + + @pytest.mark.parametrize('case', + [ + { + "name": "exists directory", + "exists": "/test_exists_dir", + "result": "d" + }, + { + "name": "exists file", + "exists": "/test_exists_file", + "result": "f" + }, + { + "name": "exists symlink", + "exists": "/test_exists_symlink", + "result": "l" + }, + { + "name": "exists broken symlink", + "exists": "/test_exists_broken_symlink", + "result": "l" + }, + { + "name": "not exists symlink", + "exists": "/test_exists_qwerty", + "result": "" + }, + ], + ids=lambda x:x["name"]) + def test_exists(self, case): + datavars_module = Variables({'vars_1': + Variables({'var_1': + 'test-category/test-package', + 'var_2': 1.2})}) + input_template = '''{% calculate name = 'filename', force -%} + {{ exists(\'''' + case['exists'] + '''\') }}''' + output_text = case["result"] + template_engine = TemplateEngine(appends_set=APPENDS_SET, + datavars_module=datavars_module, + chroot_path=CHROOT_PATH) + template_engine.process_template_from_string(input_template, FILE) + + text = template_engine.template_text + assert text == output_text diff --git a/tests/templates/testfiles/test_template_engine_root/test_exists_broken_symlink b/tests/templates/testfiles/test_template_engine_root/test_exists_broken_symlink new file mode 120000 index 0000000..00189a6 --- /dev/null +++ b/tests/templates/testfiles/test_template_engine_root/test_exists_broken_symlink @@ -0,0 +1 @@ +test_exists_zxcvb \ No newline at end of file diff --git a/tests/templates/testfiles/test_template_engine_root/test_exists_dir/.keep b/tests/templates/testfiles/test_template_engine_root/test_exists_dir/.keep new file mode 100644 index 0000000..e69de29 diff --git a/tests/templates/testfiles/test_template_engine_root/test_exists_file b/tests/templates/testfiles/test_template_engine_root/test_exists_file new file mode 100644 index 0000000..e69de29 diff --git a/tests/templates/testfiles/test_template_engine_root/test_exists_symlink b/tests/templates/testfiles/test_template_engine_root/test_exists_symlink new file mode 120000 index 0000000..1067d90 --- /dev/null +++ b/tests/templates/testfiles/test_template_engine_root/test_exists_symlink @@ -0,0 +1 @@ +test_exists_file \ No newline at end of file