summaryrefslogtreecommitdiff
path: root/morphlib/localartifactcache.py
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-06-07 12:24:11 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-06-07 12:30:39 +0000
commitdef5cd6f2a5c037c05173f023239a4d2b0babee0 (patch)
tree88c8a50c44d3211e3e57b1e12a5f7afc3e548206 /morphlib/localartifactcache.py
parent50d844c340016ebef54f1c0daee5cbeb4692764f (diff)
downloadmorph-def5cd6f2a5c037c05173f023239a4d2b0babee0.tar.gz
LAC: implement contents listing and removal by key
Diffstat (limited to 'morphlib/localartifactcache.py')
-rw-r--r--morphlib/localartifactcache.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/morphlib/localartifactcache.py b/morphlib/localartifactcache.py
index b845cebf..d8abf2ad 100644
--- a/morphlib/localartifactcache.py
+++ b/morphlib/localartifactcache.py
@@ -14,6 +14,7 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+import collections
import os
import morphlib
@@ -115,3 +116,30 @@ class LocalArtifactCache(object):
for basename in basenames:
os.remove(os.path.join(dirname, basename))
+ def list_contents(self): # pragma: no cover
+ '''Return the set of sources cached and related information.
+
+ returns a [(cache_key, set(artifacts), last_used)]
+
+ '''
+ CacheInfo = collections.namedtuple('CacheInfo', ('artifacts', 'mtime'))
+ contents = collections.defaultdict(lambda: CacheInfo(set(), 0))
+ for dirpath, dirnames, filenames in os.walk(self.cachedir):
+ for filename in filenames:
+ cachekey = filename[:63]
+ artifact = filename[65:]
+ artifacts, max_mtime = contents[cachekey]
+ artifacts.add(artifact)
+ this_mtime = os.stat(os.path.join(dirpath, filename)).st_mtime
+ contents[cachekey] = CacheInfo(artifacts,
+ max(max_mtime, this_mtime))
+ return ((cache_key, info.artifacts, info.mtime)
+ for cache_key, info in contents.iteritems())
+
+
+ def remove(self, cachekey): # pragma: no cover
+ '''Remove all artifacts associated with the given cachekey.'''
+ for dirpath, dirnames, filenames in os.walk(self.cachedir):
+ for filename in filenames:
+ if filename.startswith(cachekey):
+ os.remove(os.path.join(dirpath, filename))