diff options
-rw-r--r-- | morphlib/__init__.py | 1 | ||||
-rw-r--r-- | morphlib/builder.py | 17 | ||||
-rw-r--r-- | morphlib/morphologyloader.py | 52 | ||||
-rw-r--r-- | morphlib/morphologyloader_tests.py | 47 |
4 files changed, 105 insertions, 12 deletions
diff --git a/morphlib/__init__.py b/morphlib/__init__.py index 5d8568b4..9787846e 100644 --- a/morphlib/__init__.py +++ b/morphlib/__init__.py @@ -26,6 +26,7 @@ import cachedir import execute import git import morphology +import morphologyloader import stopwatch import tempdir import tester diff --git a/morphlib/builder.py b/morphlib/builder.py index 8d8fcf14..e653ffc4 100644 --- a/morphlib/builder.py +++ b/morphlib/builder.py @@ -1,4 +1,4 @@ -# Copyright (C) 2011 Codethink Limited +# Copyright (C) 2011-2012 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 @@ -476,6 +476,8 @@ class Builder(object): self.dump_memory_profile = app.dump_memory_profile self.cachedir = morphlib.cachedir.CacheDir(self.settings['cachedir']) self.indent = 0 + self.morph_loader = \ + morphlib.morphologyloader.MorphologyLoader(self.settings) def msg(self, text): spaces = ' ' * self.indent @@ -497,7 +499,7 @@ class Builder(object): if not base_url.endswith('/'): base_url += '/' repo = urlparse.urljoin(base_url, repo) - morph = self.get_morph_from_git(repo, ref, filename) + morph = self.morph_loader.load(repo, ref, filename) self.dump_memory_profile('after getting morph from git') if morph.kind == 'chunk': @@ -581,19 +583,10 @@ class Builder(object): morphlib.bins.unpack_binary(chunk_filename, staging_dir, ex) ldconfig(ex, staging_dir) - def get_morph_from_git(self, repo, ref, filename): - morph_text = morphlib.git.get_morph_text(repo, ref, filename) - f = StringIO.StringIO(morph_text) - scheme, netlock, path, params, query, frag = urlparse.urlparse(repo) - f.name = os.path.join(path, filename) - morph = morphlib.morphology.Morphology(f, - self.settings['git-base-url']) - return morph - def get_cache_id(self, repo, ref, morph_filename): logging.debug('get_cache_id(%s, %s, %s)' % (repo, ref, morph_filename)) - morph = self.get_morph_from_git(repo, ref, morph_filename) + morph = self.morph_loader.load(repo, ref, morph_filename) if morph.kind == 'chunk': kids = [] elif morph.kind == 'stratum': diff --git a/morphlib/morphologyloader.py b/morphlib/morphologyloader.py new file mode 100644 index 00000000..0a1f6513 --- /dev/null +++ b/morphlib/morphologyloader.py @@ -0,0 +1,52 @@ +# Copyright (C) 2012 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 +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import os +import StringIO +import urlparse + +import morphlib + + +class MorphologyLoader(object): + + '''Load morphologies from git and parse them into Morphology objects.''' + + def __init__(self, settings): + self.settings = settings + self.morphologies = {} + + def load(self, repo, ref, filename): + key = (repo, ref, filename) + + if key in self.morphologies: + return self.morphologies[key] + else: + morph = self._get_morph_from_git(repo, ref, filename) + self.morphologies[key] = morph + return morph + + def _get_morph_text(self, repo, ref, filename): # pragma: no cover + return morphlib.git.get_morph_text(repo, ref, filename) + + def _get_morph_from_git(self, repo, ref, filename): + morph_text = self._get_morph_text(repo, ref, filename) + scheme, netlock, path, params, query, frag = urlparse.urlparse(repo) + f = StringIO.StringIO(morph_text) + f.name = os.path.join(path, filename) + morph = morphlib.morphology.Morphology(f, + self.settings['git-base-url']) + return morph diff --git a/morphlib/morphologyloader_tests.py b/morphlib/morphologyloader_tests.py new file mode 100644 index 00000000..f9a03915 --- /dev/null +++ b/morphlib/morphologyloader_tests.py @@ -0,0 +1,47 @@ +# Copyright (C) 2012 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 +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import os +import StringIO +import unittest + +import morphlib + + +class MorphologyLoaderTests(unittest.TestCase): + + def test_load_twice_verify_same_morph_object(self): + settings = { 'git-base-url': '' } + loader = morphlib.morphologyloader.MorphologyLoader(settings) + loader._get_morph_text = self.get_morph_text + + morph1 = loader.load('repo', 'ref', 'hello.morph') + morph2 = loader.load('repo', 'ref', 'hello.morph') + self.assertEqual(morph1, morph2) + + def get_morph_text(self, repo, ref, filename): + return (''' + { + "name": "foo", + "kind": "stratum", + "sources": [ + { + "name": "bar", + "repo": "bar", + "ref": "master" + } + ] + }''') |