From 17c81d12d706eb29218a746dfb4bb96087b639d9 Mon Sep 17 00:00:00 2001 From: Will Woods Date: Tue, 10 May 2011 15:20:38 -0400 Subject: [PATCH] treebuilder fixups: fix replace, glob() for templates, etc. "from glob import glob" instead of "import glob" rename args to BaseBuilder.runtemplate set up 'exists' and 'glob' in runtemplate rather than getdefaults --- src/pylorax/treebuilder.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/pylorax/treebuilder.py b/src/pylorax/treebuilder.py index bcdd5208..ed71d70c 100644 --- a/src/pylorax/treebuilder.py +++ b/src/pylorax/treebuilder.py @@ -20,7 +20,8 @@ import logging logger = logging.getLogger("pylorax.treebuilder") -import os, re, glob +import os, re +from glob import glob from os.path import join, basename, isdir, getsize from subprocess import check_call, PIPE from tempfile import NamedTemporaryFile @@ -65,7 +66,7 @@ def findkernels(root="/", kdir="boot"): def _exists(root, p): if p[0] != '/': p = joinpaths(root, p) - return (len(glob.glob(p)) > 0) + return (len(glob(p)) > 0) class BaseBuilder(object): def __init__(self, product, arch, inroot, outroot, templatedir=None): @@ -79,17 +80,21 @@ class BaseBuilder(object): def getdefaults(self): return dict(arch=self.arch, product=self.product, inroot=self.inroot, outroot=self.outroot, - basearch=self.arch.basearch, libdir=self.arch.libdir, - exists=lambda p: _exists(self.inroot, p)) + basearch=self.arch.basearch, libdir=self.arch.libdir) - def runtemplate(self, templatefile, **variables): + def runtemplate(self, tfile, **tvars): + # get data for template - start with defaults and override from args for k,v in self.getdefaults().items(): - variables.setdefault(k,v) # setdefault won't override existing args - t = LoraxTemplate(directories=[self.templatedir]) - logger.info("parsing %s with the following variables", templatefile) - for key, val in variables.items(): + tvars.setdefault(k,v) # setdefault won't override existing keys + logger.info("parsing %s with the following variables", tfile) + for key, val in tvars.items(): logger.info(" %s: %s", key, val) - template = t.parse(templatefile, variables) + # set up functions for template + tvars.setdefault('exists', lambda p: _exists(tvars['inroot'], p)) + tvars.setdefault('glob', glob) + # parse and run the template + t = LoraxTemplate(directories=[self.templatedir]) + template = t.parse(tfile, tvars) self.runner = TemplateRunner(self.inroot, self.outroot, template) logger.info("running template commands") self.runner.run() @@ -194,7 +199,7 @@ class TemplateRunner(object): logger.error(str(e)) def install(self, srcglob, dest): - sources = glob.glob(self._in(srcglob)) + sources = glob(self._in(srcglob)) if not sources: raise IOError, "couldn't find %s" % srcglob for src in sources: @@ -207,8 +212,9 @@ class TemplateRunner(object): os.makedirs(d) def replace(self, pat, repl, *files): - for f in files: - replace(pat, repl, self._out(f)) + for g in globs: + for f in glob(self._out(f)): + replace(pat, repl, f) def append(self, filename, data): with open(self._out(filename), "a") as fobj: