summaryrefslogtreecommitdiff
path: root/morphlib
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-04-10 16:12:06 +0100
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-04-10 16:28:13 +0100
commitcaaa37d42dec002ffe20f2152767f3ce37cb1bf5 (patch)
tree9bfb0e630f0688aeb31940212da5c32e738cf74f /morphlib
parent7ec6cd23cefe5e116a54c6c0cb2aa0669adcd991 (diff)
downloadmorph-caaa37d42dec002ffe20f2152767f3ce37cb1bf5.tar.gz
Add SourcePool class and make Source store its morphology filename
Diffstat (limited to 'morphlib')
-rw-r--r--morphlib/__init__.py1
-rw-r--r--morphlib/source.py4
-rw-r--r--morphlib/source_tests.py8
-rw-r--r--morphlib/sourcepool.py51
-rw-r--r--morphlib/sourcepool_tests.py65
5 files changed, 126 insertions, 3 deletions
diff --git a/morphlib/__init__.py b/morphlib/__init__.py
index fbfc3b31..c588e191 100644
--- a/morphlib/__init__.py
+++ b/morphlib/__init__.py
@@ -36,6 +36,7 @@ import morphologyloader
import savefile
import source
import sourcemanager
+import sourcepool
import stopwatch
import tempdir
import util
diff --git a/morphlib/source.py b/morphlib/source.py
index 399d9e0c..33549df4 100644
--- a/morphlib/source.py
+++ b/morphlib/source.py
@@ -26,6 +26,7 @@ class Source(object):
* ``repo`` -- the git repository which contains the source
* ``sha1`` -- the absolute git commit id for the revision we use
* ``morphology`` -- the in-memory representation of the morphology we use
+ * ``filename`` -- basename of the morphology filename
* ``dependencies`` -- list of Sources for build dependencies for us
* ``dependents`` -- list of Source for whom we are a build dependency
@@ -34,11 +35,12 @@ class Source(object):
'''
- def __init__(self, repo, sha1, morphology):
+ def __init__(self, repo, sha1, morphology, filename):
assert type(morphology) == morphlib.morph2.Morphology
self.repo = repo
self.sha1 = sha1
self.morphology = morphology
+ self.filename = filename
self.dependencies = []
self.dependents = []
diff --git a/morphlib/source_tests.py b/morphlib/source_tests.py
index 79ccf408..f9999ca0 100644
--- a/morphlib/source_tests.py
+++ b/morphlib/source_tests.py
@@ -32,10 +32,11 @@ class SourceTests(unittest.TestCase):
self.repo = 'foo.repo'
self.sha1 = 'CAFEF00D'
self.morphology = morphlib.morph2.Morphology(self.morphology_text)
+ self.filename = 'foo.morph'
self.source = morphlib.source.Source(self.repo, self.sha1,
- self.morphology)
+ self.morphology, self.filename)
self.other = morphlib.source.Source(self.repo, self.sha1,
- self.morphology)
+ self.morphology, self.filename)
def test_sets_repo(self):
self.assertEqual(self.source.repo, self.repo)
@@ -46,6 +47,9 @@ class SourceTests(unittest.TestCase):
def test_sets_morphology(self):
self.assertEqual(self.source.morphology, self.morphology)
+ def test_sets_filename(self):
+ self.assertEqual(self.source.filename, self.filename)
+
def test_sets_dependencies_to_empty(self):
self.assertEqual(self.source.dependencies, [])
diff --git a/morphlib/sourcepool.py b/morphlib/sourcepool.py
new file mode 100644
index 00000000..449d7757
--- /dev/null
+++ b/morphlib/sourcepool.py
@@ -0,0 +1,51 @@
+# 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.
+
+
+class SourcePool(object):
+
+ '''Manage a collection of Source objects.'''
+
+ def __init__(self):
+ self._sources = {}
+ self._order = []
+
+ def _key(self, repo, sha1, filename):
+ return (repo, sha1, filename)
+
+ def add(self, source):
+ '''Add a source to the pool.'''
+ key = self._key(source.repo, source.sha1, source.filename)
+ self._sources[key] = source
+ self._order.append(source)
+
+ def lookup(self, repo, sha1, filename):
+ '''Find a source in the pool.
+
+ Raise KeyError if it is not found.
+
+ '''
+
+ key = self._key(repo, sha1, filename)
+ return self._sources[key]
+
+ def __iter__(self):
+ '''Iterate over sources in the pool, in the order they were added.'''
+ for source in self._order:
+ yield source
+
+ def __len__(self):
+ return len(self._sources)
+
diff --git a/morphlib/sourcepool_tests.py b/morphlib/sourcepool_tests.py
new file mode 100644
index 00000000..35a94b39
--- /dev/null
+++ b/morphlib/sourcepool_tests.py
@@ -0,0 +1,65 @@
+# 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 unittest
+
+import morphlib
+
+
+class DummySource(object):
+
+ def __init__(self):
+ self.repo = 'dummy.repo'
+ self.sha1 = 'dummy.sha1'
+ self.filename = 'dummy.morph'
+ self.morphology = {}
+ self.dependencies = []
+ self.dependents = []
+
+
+class SourcePoolTests(unittest.TestCase):
+
+ def setUp(self):
+ self.pool = morphlib.sourcepool.SourcePool()
+ self.source = DummySource()
+
+ def test_is_empty_initially(self):
+ self.assertEqual(list(self.pool), [])
+ self.assertEqual(len(self.pool), 0)
+
+ def test_adds_source(self):
+ self.pool.add(self.source)
+ self.assertEqual(list(self.pool), [self.source])
+
+ def test_looks_up_source(self):
+ self.pool.add(self.source)
+ result = self.pool.lookup(self.source.repo, self.source.sha1,
+ self.source.filename)
+ self.assertEqual(result, self.source)
+
+ def test_lookup_raises_keyerror_if_not_found(self):
+ self.assertRaises(KeyError, self.pool.lookup, self.source.repo,
+ self.source.sha1, self.source.filename)
+
+ def test_iterates_in_add_order(self):
+ sources = []
+ for i in range(10):
+ source = DummySource()
+ source.filename = str(i)
+ self.pool.add(source)
+ sources.append(source)
+ self.assertEqual(list(self.pool), sources)
+