From 552420219dd9275c15cf25b31e0b8355b4a72f6e Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Fri, 8 Aug 2014 07:59:02 +0000 Subject: morph3: Add a get_commands method to Morphology morph2.Morphology has a `get_commands` method which is used to return the [configure|build|install]-commands of the chunk, either by getting them from the chunk morphology or from the build system of the chunk. This commit implements a similar method in morph3.Morphology and adds tests for it, so that when we remove morph2 the API changes to morphologies are minimal. --- morphlib/morph3.py | 12 +++++++++++- morphlib/morph3_tests.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/morphlib/morph3.py b/morphlib/morph3.py index 477cac1a..a91ff390 100644 --- a/morphlib/morph3.py +++ b/morphlib/morph3.py @@ -1,4 +1,4 @@ -# Copyright (C) 2013 Codethink Limited +# Copyright (C) 2013-2014 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,6 +18,8 @@ import UserDict +import morphlib + class Morphology(UserDict.IterableUserDict): @@ -43,3 +45,11 @@ class Morphology(UserDict.IterableUserDict): self.filename = None self.dirty = None + def get_commands(self, which): + '''Return the commands to run from a morphology or the build system''' + if self.get(which, None) is None: + attr = '_'.join(which.split('-')) + bs = morphlib.buildsystem.lookup_build_system(self['build-system']) + return getattr(bs, attr) + else: + return self[which] diff --git a/morphlib/morph3_tests.py b/morphlib/morph3_tests.py index e150bf33..51b70597 100644 --- a/morphlib/morph3_tests.py +++ b/morphlib/morph3_tests.py @@ -1,4 +1,4 @@ -# Copyright (C) 2013 Codethink Limited +# Copyright (C) 2013-2014 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -46,3 +46,37 @@ class MorphologyTests(unittest.TestCase): self.morph.dirty = True self.assertEqual(self.morph.dirty, True) + def test_uses_morphology_commands_when_given(self): + m = morphlib.morph3.Morphology( + { + 'name': 'foo', + 'kind': 'chunk', + 'build-system': 'dummy', + 'build-commands': ['build-it'] + } + ) + cmds = m.get_commands('build-commands') + self.assertEqual(cmds, ['build-it']) + + def test_uses_build_system_commands_when_morphology_doesnt(self): + m = morphlib.morph3.Morphology( + { + 'name': 'foo', + 'kind': 'chunk', + 'build-system': 'dummy', + } + ) + cmds = m.get_commands('build-commands') + self.assertEqual(cmds, ['echo dummy build']) + + def test_uses_morphology_commands_when_morphology_has_empty_list(self): + m = morphlib.morph3.Morphology( + { + 'name': 'foo', + 'kind': 'chunk', + 'build-system': 'dummy', + 'build-commands': [] + } + ) + cmds = m.get_commands('build-commands') + self.assertEqual(cmds, []) -- cgit v1.2.1