]> git.ipfire.org Git - people/amarx/ipfire-3.x.git/blob - pakfire/patches/0002-lexer-Add-shell-commands-with-echo-123.patch
pakfire: Import latest upstream patches.
[people/amarx/ipfire-3.x.git] / pakfire / patches / 0002-lexer-Add-shell-commands-with-echo-123.patch
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..).
5
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
8 in a variable.
9
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.
13 ---
14 python/pakfire/packages/lexer.py | 37 ++++++++++++++++++++++++++++++++++++-
15 1 file changed, 36 insertions(+), 1 deletion(-)
16
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 _
24
25 +import pakfire.chroot
26 +
27 import logging
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+(.+)$")
32
33 LEXER_VARIABLE = re.compile(r"\%\{([A-Za-z0-9_\-]+)\}")
34 -LEXER_SHELL = re.compile(r"\%\(.*\)")
35 +LEXER_SHELL = re.compile(r"\%\((.*)\)")
36
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):
40 if s is None:
41 return ""
42
43 + # First run all embedded commands.
44 + while s:
45 + m = re.search(LEXER_SHELL, s)
46 + if not m:
47 + break
48 +
49 + command = m.group(1)
50 + result = self.exec_command(command)
51 +
52 + s = s.replace("%%(%s)" % command, result or "")
53 +
54 + # Then expand the variables.
55 while s:
56 m = re.search(LEXER_VARIABLE, s)
57 if not m:
58 @@ -214,6 +228,27 @@ class Lexer(object):
59
60 return s
61
62 + def exec_command(self, command):
63 + # Expand all variables in the command.
64 + command = self.expand_string(command)
65 +
66 + # If the command is empty, we don't do anything.
67 + if not command:
68 + return
69 +
70 + # Do we need to chroot and change personality?
71 + try:
72 + output = pakfire.chroot.do(command, shell=True, returnOutput=1, logger=log)
73 +
74 + except Error:
75 + return
76 +
77 + # Strip newline.
78 + if output:
79 + output = output.rstrip("\n")
80 +
81 + return output
82 +
83 def get_var(self, key, default=None, raw=False):
84 definitions = {}
85 definitions.update(self.root.definitions)
86 --
87 1.7.11.4
88