comparison piecrust/cache.py @ 371:c2ca72fb7f0b 2.0.0a8

caching: Use separate caches for config variants and other contexts. * The `_cache` directory is now organized in multiple "sub-caches" for different contexts. * A new context is created when config variants or overrides are applied. * `serve` context uses a different context that the other commends, to prevent the `bake` command's output from messing up the preview server (e.g. with how asset URLs are generated differently between the two). * Fix a few places where the cache directory was referenced directly.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 23:59:46 -0700
parents 7d2fdf43d7ca
children ff6cc43fb40c
comparison
equal deleted inserted replaced
370:a1bbe66cba03 371:c2ca72fb7f0b
1 import os 1 import os
2 import os.path 2 import os.path
3 import shutil
3 import codecs 4 import codecs
4 import logging 5 import logging
5 import threading 6 import threading
6 7
7 8
38 def getCacheNames(self, except_names=None): 39 def getCacheNames(self, except_names=None):
39 _, dirnames, __ = next(os.walk(self.base_dir)) 40 _, dirnames, __ = next(os.walk(self.base_dir))
40 if except_names is None: 41 if except_names is None:
41 return dirnames 42 return dirnames
42 return [dn for dn in dirnames if dn not in except_names] 43 return [dn for dn in dirnames if dn not in except_names]
44
45 def clearCache(self, name):
46 cache_dir = self.getCacheDir(name)
47 if os.path.isdir(cache_dir):
48 logger.debug("Cleaning cache: %s" % cache_dir)
49 shutil.rmtree(cache_dir)
50
51 def clearCaches(self, except_names=None):
52 for name in self.getCacheNames(except_names=except_names):
53 self.clearCache(name)
43 54
44 55
45 class SimpleCache(object): 56 class SimpleCache(object):
46 def __init__(self, base_dir): 57 def __init__(self, base_dir):
47 self.base_dir = base_dir 58 self.base_dir = base_dir
120 return False 131 return False
121 132
122 def getCache(self, name): 133 def getCache(self, name):
123 return self.null_cache 134 return self.null_cache
124 135
136 def getCacheDir(self, name):
137 raise NotImplementedError()
138
139 def getCacheNames(self, except_names=None):
140 return []
141
142 def clearCache(self, name):
143 pass
144
145 def clearCaches(self, except_names=None):
146 pass
147