From 39a0ad8d31d6a39bfa607187b1d76478ccbe2513 Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Fri, 8 Aug 2014 10:20:25 +0000 Subject: buildsystem: Generate a Morphology not text Rather than generating the text of a morphology which is later loaded, generate a Morphology object and return that. --- morphlib/buildsystem.py | 19 ++++++++----------- morphlib/buildsystem_tests.py | 8 ++++---- morphlib/morphologyfactory.py | 24 +++++++++++++----------- morphlib/morphologyfactory_tests.py | 8 ++++---- 4 files changed, 29 insertions(+), 30 deletions(-) (limited to 'morphlib') diff --git a/morphlib/buildsystem.py b/morphlib/buildsystem.py index 90cc15c2..fb99e70e 100644 --- a/morphlib/buildsystem.py +++ b/morphlib/buildsystem.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2013 Codethink Limited +# Copyright (C) 2012-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 @@ -16,6 +16,8 @@ import os +import morphlib + class BuildSystem(object): @@ -49,19 +51,14 @@ class BuildSystem(object): key = '_'.join(key.split('-')) return getattr(self, key) - def get_morphology_text(self, name): + def get_morphology(self, name): '''Return the text of an autodetected chunk morphology.''' - return ''' - { - "name": "%(name)s", - "kind": "chunk", - "build-system": "%(bs)s" - } - ''' % { + return morphlib.morphology.Morphology({ 'name': name, - 'bs': self.name, - } + 'kind': 'chunk', + 'build-system': self.name, + }) def used_by_project(self, file_list): '''Does a project use this build system? diff --git a/morphlib/buildsystem_tests.py b/morphlib/buildsystem_tests.py index 3171366b..56ba64d7 100644 --- a/morphlib/buildsystem_tests.py +++ b/morphlib/buildsystem_tests.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2013 Codethink Limited +# Copyright (C) 2012-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 @@ -49,10 +49,10 @@ class BuildSystemTests(unittest.TestCase): def test_has_install_commands(self): self.assertEqual(self.bs['install-commands'], []) - def test_returns_morphology_text(self): + def test_returns_morphology(self): self.bs.name = 'fake' - text = self.bs.get_morphology_text('foobar') - self.assertTrue(type(text) in (str, unicode)) + morph = self.bs.get_morphology('foobar') + self.assertTrue(morph.__class__.__name__ == 'Morphology') class ManualBuildSystemTests(unittest.TestCase): diff --git a/morphlib/morphologyfactory.py b/morphlib/morphologyfactory.py index fd78c94f..1a8e374e 100644 --- a/morphlib/morphologyfactory.py +++ b/morphlib/morphologyfactory.py @@ -50,16 +50,17 @@ class MorphologyFactory(object): if self._app is not None: self._app.status(*args, **kwargs) - def _get_morphology_text(self, reponame, sha1, filename): + def _load_morphology(self, reponame, sha1, filename): morph_name = os.path.splitext(os.path.basename(filename))[0] + loader = morphlib.morphloader.MorphologyLoader() if self._lrc.has_repo(reponame): self.status(msg="Looking for %s in local repo cache" % filename, chatty=True) try: repo = self._lrc.get_repo(reponame) - text = repo.cat(sha1, filename) + morph = loader.load_from_string(repo.cat(sha1, filename)) except IOError: - text = None + morph = None file_list = repo.ls_tree(sha1) elif self._rrc is not None: self.status(msg="Retrieving %(reponame)s %(sha1)s %(filename)s" @@ -68,27 +69,28 @@ class MorphologyFactory(object): chatty=True) try: text = self._rrc.cat_file(reponame, sha1, filename) + morph = loader.load_from_string(text) except morphlib.remoterepocache.CatFileError: - text = None + morph = None file_list = self._rrc.ls_tree(reponame, sha1) else: raise NotcachedError(reponame) - if text is None: + if morph is None: self.status(msg="File %s doesn't exist: attempting to infer " "chunk morph from repo's build system" % filename, chatty=True) bs = morphlib.buildsystem.detect_build_system(file_list) if bs is None: raise MorphologyNotFoundError(filename) - text = bs.get_morphology_text(morph_name) - return morph_name, text + morph = bs.get_morphology(morph_name) + loader.validate(morph) + loader.set_commands(morph) + loader.set_defaults(morph) + return morph def get_morphology(self, reponame, sha1, filename): - morph_name, text = self._get_morphology_text(reponame, sha1, filename) - - loader = morphlib.morphloader.MorphologyLoader() - morphology = loader.load_from_string(text) + morphology = self._load_morphology(reponame, sha1, filename) method_name = '_check_and_tweak_%s' % morphology['kind'] if hasattr(self, method_name): diff --git a/morphlib/morphologyfactory_tests.py b/morphlib/morphologyfactory_tests.py index 347d93f7..0b3253da 100644 --- a/morphlib/morphologyfactory_tests.py +++ b/morphlib/morphologyfactory_tests.py @@ -30,7 +30,7 @@ class FakeRemoteRepoCache(object): return '''{ "name": "%s", "kind": "chunk", - "build-system": "bar" + "build-system": "dummy" }''' % filename[:-len('.morph')] return 'text' @@ -43,12 +43,12 @@ class FakeLocalRepo(object): 'chunk.morph': ''' name: chunk kind: chunk - build-system: bar + build-system: dummy ''', 'chunk-split.morph': ''' name: chunk-split kind: chunk - build-system: bar + build-system: dummy products: - artifact: chunk-split-runtime include: [] @@ -125,7 +125,7 @@ class FakeLocalRepo(object): return '''{ "name": "%s", "kind": "chunk", - "build-system": "bar" + "build-system": "dummy" }''' % filename[:-len('.morph')] return 'text' -- cgit v1.2.1