1 From 2eceee39ef4d78e77101ece088d7833f1b83fdad Mon Sep 17 00:00:00 2001
2 From: Michael Tremer <michael.tremer@ipfire.org>
3 Date: Tue, 2 Oct 2012 22:04:43 +0200
4 Subject: [PATCH 2/4] lexer: Add shell commands with %(echo 123..).
6 It is possible to use shell commands, just like $(...) in shell.
7 It's a handy feature when you need some output of a tool
10 Every time the variable is expanded, the command is called.
11 It is not ensured that the personality is the same as in
12 the build environment.
14 python/pakfire/packages/lexer.py | 37 ++++++++++++++++++++++++++++++++++++-
15 1 file changed, 36 insertions(+), 1 deletion(-)
17 diff --git a/python/pakfire/packages/lexer.py b/python/pakfire/packages/lexer.py
18 index 09a8215..af6dda0 100644
19 --- a/python/pakfire/packages/lexer.py
20 +++ b/python/pakfire/packages/lexer.py
21 @@ -6,6 +6,8 @@ import re
22 from pakfire.constants import *
23 from pakfire.i18n import _
25 +import pakfire.chroot
28 #log = logging.getLogger("pakfire.lexer")
29 log = logging.getLogger("pakfire")
30 @@ -90,7 +92,7 @@ LEXER_UNEXPORT = re.compile(r"^unexport\s+([A-Za-z0-9_\-]+)$")
31 LEXER_INCLUDE = re.compile(r"^include\s+(.+)$")
33 LEXER_VARIABLE = re.compile(r"\%\{([A-Za-z0-9_\-]+)\}")
34 -LEXER_SHELL = re.compile(r"\%\(.*\)")
35 +LEXER_SHELL = re.compile(r"\%\((.*)\)")
37 LEXER_IF_IF = re.compile(r"^if\s+(.*)\s+(==|!=)\s+(.*)\s*")
38 LEXER_IF_ELIF = re.compile(r"^elif\s+(.*)\s*(==|!=)\s*(.*)\s*")
39 @@ -204,6 +206,18 @@ class Lexer(object):
43 + # First run all embedded commands.
45 + m = re.search(LEXER_SHELL, s)
49 + command = m.group(1)
50 + result = self.exec_command(command)
52 + s = s.replace("%%(%s)" % command, result or "")
54 + # Then expand the variables.
56 m = re.search(LEXER_VARIABLE, s)
58 @@ -214,6 +228,27 @@ class Lexer(object):
62 + def exec_command(self, command):
63 + # Expand all variables in the command.
64 + command = self.expand_string(command)
66 + # If the command is empty, we don't do anything.
70 + # Do we need to chroot and change personality?
72 + output = pakfire.chroot.do(command, shell=True, returnOutput=1, logger=log)
79 + output = output.rstrip("\n")
83 def get_var(self, key, default=None, raw=False):
85 definitions.update(self.root.definitions)