From a0ba0157c77bc3230f9de2b58d3a3fb40db0ab76 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Mon, 3 Sep 2012 13:01:27 +0100 Subject: Morph: index component morphologies by name This requires that we enforce uniqueness. New method: Morphology.lookup_morphology_by_name() --- morphlib/morph2.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'morphlib/morph2.py') diff --git a/morphlib/morph2.py b/morphlib/morph2.py index d4d9fe07..ffb05660 100644 --- a/morphlib/morph2.py +++ b/morphlib/morph2.py @@ -44,6 +44,7 @@ class Morphology(object): def __init__(self, text): self._dict = json.loads(text) self._set_defaults() + self._create_child_index() def __getitem__(self, key): return self._dict[key] @@ -54,6 +55,11 @@ class Morphology(object): def keys(self): return self._dict.keys() + def lookup_morphology_by_name(self, name): + '''Find child morphology by its morphology name, honouring aliases + defined within this stratum.''' + return self._child_dict[name] + def _set_defaults(self): if 'max-jobs' in self: self._dict['max-jobs'] = int(self['max-jobs']) @@ -86,3 +92,22 @@ class Morphology(object): source['morph'] = source['name'] if 'build-depends' not in source: source['build-depends'] = None + + def _get_valid_triple(self, info): + return (info['repo'], info['ref'], "%s.morph" % info['morph']) + + def _create_child_index(self): + self._child_dict = {} + if self['kind'] == 'system': + for info in self['strata']: + source_name = info.get('alias', info['morph']) + if source_name in self._child_dict: + raise ValueError("duplicate stratum name: " + source_name) + self._child_dict[source_name] = self._get_valid_triple(info) + elif self['kind'] == 'stratum': + for info in self['chunks']: + # FIXME: in the future, chunks will have an 'alias' field too + source_name = info['name'] + if source_name in self._child_dict: + raise ValueError("duplicate chunk name: " + source_name) + self._child_dict[source_name] = self._get_valid_triple(info) -- cgit v1.2.1