Refactor: Merge TemplateParser+TemplateRunner into LoraxTemplateRunner
Also move 'glob' and 'exists' into the default args held by the template runner.
This commit is contained in:
parent
9cd0ac09d1
commit
45c8f234ac
@ -106,41 +106,24 @@ def _glob(globpat, root="/", fatal=True):
|
|||||||
def _exists(path, root=""):
|
def _exists(path, root=""):
|
||||||
return (len(_glob(path, root, fatal=False)) > 0)
|
return (len(_glob(path, root, fatal=False)) > 0)
|
||||||
|
|
||||||
class TemplateParser(object):
|
|
||||||
def __init__(self, templatedir=None, defaults={}):
|
|
||||||
self.templatedir = templatedir
|
|
||||||
self.defaults = defaults
|
|
||||||
|
|
||||||
def parse(self, templatefile, variables):
|
|
||||||
for k,v in self.defaults.items():
|
|
||||||
variables.setdefault(k,v)
|
|
||||||
logger.info("parsing %s", templatefile)
|
|
||||||
t = LoraxTemplate(directories=[self.templatedir])
|
|
||||||
return t.parse(templatefile, variables)
|
|
||||||
|
|
||||||
class RuntimeBuilder(object):
|
class RuntimeBuilder(object):
|
||||||
'''Builds the anaconda runtime image.'''
|
'''Builds the anaconda runtime image.'''
|
||||||
def __init__(self, product, arch, yum, templatedir=None):
|
def __init__(self, product, arch, yum, templatedir=None):
|
||||||
root = yum.conf.installroot
|
root = yum.conf.installroot
|
||||||
|
# use a copy of product so we can modify it locally
|
||||||
product = product.copy()
|
product = product.copy()
|
||||||
product.name = product.name.lower()
|
product.name = product.name.lower()
|
||||||
v = DataHolder(arch=arch, product=product, yum=yum, root=root,
|
self.vars = DataHolder(arch=arch, product=product, yum=yum, root=root,
|
||||||
basearch=arch.basearch, libdir=arch.libdir,
|
basearch=arch.basearch, libdir=arch.libdir)
|
||||||
exists = lambda p: _exists(p, root=root),
|
|
||||||
glob = lambda g: _glob(g, root=root, fatal=False))
|
|
||||||
self.vars = v
|
|
||||||
self.yum = yum
|
self.yum = yum
|
||||||
self.templatedir = templatedir
|
self._runner = LoraxTemplateRunner(inroot=root, outroot=root, yum=yum)
|
||||||
|
self._runner.templatedir = templatedir
|
||||||
def runtemplate(self, templatefile, **variables):
|
self._runner.defaults = self.vars
|
||||||
parser = TemplateParser(self.templatedir, self.vars)
|
|
||||||
template = parser.parse(templatefile, variables)
|
|
||||||
runner = TemplateRunner(self.vars.root, self.vars.root, self.vars.yum)
|
|
||||||
runner.run(template)
|
|
||||||
|
|
||||||
def install(self):
|
def install(self):
|
||||||
'''Install packages and do initial setup with runtime-install.tmpl'''
|
'''Install packages and do initial setup with runtime-install.tmpl'''
|
||||||
self.runtemplate("runtime-install.tmpl")
|
self._runner.run("runtime-install.tmpl")
|
||||||
|
|
||||||
def postinstall(self, configdir="/usr/share/lorax/config_files"):
|
def postinstall(self, configdir="/usr/share/lorax/config_files"):
|
||||||
'''Do some post-install setup work with runtime-postinstall.tmpl'''
|
'''Do some post-install setup work with runtime-postinstall.tmpl'''
|
||||||
@ -150,7 +133,7 @@ class RuntimeBuilder(object):
|
|||||||
if os.path.exists(fullpath):
|
if os.path.exists(fullpath):
|
||||||
remove(fullpath)
|
remove(fullpath)
|
||||||
linktree(configdir, fullpath)
|
linktree(configdir, fullpath)
|
||||||
self.runtemplate("runtime-postinstall.tmpl", configdir=configdir_path)
|
self._runner.run("runtime-postinstall.tmpl", configdir=configdir_path)
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
'''Remove unneeded packages and files with runtime-cleanup.tmpl'''
|
'''Remove unneeded packages and files with runtime-cleanup.tmpl'''
|
||||||
@ -160,7 +143,7 @@ class RuntimeBuilder(object):
|
|||||||
locales = set([basename(d) for d in _glob(localedir+"/*") if isdir(d)])
|
locales = set([basename(d) for d in _glob(localedir+"/*") if isdir(d)])
|
||||||
keeplocales = set([line.split()[1] for line in open(langtable)])
|
keeplocales = set([line.split()[1] for line in open(langtable)])
|
||||||
removelocales = locales.difference(keeplocales)
|
removelocales = locales.difference(keeplocales)
|
||||||
self.runtemplate("runtime-cleanup.tmpl", removelocales=removelocales)
|
self._runner.run("runtime-cleanup.tmpl", removelocales=removelocales)
|
||||||
|
|
||||||
def create_runtime(self, outfile="/tmp/squashfs.img"):
|
def create_runtime(self, outfile="/tmp/squashfs.img"):
|
||||||
# make live rootfs image - must be named "LiveOS/rootfs.img" for dracut
|
# make live rootfs image - must be named "LiveOS/rootfs.img" for dracut
|
||||||
@ -179,24 +162,21 @@ class TreeBuilder(object):
|
|||||||
def __init__(self, product, arch, inroot, outroot, runtime, templatedir=None):
|
def __init__(self, product, arch, inroot, outroot, runtime, templatedir=None):
|
||||||
# NOTE: if you pass an arg named "runtime" to a mako template it'll
|
# NOTE: if you pass an arg named "runtime" to a mako template it'll
|
||||||
# clobber some mako internal variables - hence "runtime_img".
|
# clobber some mako internal variables - hence "runtime_img".
|
||||||
v = DataHolder(arch=arch, product=product,
|
self.vars = DataHolder(arch=arch, product=product, runtime_img=runtime,
|
||||||
inroot=inroot, outroot=outroot, runtime_img=runtime,
|
inroot=inroot, outroot=outroot,
|
||||||
basearch=arch.basearch, libdir=arch.libdir,
|
basearch=arch.basearch, libdir=arch.libdir)
|
||||||
exists = lambda p: _exists(p, root=inroot))
|
self._runner = LoraxTemplateRunner(inroot, outroot)
|
||||||
self.vars = v
|
self._runner.templatedir = templatedir
|
||||||
self.templatedir = templatedir
|
self._runner.defaults = self.vars
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def kernels(self):
|
def kernels(self):
|
||||||
return findkernels(root=self.vars.inroot)
|
return findkernels(root=self.vars.inroot)
|
||||||
|
|
||||||
def build(self):
|
def build(self):
|
||||||
parser = TemplateParser(self.templatedir, self.vars)
|
|
||||||
templatefile = templatemap[self.vars.arch.basearch]
|
templatefile = templatemap[self.vars.arch.basearch]
|
||||||
template = parser.parse(templatefile, {'kernels':self.kernels})
|
self._runner.run(templatefile, kernels=self.kernels)
|
||||||
runner = TemplateRunner(self.vars.inroot, self.vars.outroot)
|
self.treeinfo_data = self.runner.results.treeinfo
|
||||||
runner.run(template)
|
|
||||||
self.treeinfo_data = runner.results.treeinfo
|
|
||||||
self.implantisomd5()
|
self.implantisomd5()
|
||||||
|
|
||||||
def generate_module_data(self):
|
def generate_module_data(self):
|
||||||
@ -237,12 +217,18 @@ class TreeBuilder(object):
|
|||||||
# multiple args allowed: mkdir, treeinfo, runcmd, remove, replace
|
# multiple args allowed: mkdir, treeinfo, runcmd, remove, replace
|
||||||
# globs accepted: chmod, install*, remove*, replace
|
# globs accepted: chmod, install*, remove*, replace
|
||||||
|
|
||||||
class TemplateRunner(object):
|
class LoraxTemplateRunner(object):
|
||||||
def __init__(self, inroot, outroot, yum=None, fatalerrors=False):
|
def __init__(self, inroot, outroot, yum=None, fatalerrors=False,
|
||||||
|
templatedir=None, defaults={}):
|
||||||
self.inroot = inroot
|
self.inroot = inroot
|
||||||
self.outroot = outroot
|
self.outroot = outroot
|
||||||
self.yum = yum
|
self.yum = yum
|
||||||
self.fatalerrors = fatalerrors
|
self.fatalerrors = fatalerrors
|
||||||
|
self.templatedir = templatedir
|
||||||
|
# defaults starts with some builtin methods
|
||||||
|
self.defaults = DataHolder(exists=lambda p: _exists(p, root=inroot),
|
||||||
|
glob=lambda g: _glob(g, root=root, fatal=False))
|
||||||
|
self.defaults.update(defaults)
|
||||||
self.results = DataHolder(treeinfo=dict()) # just treeinfo for now
|
self.results = DataHolder(treeinfo=dict()) # just treeinfo for now
|
||||||
|
|
||||||
def _out(self, path):
|
def _out(self, path):
|
||||||
@ -254,7 +240,15 @@ class TemplateRunner(object):
|
|||||||
pkglist = self.yum.doPackageLists(pkgnarrow="installed", patterns=pkgs)
|
pkglist = self.yum.doPackageLists(pkgnarrow="installed", patterns=pkgs)
|
||||||
return set([f for pkg in pkglist.installed for f in pkg.filelist])
|
return set([f for pkg in pkglist.installed for f in pkg.filelist])
|
||||||
|
|
||||||
def run(self, parsed_template):
|
def run(self, templatefile, **variables):
|
||||||
|
for k,v in self.defaults.items():
|
||||||
|
variables.setdefault(k,v)
|
||||||
|
logger.info("parsing %s", templatefile)
|
||||||
|
t = LoraxTemplate(directories=[self.templatedir])
|
||||||
|
commands = t.parse(templatefile, variables)
|
||||||
|
self._run(commands)
|
||||||
|
|
||||||
|
def _run(self, parsed_template):
|
||||||
logger.info("running template commands")
|
logger.info("running template commands")
|
||||||
for (num, line) in enumerate(parsed_template,1):
|
for (num, line) in enumerate(parsed_template,1):
|
||||||
logger.debug("template line %i: %s", num, " ".join(line))
|
logger.debug("template line %i: %s", num, " ".join(line))
|
||||||
|
Loading…
Reference in New Issue
Block a user