diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-11-22 18:28:12 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-11-29 16:11:31 +0000 |
commit | f5c1a50c9f35450801846a0309aa571e9893946a (patch) | |
tree | 2a5ed1e3c5145ac89893eadbb539ffaefe54899a /morphlib/morphloader.py | |
parent | 6e30db8033160fedbf864db08e98fd18b92a0d08 (diff) | |
download | morph-f5c1a50c9f35450801846a0309aa571e9893946a.tar.gz |
morphloader: Require systems have at least one stratum
It doesn't currently make sense to build a system which contains no
strata. We may later add other fields, such as initramfs to contribute
to the system's artifact, but until then it's another bug to trip over.
This uses collections.Sequence for checking the type of the systems entry
in the morphology as a style choice, though it allows more flexibility
if the types in the parsed morphology change.
Diffstat (limited to 'morphlib/morphloader.py')
-rw-r--r-- | morphlib/morphloader.py | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/morphlib/morphloader.py b/morphlib/morphloader.py index 9ac248da..fdc3b62f 100644 --- a/morphlib/morphloader.py +++ b/morphlib/morphloader.py @@ -16,6 +16,7 @@ # =*= License: GPL-2 =*= +import collections import logging import yaml @@ -102,6 +103,16 @@ class DuplicateChunkError(morphlib.Error): 'in stratum %(stratum_name)s' % locals()) +class SystemStrataNotListError(morphlib.Error): + + def __init__(self, system_name, strata_type): + self.system_name = system_name + self.strata_type = strata_type + typename = strata_type.__name__ + morphlib.Error.__init__( + self, 'System %(system_name)s has the wrong type for its strata: '\ + '%(typename)s, expected list' % locals()) + class DuplicateStratumError(morphlib.Error): def __init__(self, system_name, stratum_name): @@ -112,6 +123,23 @@ class DuplicateStratumError(morphlib.Error): 'in system %(system_name)s' % locals()) +class SystemStratumSpecsNotMappingError(morphlib.Error): + + def __init__(self, system_name, strata): + self.system_name = system_name + self.strata = strata + morphlib.Error.__init__( + self, 'System %(system_name)s has stratum specs '\ + 'that are not mappings.' % locals()) + + +class EmptySystemError(morphlib.Error): + + def __init__(self, system_name): + morphlib.Error.__init__( + self, 'System %(system_name)s has no strata.' % locals()) + + class MorphologyLoader(object): '''Load morphologies from disk, or save them back to disk.''' @@ -126,6 +154,7 @@ class MorphologyLoader(object): 'system': [ 'name', 'arch', + 'strata', ], 'cluster': [ 'name', @@ -166,7 +195,6 @@ class MorphologyLoader(object): 'build-depends': [], }, 'system': { - 'strata': [], 'description': '', 'arch': None, 'configuration-extensions': [], @@ -270,9 +298,23 @@ class MorphologyLoader(object): assert kind == 'cluster' def _validate_system(self, morph): + # A system must contain at least one stratum + strata = morph['strata'] + if (not isinstance(strata, collections.Iterable) + or isinstance(strata, collections.Mapping)): + + raise SystemStrataNotListError(morph['name'], + type(strata)) + + if not strata: + raise EmptySystemError(morph['name']) + + if not all(isinstance(o, collections.Mapping) for o in strata): + raise SystemStratumSpecsNotMappingError(morph['name'], strata) + # All stratum names should be unique within a system. names = set() - for spec in morph['strata']: + for spec in strata: name = spec.get('alias', spec['morph']) if name in names: raise DuplicateStratumError(morph['name'], name) |