From e1c500d5c512d72b2980fc77a571c3b05f5c1941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A5=D0=B8=D1=80=D0=B5=D1=86=D0=BA=D0=B8=D0=B9=20=D0=9C?= =?UTF-8?q?=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Fri, 20 Nov 2020 12:33:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=BE=D0=B2=20grep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calculate/templates/template_engine.py | 32 +++++++++++++ tests/templates/test_template_engine.py | 45 +++++++++++++++++++ .../test_template_engine_root/test_grep | 10 +++++ 3 files changed, 87 insertions(+) create mode 100644 tests/templates/testfiles/test_template_engine_root/test_grep diff --git a/calculate/templates/template_engine.py b/calculate/templates/template_engine.py index 1ad20f6..626c2dd 100644 --- a/calculate/templates/template_engine.py +++ b/calculate/templates/template_engine.py @@ -39,6 +39,7 @@ from calculate.variables.datavars import ( FloatType, ListType ) +from calculate.utils.fs import readFile from calculate.variables.loader import Datavars import calculate.templates.template_filters as template_filters @@ -970,6 +971,7 @@ class CalculateExtension(Extension): self.package_atom_parser = PackageAtomParser(chroot_path=chroot_path) self.environment.globals.update({'pkg': self.pkg}) + self.environment.globals.update({'grep': self.grep}) self._datavars = datavars_module self.parameters_processor = parameters_processor @@ -1472,6 +1474,36 @@ 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")) + # TODO: добавить получение домашней директории пользователя + #if fname[0] == "~": + # # Получаем директорию пользователя + # fname = os.path.join(self.homeDir, + # fname.partition("/")[2], "")[:-1] + # TODO: учитывать также root_path + fname = os.path.join( + self.parameters_processor.chroot_path, + fname.lstrip("/")) + fileContent = readFile(fname) + if not fileContent: + return "" + match_data = reg.search(fileContent) + if match_data: + md_groups = match_data.groups() + if md_groups: + return md_groups[0] or "" + else: + return match_data.group() + else: + return "" class TemplateEngine: def __init__(self, directory_path=None, diff --git a/tests/templates/test_template_engine.py b/tests/templates/test_template_engine.py index a17e7cb..ebf073b 100644 --- a/tests/templates/test_template_engine.py +++ b/tests/templates/test_template_engine.py @@ -262,3 +262,48 @@ parameter_2 = {{ vars_1.var_1 }}''' text = template_engine.template_text assert text == output_text + + @pytest.mark.parametrize('case', + [ + { + "name": "pattern not found", + "grep": "'/test_grep', 'QWERTY'", + "result": "" + }, + { + "name": "file not found", + "grep": "'/test_grep_not_found', 'QWERTY'", + "result": "" + }, + { + "name": "pattern found", + "grep": "'/test_grep', 'NOSTATUSLINE=true'", + "result": "NOSTATUSLINE=true" + }, + { + "name": "regexp found", + "grep": "'/test_grep', 'NOSTATUS.*'", + "result": "NOSTATUSLINE=true" + }, + { + "name": "group regexp found", + "grep": "'/test_grep', 'NOSTATUSLINE=(.*)'", + "result": "true" + }, + ], + ids=lambda x:x["name"]) + def test_grep(self, case): + datavars_module = Variables({'vars_1': + Variables({'var_1': + 'test-category/test-package', + 'var_2': 1.2})}) + input_template = '''{% calculate name = 'filename', force -%} + {{ grep(''' + case['grep'] + ''') }}''' + 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_grep b/tests/templates/testfiles/test_template_engine_root/test_grep new file mode 100644 index 0000000..2c05db5 --- /dev/null +++ b/tests/templates/testfiles/test_template_engine_root/test_grep @@ -0,0 +1,10 @@ +#------------------------------------------------------------------------------ +# Modified Calculate Utilities 3.6.7.23 +# Processed template files: +# /var/db/repos/calculate/profiles/templates/3.6/2_ac_install_merge/app-portage/eix/50-calculate +# For modify this file, create /run/calculate/mount/install/etc/eixrc/50-calculate.clt template. +#------------------------------------------------------------------------------ +COLORSCHEME1="true" +NOSTATUSLINE=true +EIX_LIMIT=0 +EIX_LIMIT_COMPACT=0