From 2eceee39ef4d78e77101ece088d7833f1b83fdad Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 2 Oct 2012 22:04:43 +0200 Subject: [PATCH] lexer: Add shell commands with %(echo 123..). It is possible to use shell commands, just like $(...) in shell. It's a handy feature when you need some output of a tool in a variable. Every time the variable is expanded, the command is called. It is not ensured that the personality is the same as in the build environment. --- python/pakfire/packages/lexer.py | 37 +++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/python/pakfire/packages/lexer.py b/python/pakfire/packages/lexer.py index 09a8215eb..af6dda0fd 100644 --- a/python/pakfire/packages/lexer.py +++ b/python/pakfire/packages/lexer.py @@ -6,6 +6,8 @@ import re from pakfire.constants import * from pakfire.i18n import _ +import pakfire.chroot + import logging #log = logging.getLogger("pakfire.lexer") log = logging.getLogger("pakfire") @@ -90,7 +92,7 @@ LEXER_UNEXPORT = re.compile(r"^unexport\s+([A-Za-z0-9_\-]+)$") LEXER_INCLUDE = re.compile(r"^include\s+(.+)$") LEXER_VARIABLE = re.compile(r"\%\{([A-Za-z0-9_\-]+)\}") -LEXER_SHELL = re.compile(r"\%\(.*\)") +LEXER_SHELL = re.compile(r"\%\((.*)\)") LEXER_IF_IF = re.compile(r"^if\s+(.*)\s+(==|!=)\s+(.*)\s*") LEXER_IF_ELIF = re.compile(r"^elif\s+(.*)\s*(==|!=)\s*(.*)\s*") @@ -204,6 +206,18 @@ class Lexer(object): if s is None: return "" + # First run all embedded commands. + while s: + m = re.search(LEXER_SHELL, s) + if not m: + break + + command = m.group(1) + result = self.exec_command(command) + + s = s.replace("%%(%s)" % command, result or "") + + # Then expand the variables. while s: m = re.search(LEXER_VARIABLE, s) if not m: @@ -214,6 +228,27 @@ class Lexer(object): return s + def exec_command(self, command): + # Expand all variables in the command. + command = self.expand_string(command) + + # If the command is empty, we don't do anything. + if not command: + return + + # Do we need to chroot and change personality? + try: + output = pakfire.chroot.do(command, shell=True, returnOutput=1, logger=log) + + except Error: + return + + # Strip newline. + if output: + output = output.rstrip("\n") + + return output + def get_var(self, key, default=None, raw=False): definitions = {} definitions.update(self.root.definitions) -- 2.39.5