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.
This commit is contained in:
Will Woods 2011-07-06 18:02:20 -04:00
parent d61ae8c1e6
commit f5164d6460

View File

@ -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)