From 2100445ad17f11297b51f8acd2be3e66eec80342 Mon Sep 17 00:00:00 2001 From: Will Woods Date: Fri, 24 Jun 2011 18:12:59 -0400 Subject: [PATCH] Expand braces (e.g. /usr/share/{doc,man,info}) in globs The braces will be expanded as in the shell, before globbing. Note that the output may include duplicates. --- src/pylorax/treebuilder.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pylorax/treebuilder.py b/src/pylorax/treebuilder.py index 510899c6..09374c82 100644 --- a/src/pylorax/treebuilder.py +++ b/src/pylorax/treebuilder.py @@ -64,8 +64,21 @@ def findkernels(root="/", kdir="boot"): return kernels +def brace_expand(s): + if not ('{' in s and ',' in s and '}' in s): + yield s + else: + right = s.find('}') + left = s[:right].rfind('{') + (prefix, choices, suffix) = (s[:left], s[left+1:right], s[right+1:]) + for choice in choices.split(','): + for alt in brace_expand(prefix+choice+suffix): + yield alt + def _glob(globpat, root="/", fatal=True): - files_found = glob.glob(joinpaths(root, globpat)) + files_found = set() + for g in brace_expand(globpat): + files_found.update(glob.glob(joinpaths(root, g))) if fatal and not files_found: raise IOError, "nothing matching %s" % joinpaths(root, globpat) return [f.replace(root+os.path.sep,"",1) for f in files_found] @@ -331,6 +344,7 @@ class TemplateRunner(object): self.yum.closeRpmDB() def removefrom(self, pkg, *globs): + globs = set(brace_expand(globs)) 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)