From f5164d6460d1952ce99f41252d3b5ffbdb18d0d9 Mon Sep 17 00:00:00 2001 From: Will Woods Date: Wed, 6 Jul 2011 18:02:20 -0400 Subject: [PATCH] ltmpl: do brace expansion on the entire template Handle brace expansion while parsing the template rather than individually per-command. This is closer to how bash does things anyway. --- src/pylorax/ltmpl.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py index 575e553a..b349d18f 100644 --- a/src/pylorax/ltmpl.py +++ b/src/pylorax/ltmpl.py @@ -59,14 +59,17 @@ class LoraxTemplate(object): lines = filter(lambda line: not line.startswith("#"), lines) # mako template now returns unicode strings - lines = map(lambda line: line.encode("ascii"), lines) + lines = map(lambda line: line.encode("utf8"), lines) - # split with shlex - lines = map(shlex.split, lines) + # split with shlex and perform brace expansion + lines = map(split_and_expand, lines) self.lines = lines return lines +def split_and_expand(line): + return [exp for word in shlex.split(line) for exp in brace_expand(word)] + def brace_expand(s): if not ('{' in s and ',' in s and '}' in s): yield s @@ -81,11 +84,10 @@ def brace_expand(s): def rglob(pathname, root="/", fatal=False): seen = set() rootlen = len(root)+1 - for g in brace_expand(pathname): - for f in glob.iglob(joinpaths(root, g)): - if f not in seen: - seen.add(f) - yield f[rootlen:] # remove the root to produce relative path + for f in glob.iglob(joinpaths(root, pathname)): + if f not in seen: + seen.add(f) + yield f[rootlen:] # remove the root to produce relative path if fatal and not seen: raise IOError, "nothing matching %s in %s" % (pathname, root) @@ -252,10 +254,7 @@ class LoraxTemplateRunner(object): self.yum.closeRpmDB() def removefrom(self, pkg, *globs): - globset = set() - for g in globs: - globset.update(brace_expand(g)) - globs_re = re.compile("|".join([fnmatch.translate(g) for g in globset])) + globs_re = re.compile("|".join([fnmatch.translate(g) for g in globs])) remove = filter(globs_re.match, self._filelist(pkg)) logger.debug("removing %i files from %s", len(remove), pkg) self.remove(*remove)