summaryrefslogtreecommitdiff
path: root/morphlib
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-09-29 14:40:47 +0100
committerLars Wirzenius <liw@liw.fi>2011-09-29 14:40:47 +0100
commitca50eed4c902a3454cf7210eeeb818f28d6e7e2a (patch)
treee47b8b237e1aaa3dee236633d279b8bff93ba439 /morphlib
parente8f1882ce8236081ba7a6cc716cff0754e8a438a (diff)
downloadmorph-ca50eed4c902a3454cf7210eeeb818f28d6e7e2a.tar.gz
Add generation of filenames in the cache directory.
Diffstat (limited to 'morphlib')
-rw-r--r--morphlib/cachedir.py22
-rw-r--r--morphlib/cachedir_tests.py31
2 files changed, 53 insertions, 0 deletions
diff --git a/morphlib/cachedir.py b/morphlib/cachedir.py
index 88008547..7ee77222 100644
--- a/morphlib/cachedir.py
+++ b/morphlib/cachedir.py
@@ -15,6 +15,7 @@
import hashlib
+import os
class CacheDir(object):
@@ -40,3 +41,24 @@ class CacheDir(object):
data = ''.join(key + value for key, value in dict_key.iteritems())
return hashlib.sha256(data).hexdigest()
+ def name(self, dict_key):
+ '''Return a filename for an object described by dictionary key.
+
+ It is the caller's responsibility to set the fields in the
+ dictionary key suitably. For example, if there is a field
+ specifying a commit id, it should be the full git SHA-1
+ identifier, not something ephemeral like HEAD.
+
+ If the field 'kind' has a value, it is used as a suffix for
+ the filename.
+
+ '''
+
+ key = self.key(dict_key)
+ if 'kind' in dict_key and dict_key['kind']:
+ suffix = '.%s' % dict_key['kind']
+ else:
+ suffix = ''
+
+ return os.path.join(self.dirname, key + suffix)
+
diff --git a/morphlib/cachedir_tests.py b/morphlib/cachedir_tests.py
index 17fa6e0f..b9f7c80b 100644
--- a/morphlib/cachedir_tests.py
+++ b/morphlib/cachedir_tests.py
@@ -56,3 +56,34 @@ class CacheDirTests(unittest.TestCase):
self.assertNotEqual(self.cachedir.key(dict_key_1),
self.cachedir.key(dict_key_2))
+ def test_returns_a_chunk_pathname_in_cache_directory(self):
+ dict_key = {
+ 'kind': 'chunk',
+ 'ref': 'DEADBEEF',
+ 'repo': 'git://git.baserock.org/hello/',
+ 'arch': 'armel',
+ }
+ pathname = self.cachedir.name(dict_key)
+ self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
+ self.assert_(pathname.endswith('.chunk'))
+
+ def test_returns_a_stratum_pathname_in_cache_directory(self):
+ dict_key = {
+ 'kind': 'stratum',
+ 'ref': 'DEADBEEF',
+ 'repo': 'git://git.baserock.org/hello/',
+ 'arch': 'armel',
+ }
+ pathname = self.cachedir.name(dict_key)
+ self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
+ self.assert_(pathname.endswith('.stratum'))
+
+ def test_returns_a_valid_pathname_in_cache_directory(self):
+ dict_key = {
+ 'ref': 'DEADBEEF',
+ 'repo': 'git://git.baserock.org/hello/',
+ 'arch': 'armel',
+ }
+ pathname = self.cachedir.name(dict_key)
+ self.assert_(pathname.startswith(self.cachedir.dirname + '/'))
+