Allow optional files in package declarations.

Files listed in the new 'optional-files' key will be included if present
but will not report packaging errors if missing.
This commit is contained in:
Jeffrey Townsend
2017-02-15 15:58:31 +00:00
parent 395108618c
commit 61f9268995
2 changed files with 24 additions and 15 deletions

View File

@@ -242,12 +242,13 @@ class OnlPackage(object):
return True
def _validate_files(self):
def _validate_files(self, key, required=True):
"""Validate the existence of the required input files for the current package."""
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)
self.pkg[key] = onlu.validate_src_dst_file_tuples(self.dir,
self.pkg[key],
dict(PKG=self.pkg['name'], PKG_INSTALL='/usr/share/onl/packages/%s/%s' % (self.pkg['arch'], self.pkg['name'])),
OnlPackageError,
required=required)
def _validate(self):
"""Validate the package contents."""
@@ -328,7 +329,10 @@ class OnlPackage(object):
# Make sure all required files exist
if 'files' in self.pkg:
self._validate_files()
self._validate_files('files', True)
if 'optional-files' in self.pkg:
self._validate_files('optional-files', False)
# If dir_ is not specified, leave package in local package directory.
if dir_ is None:
@@ -344,6 +348,10 @@ class OnlPackage(object):
for (src,dst) in self.pkg.get('files', {}):
OnlPackage.copyf(src, dst, root)
for (src,dst) in self.pkg.get('optional-files', {}):
if os.path.exists(src):
OnlPackage.copyf(src, dst, root)
for (link,src) in self.pkg.get('links', {}).iteritems():
logger.info("Linking %s -> %s..." % (link, src))
link = os.path.join(root, link)

View File

@@ -159,7 +159,7 @@ class Lock(object):
self.handle.close()
def filepath(absdir, relpath, eklass):
def filepath(absdir, relpath, eklass, required=True):
"""Return the absolute path for the given absdir/repath file."""
p = None
if os.path.isabs(relpath):
@@ -170,7 +170,8 @@ def filepath(absdir, relpath, eklass):
# 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)
if required:
raise eklass("'%s' did not match any files." % p)
elif len(g) > 1:
raise eklass("'%s' matched too many files %s" % (p, g))
else:
@@ -178,7 +179,7 @@ def filepath(absdir, relpath, eklass):
return p
def validate_src_dst_file_tuples(absdir, data, dstsubs, eklass):
def validate_src_dst_file_tuples(absdir, data, dstsubs, eklass, required=True):
files = []
if type(data) is dict:
for (s,d) in data.iteritems():
@@ -202,11 +203,11 @@ def validate_src_dst_file_tuples(absdir, data, dstsubs, eklass):
#
flist = []
for f in files:
src = filepath(absdir, f[0], eklass)
if not os.path.exists(src):
src = filepath(absdir, f[0], eklass, required)
if os.path.exists(src):
t = Template(f[1])
dst = t.substitute(dstsubs)
flist.append((src, dst))
elif required:
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