treebuilder fixups: add _glob, use it in _exists, replace, install
This commit is contained in:
parent
d2203b348d
commit
d4813816b7
@ -20,8 +20,7 @@
|
|||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger("pylorax.treebuilder")
|
logger = logging.getLogger("pylorax.treebuilder")
|
||||||
|
|
||||||
import os, re
|
import os, re, glob
|
||||||
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
|
||||||
@ -64,9 +63,14 @@ def findkernels(root="/", kdir="boot"):
|
|||||||
|
|
||||||
return kernels
|
return kernels
|
||||||
|
|
||||||
def _exists(root, p):
|
def _glob(glob, root="", fatal=True):
|
||||||
if p[0] != '/': p = joinpaths(root, p)
|
files_found = glob.glob(os.path.join(root, glob))
|
||||||
return (len(glob(p)) > 0)
|
if fatal and not files_found:
|
||||||
|
raise IOError, "nothing matching %s" % os.path.join(root, glob)
|
||||||
|
return files_found
|
||||||
|
|
||||||
|
def _exists(path, root=""):
|
||||||
|
return (len(_glob(path, root, fatal=False)) > 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):
|
||||||
@ -90,8 +94,8 @@ class BaseBuilder(object):
|
|||||||
for key, val in tvars.items():
|
for key, val in tvars.items():
|
||||||
logger.info(" %s: %s", key, val)
|
logger.info(" %s: %s", key, val)
|
||||||
# set up functions for template
|
# set up functions for template
|
||||||
tvars.setdefault('exists', lambda p: _exists(tvars['inroot'], p))
|
tvars.setdefault('exists', lambda p: _exists(p, root=tvars['inroot']))
|
||||||
tvars.setdefault('glob', glob)
|
tvars.setdefault('glob', lambda g: glob(g, root=tvars['inroot']))
|
||||||
# parse and run the template
|
# parse and run the template
|
||||||
t = LoraxTemplate(directories=[self.templatedir])
|
t = LoraxTemplate(directories=[self.templatedir])
|
||||||
template = t.parse(tfile, tvars)
|
template = t.parse(tfile, tvars)
|
||||||
@ -155,8 +159,8 @@ class TreeBuilder(BaseBuilder):
|
|||||||
# everything else operates on outroot
|
# everything else operates on outroot
|
||||||
# "mkdir", "treeinfo", "runcmd", "remove", "replace" will take multiple args
|
# "mkdir", "treeinfo", "runcmd", "remove", "replace" will take multiple args
|
||||||
|
|
||||||
# TODO: replace installtree:
|
# TODO: to replace installtree:
|
||||||
# glob(), find(glob)
|
# find(glob)
|
||||||
# installpkg/removepkg pkgglob [pkgglob..]
|
# installpkg/removepkg pkgglob [pkgglob..]
|
||||||
# run_pkg_transaction
|
# run_pkg_transaction
|
||||||
# removefrom [pkgname] glob [glob..]
|
# removefrom [pkgname] glob [glob..]
|
||||||
@ -175,7 +179,7 @@ class TemplateRunner(object):
|
|||||||
self.fatalerrors = fatalerrors
|
self.fatalerrors = fatalerrors
|
||||||
|
|
||||||
self.treeinfo_data = dict()
|
self.treeinfo_data = dict()
|
||||||
self.exists = lambda p: _exists(inroot, p)
|
self.exists = lambda p: _exists(p, root=inroot)
|
||||||
|
|
||||||
def _out(self, path):
|
def _out(self, path):
|
||||||
return joinpaths(self.outroot, path)
|
return joinpaths(self.outroot, path)
|
||||||
@ -199,10 +203,7 @@ class TemplateRunner(object):
|
|||||||
logger.error(str(e))
|
logger.error(str(e))
|
||||||
|
|
||||||
def install(self, srcglob, dest):
|
def install(self, srcglob, dest):
|
||||||
sources = glob(self._in(srcglob))
|
for src in _glob(srcglob, root=self.inroot):
|
||||||
if not sources:
|
|
||||||
raise IOError, "couldn't find %s" % srcglob
|
|
||||||
for src in sources:
|
|
||||||
cpfile(src, self._out(dest))
|
cpfile(src, self._out(dest))
|
||||||
|
|
||||||
def mkdir(self, *dirs):
|
def mkdir(self, *dirs):
|
||||||
@ -213,7 +214,7 @@ class TemplateRunner(object):
|
|||||||
|
|
||||||
def replace(self, pat, repl, *fileglobs):
|
def replace(self, pat, repl, *fileglobs):
|
||||||
for g in fileglobs:
|
for g in fileglobs:
|
||||||
for f in glob(self._out(f)):
|
for f in _glob(g, root=self.outroot):
|
||||||
replace(pat, repl, f)
|
replace(pat, repl, f)
|
||||||
|
|
||||||
def append(self, filename, data):
|
def append(self, filename, data):
|
||||||
|
Loading…
Reference in New Issue
Block a user