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 +++++++++++ morphlib/morph2_tests.py | 110 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 123 insertions(+), 12 deletions(-) (limited to 'morphlib') 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) diff --git a/morphlib/morph2_tests.py b/morphlib/morph2_tests.py index b7d0a7c8..afa55769 100644 --- a/morphlib/morph2_tests.py +++ b/morphlib/morph2_tests.py @@ -40,17 +40,6 @@ class MorphologyTests(unittest.TestCase): self.assertEqual(m['max-jobs'], None) self.assertEqual(m['chunks'], []) - def test_makes_max_jobs_be_an_integer(self): - m = Morphology(''' - { - "name": "foo", - "kind": "chunk", - "max-jobs": "42" - } - ''') - - self.assertEqual(m['max-jobs'], 42) - def test_sets_stratum_chunks_repo_and_morph_from_name(self): m = Morphology(''' { @@ -58,7 +47,8 @@ class MorphologyTests(unittest.TestCase): "kind": "stratum", "chunks": [ { - "name": "le-chunk" + "name": "le-chunk", + "ref": "ref" } ] } @@ -91,3 +81,99 @@ class MorphologyTests(unittest.TestCase): self.assertTrue('name' in m.keys()) self.assertTrue('kind' in m.keys()) self.assertTrue('disk-size' in m.keys()) + + def test_system_indexes_strata(self): + m = Morphology(''' + { + "kind": "system", + "strata": [ + { + "morph": "stratum1", + "repo": "repo", + "ref": "ref" + }, + { + "alias": "aliased-stratum", + "morph": "stratum2", + "repo": "repo", + "ref": "ref" + } + ] + } + ''') + self.assertEqual(m.lookup_morphology_by_name("stratum1"), + ("repo", "ref", "stratum1.morph")) + self.assertEqual(m.lookup_morphology_by_name("aliased-stratum"), + ("repo", "ref", "stratum2.morph")) + + def test_stratum_indexes_chunks(self): + m = Morphology(''' + { + "kind": "stratum", + "chunks": [ + { + "name": "chunk", + "repo": "repo", + "ref": "ref" + } + ] + } + ''') + self.assertEqual(m.lookup_morphology_by_name("chunk"), + ("repo", "ref", "chunk.morph")) + + ## Validation tests + + def test_makes_max_jobs_be_an_integer(self): + m = Morphology(''' + { + "name": "foo", + "kind": "chunk", + "max-jobs": "42" + } + ''') + self.assertEqual(m['max-jobs'], 42) + + def test_stratum_names_must_be_unique_within_a_system(self): + text = ''' + { + "kind": "system", + "strata": [ + { + "morph": "stratum", + "repo": "test1", + "ref": "ref" + }, + { + "morph": "stratum", + "repo": "test2", + "ref": "ref" + } + ] + } + ''' + self.assertRaises(ValueError, + Morphology, + text) + + def test_chunk_names_must_be_unique_within_a_stratum(self): + text = ''' + { + "kind": "stratum", + "chunks": [ + { + "name": "chunk", + "repo": "test1", + "ref": "ref" + }, + { + "name": "chunk", + "repo": "test2", + "ref": "ref" + } + ] + } + ''' + self.assertRaises(ValueError, + Morphology, + text) -- cgit v1.2.1