- Move src/dst file processing into common onl util

- Support direct release of file products during package builds
- Include suite codename in repo and delivery prefixes in anticipation of multisuite build support.
This commit is contained in:
Jeffrey Townsend
2015-12-01 05:10:20 +00:00
parent 04df114aff
commit 9ac09ed790
2 changed files with 75 additions and 63 deletions

View File

@@ -20,6 +20,9 @@ import onlu
from string import Template
import re
import json
import lsb_release
g_dist_codename = lsb_release.get_distro_information().get('CODENAME')
logger = onlu.init_logging('onlpm')
@@ -201,68 +204,12 @@ class OnlPackage(object):
return True
def filepath(self, filename):
"""Return the file path for a given package file."""
p = None
if os.path.isabs(filename):
p = filename
else:
p = os.path.join(self.dir, filename)
# Globs that result in a single file are allowed:
g = glob.glob(p)
if len(g) is 0:
raise OnlPackageError("'%s' did not match any files." % p)
elif len(g) > 1:
raise OnlPackageError("'%s' matched too many files %s" % (p, g))
else:
p = g[0]
logger.debug("package file: %s" % p)
return p
def _validate_files(self):
"""Validate the existence of the required input files for the current package."""
files = []
if type(self.pkg['files']) is dict:
for (s,d) in self.pkg['files'].iteritems():
files.append((s,d))
elif type(self.pkg['files']) is list:
for e in self.pkg['files']:
if type(e) is dict:
for (s,d) in e.iteritems():
files.append((s,d))
elif type(e) in [ list, tuple ]:
if len(e) != 2:
raise OnlPackageError("Too many filenames: '%s'" % (e))
else:
files.append((e[0], e[1]))
else:
raise OnlPackageError("File entry '%s' is invalid." % (e))
#
# Make sure all source files exist.
#
for f in files:
fp = self.filepath(f[0])
if not os.path.exists(fp):
raise OnlPackageError("Source file or directory '%s' does not exist." % fp)
#
# Perform destination-file substitutions.
#
flist = []
for f in files:
s = Template(f[1])
dst = s.substitute(dict(PKG=self.pkg['name'], PKG_INSTALL='/usr/share/onl/packages/%s/%s' % (self.pkg['arch'], self.pkg['name'])))
flist.append((f[0], dst))
self.pkg['files'] = flist
self.pkg['files'] = onlu.validate_src_dst_file_tuples(self.dir,
self.pkg['files'],
dict(PKG=self.pkg['name'], PKG_INSTALL='/usr/share/onl/packages/%s/%s' % (self.pkg['arch'], self.pkg['name'])),
OnlPackageError)
def _validate(self):
"""Validate the package contents."""
@@ -331,8 +278,6 @@ class OnlPackage(object):
if dst.startswith('/'):
dst = dst[1:]
src = self.filepath(src)
if os.path.isdir(src):
#
# Copy entire src directory to target directory
@@ -583,6 +528,19 @@ class OnlPackageGroup(object):
products.append(p.build(dir_=dir_))
if 'release' in self._pkgs:
release_list = onlu.validate_src_dst_file_tuples(self._pkgs['__directory'],
self._pkgs['release'],
dict(),
OnlPackageError)
for f in release_list:
# Todo -- customize
dst = os.path.join(os.getenv('ONL'), 'RELEASE', g_dist_codename, f[1])
if not os.path.exists(dst):
os.makedirs(dst)
logger.info("Releasing %s -> %s" % (os.path.basename(f[0]), dst))
shutil.copy(f[0], dst)
return products
def clean(self, dir_=None):
@@ -607,7 +565,7 @@ class OnlPackageRepo(object):
root : The root directory that should be used for this repository."""
self.root = root
root = os.path.join(root, g_dist_codename)
if not os.path.exists(root):
os.makedirs(root)

View File

@@ -10,6 +10,8 @@ from collections import Iterable
import sys
import os
import fcntl
import glob
from string import Template
logger = None
@@ -156,3 +158,55 @@ class Lock(object):
def __del__(self):
self.handle.close()
def filepath(absdir, relpath, eklass):
"""Return the absolute path for the given absdir/repath file."""
p = None
if os.path.isabs(relpath):
p = relpath
else:
p = os.path.join(absdir, relpath)
# Globs that result in a single file are allowed:
g = glob.glob(p)
if len(g) is 0:
raise eklass("'%s' did not match any files." % p)
elif len(g) > 1:
raise eklass("'%s' matched too many files %s" % (p, g))
else:
p = g[0]
return p
def validate_src_dst_file_tuples(absdir, data, dstsubs, eklass):
files = []
if type(data) is dict:
for (s,d) in data.iteritems():
files.append((s,d))
elif type(data) is list:
for e in data:
if type(e) is dict:
for (s,d) in e.iteritems():
files.append((s,d))
elif type(e) in [ list, tuple ]:
if len(e) != 2:
raise eklass("Too many filenames: '%s'" % (e))
else:
files.append((e[0], e[1]))
else:
raise eklass("File entry '%s' is invalid." % (e))
#
# Validate/process source files.
# Process dst paths.
#
flist = []
for f in files:
src = filepath(absdir, f[0], eklass)
if not os.path.exists(src):
raise eklass("Source file or directory '%s' does not exist." % src)
t = Template(f[1])
dst = t.substitute(dstsubs)
flist.append((src, dst))
return flist