diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-08-29 11:25:36 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-08-30 10:34:53 +0000 |
commit | b0096f15bb3624207b1df71b22070b4780b03658 (patch) | |
tree | ffcf474dd043d404b0a9e791c7a8cd69f6fb5e1e /morphlib | |
parent | 4ee24ddb0c89723a3e6c1c15ccdea9c8d454d084 (diff) | |
download | morph-b0096f15bb3624207b1df71b22070b4780b03658.tar.gz |
morphloader: Add method to unset default values
The original contents of the morphology is not generally trackable,
comments are lost for example, and it's simpler to output a canonical
format than attempt to preserve everything of the original.
However, we don't want to clutter the output with fields that have
been filled out to be the defaults, so provide a method to remove
fields that are the same as their default.
This also removes a check in set_defaults that it is a valid kind,
since it explicitly declares it assumes the morphology is valid.
Diffstat (limited to 'morphlib')
-rw-r--r-- | morphlib/morphloader.py | 25 | ||||
-rw-r--r-- | morphlib/morphloader_tests.py | 71 |
2 files changed, 94 insertions, 2 deletions
diff --git a/morphlib/morphloader.py b/morphlib/morphloader.py index 70f46064..c94078f9 100644 --- a/morphlib/morphloader.py +++ b/morphlib/morphloader.py @@ -332,8 +332,22 @@ class MorphologyLoader(object): self._set_stratum_defaults(morphology) elif kind == 'chunk': self._set_chunk_defaults(morphology) - else: - assert kind == 'cluster' + + def unset_defaults(self, morphology): + '''If a field is equal to its default, delete it. + + The morphology is assumed to be valid. + + ''' + + kind = morphology['kind'] + defaults = self._static_defaults[kind] + for key in defaults: + if key in morphology and morphology[key] == defaults[key]: + del morphology[key] + + if kind == 'stratum': + self._unset_stratum_defaults(morphology) def _set_system_defaults(self, morph): pass @@ -345,6 +359,13 @@ class MorphologyLoader(object): if 'morph' not in spec: spec['morph'] = spec['name'] + def _unset_stratum_defaults(self, morph): + for spec in morph['chunks']: + if 'repo' in spec and spec['repo'] == spec['name']: + del spec['repo'] + if 'morph' in spec and spec['morph'] == spec['name']: + del spec['morph'] + def _set_chunk_defaults(self, morph): if morph['max-jobs'] is not None: morph['max-jobs'] = int(morph['max-jobs']) diff --git a/morphlib/morphloader_tests.py b/morphlib/morphloader_tests.py index 6e957510..ac0fef53 100644 --- a/morphlib/morphloader_tests.py +++ b/morphlib/morphloader_tests.py @@ -385,6 +385,20 @@ name: foo 'max-jobs': None, }) + def test_unsets_defaults_for_chunks(self): + m = morphlib.morph3.Morphology({ + 'kind': 'chunk', + 'name': 'foo', + 'build-system': 'manual', + }) + self.loader.unset_defaults(m) + self.assertEqual( + dict(m), + { + 'kind': 'chunk', + 'name': 'foo', + }) + def test_sets_defaults_for_strata(self): m = morphlib.morph3.Morphology({ 'kind': 'stratum', @@ -421,6 +435,27 @@ name: foo ], }) + def test_unsets_defaults_for_strata(self): + test_dict = { + 'kind': 'stratum', + 'name': 'foo', + 'chunks': [ + { + 'name': 'bar', + "ref": "bar", + 'build-mode': 'bootstrap', + 'build-depends': [], + }, + ], + } + test_dict_with_build_depends = dict(test_dict) + test_dict_with_build_depends["build-depends"] = [] + m = morphlib.morph3.Morphology(test_dict_with_build_depends) + self.loader.unset_defaults(m) + self.assertEqual( + dict(m), + test_dict) + def test_sets_defaults_for_system(self): m = morphlib.morph3.Morphology({ 'kind': 'system', @@ -442,6 +477,22 @@ name: foo 'disk-size': '1G', }) + def test_unsets_defaults_for_system(self): + m = morphlib.morph3.Morphology({ + 'kind': 'system', + 'name': 'foo', + 'arch': 'x86_64', + 'strata': [], + }) + self.loader.unset_defaults(m) + self.assertEqual( + dict(m), + { + 'kind': 'system', + 'name': 'foo', + 'arch': 'x86_64', + }) + def test_sets_stratum_chunks_repo_and_morph_from_name(self): m = morphlib.morph3.Morphology( { @@ -461,6 +512,26 @@ name: foo self.assertEqual(m['chunks'][0]['repo'], 'le-chunk') self.assertEqual(m['chunks'][0]['morph'], 'le-chunk') + def test_collapses_stratum_chunks_repo_and_morph_from_name(self): + m = morphlib.morph3.Morphology( + { + "name": "foo", + "kind": "stratum", + "chunks": [ + { + "name": "le-chunk", + "repo": "le-chunk", + "morph": "le-chunk", + "ref": "ref", + "build-depends": [], + } + ] + }) + + self.loader.unset_defaults(m) + self.assertTrue('repo' not in m['chunks'][0]) + self.assertTrue('morph' not in m['chunks'][0]) + def test_convertes_max_jobs_to_an_integer(self): m = morphlib.morph3.Morphology( { |