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
This commit is contained in:
Will Woods 2011-05-10 15:20:38 -04:00
parent 644e5f4b55
commit 17c81d12d7

View File

@ -20,7 +20,8 @@
import logging import logging
logger = logging.getLogger("pylorax.treebuilder") 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 os.path import join, basename, isdir, getsize
from subprocess import check_call, PIPE from subprocess import check_call, PIPE
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
@ -65,7 +66,7 @@ def findkernels(root="/", kdir="boot"):
def _exists(root, p): def _exists(root, p):
if p[0] != '/': p = joinpaths(root, p) if p[0] != '/': p = joinpaths(root, p)
return (len(glob.glob(p)) > 0) return (len(glob(p)) > 0)
class BaseBuilder(object): class BaseBuilder(object):
def __init__(self, product, arch, inroot, outroot, templatedir=None): def __init__(self, product, arch, inroot, outroot, templatedir=None):
@ -79,17 +80,21 @@ class BaseBuilder(object):
def getdefaults(self): def getdefaults(self):
return dict(arch=self.arch, product=self.product, return dict(arch=self.arch, product=self.product,
inroot=self.inroot, outroot=self.outroot, inroot=self.inroot, outroot=self.outroot,
basearch=self.arch.basearch, libdir=self.arch.libdir, basearch=self.arch.basearch, libdir=self.arch.libdir)
exists=lambda p: _exists(self.inroot, p))
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(): for k,v in self.getdefaults().items():
variables.setdefault(k,v) # setdefault won't override existing args tvars.setdefault(k,v) # setdefault won't override existing keys
t = LoraxTemplate(directories=[self.templatedir]) logger.info("parsing %s with the following variables", tfile)
logger.info("parsing %s with the following variables", templatefile) for key, val in tvars.items():
for key, val in variables.items():
logger.info(" %s: %s", key, val) 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) self.runner = TemplateRunner(self.inroot, self.outroot, template)
logger.info("running template commands") logger.info("running template commands")
self.runner.run() self.runner.run()
@ -194,7 +199,7 @@ class TemplateRunner(object):
logger.error(str(e)) logger.error(str(e))
def install(self, srcglob, dest): def install(self, srcglob, dest):
sources = glob.glob(self._in(srcglob)) sources = glob(self._in(srcglob))
if not sources: if not sources:
raise IOError, "couldn't find %s" % srcglob raise IOError, "couldn't find %s" % srcglob
for src in sources: for src in sources:
@ -207,8 +212,9 @@ class TemplateRunner(object):
os.makedirs(d) os.makedirs(d)
def replace(self, pat, repl, *files): def replace(self, pat, repl, *files):
for f in files: for g in globs:
replace(pat, repl, self._out(f)) for f in glob(self._out(f)):
replace(pat, repl, f)
def append(self, filename, data): def append(self, filename, data):
with open(self._out(filename), "a") as fobj: with open(self._out(filename), "a") as fobj: