diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-09-03 13:01:27 +0100 |
---|---|---|
committer | Jannis Pohlmann <jannis.pohlmann@codethink.co.uk> | 2012-09-04 11:00:28 +0000 |
commit | a0ba0157c77bc3230f9de2b58d3a3fb40db0ab76 (patch) | |
tree | ad134852437fa82cc79a8db400e6f0143121a8d3 /morphlib/morph2.py | |
parent | 606530b06b9ad41687da2e939ff3b8cf2fda0fa9 (diff) | |
download | morph-a0ba0157c77bc3230f9de2b58d3a3fb40db0ab76.tar.gz |
Morph: index component morphologies by name
This requires that we enforce uniqueness.
New method: Morphology.lookup_morphology_by_name()
Diffstat (limited to 'morphlib/morph2.py')
-rw-r--r-- | morphlib/morph2.py | 25 |
1 files changed, 25 insertions, 0 deletions
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) |