Port cache plugins to global display

This commit is contained in:
Toshio Kuratomi 2015-11-11 08:38:49 -08:00
parent b05d0b8c9c
commit 2bd695ed42
2 changed files with 20 additions and 11 deletions

View file

@ -20,7 +20,6 @@ __metaclass__ = type
from collections import MutableMapping from collections import MutableMapping
from ansible import constants as C from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.plugins import cache_loader from ansible.plugins import cache_loader
try: try:
@ -29,14 +28,16 @@ except ImportError:
from ansible.utils.display import Display from ansible.utils.display import Display
display = Display() display = Display()
class FactCache(MutableMapping): class FactCache(MutableMapping):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._plugin = cache_loader.get(C.CACHE_PLUGIN) self._plugin = cache_loader.get(C.CACHE_PLUGIN)
# Backwards compat: self._display isn't really needed, just import the global display and use that.
self._display = display self._display = display
if self._plugin is None: if self._plugin is None:
self._display.warning("Failed to load fact cache plugins") display.warning("Failed to load fact cache plugins")
return return
def __getitem__(self, key): def __getitem__(self, key):

View file

@ -35,6 +35,13 @@ from ansible.parsing.utils.jsonify import jsonify
from ansible.plugins.cache.base import BaseCacheModule from ansible.plugins.cache.base import BaseCacheModule
from ansible.utils.unicode import to_bytes from ansible.utils.unicode import to_bytes
try:
from __main__ import display
display = display
except ImportError:
from ansible.utils.display import Display
display = Display()
class CacheModule(BaseCacheModule): class CacheModule(BaseCacheModule):
""" """
@ -52,7 +59,7 @@ class CacheModule(BaseCacheModule):
try: try:
os.makedirs(self._cache_dir) os.makedirs(self._cache_dir)
except (OSError,IOError) as e: except (OSError,IOError) as e:
self._display.warning("error while trying to create cache dir %s : %s" % (self._cache_dir, to_bytes(e))) display.warning("error while trying to create cache dir %s : %s" % (self._cache_dir, to_bytes(e)))
return None return None
def get(self, key): def get(self, key):
@ -71,11 +78,12 @@ class CacheModule(BaseCacheModule):
self._cache[key] = value self._cache[key] = value
return value return value
except ValueError as e: except ValueError as e:
self._display.warning("error while trying to read %s : %s. Most likely a corrupt file, so erasing and failing." % (cachefile, to_bytes(e))) display.warning("error while trying to read %s : %s. Most likely a corrupt file, so erasing and failing." % (cachefile, to_bytes(e)))
self.delete(key) self.delete(key)
raise AnsibleError("The JSON cache file %s was corrupt, or did not otherwise contain valid JSON data. It has been removed, so you can re-run your command now." % cachefile) raise AnsibleError("The JSON cache file %s was corrupt, or did not otherwise contain valid JSON data."
" It has been removed, so you can re-run your command now." % cachefile)
except (OSError,IOError) as e: except (OSError,IOError) as e:
self._display.warning("error while trying to read %s : %s" % (cachefile, to_bytes(e))) display.warning("error while trying to read %s : %s" % (cachefile, to_bytes(e)))
raise KeyError raise KeyError
def set(self, key, value): def set(self, key, value):
@ -86,7 +94,7 @@ class CacheModule(BaseCacheModule):
try: try:
f = codecs.open(cachefile, 'w', encoding='utf-8') f = codecs.open(cachefile, 'w', encoding='utf-8')
except (OSError,IOError) as e: except (OSError,IOError) as e:
self._display.warning("error while trying to write to %s : %s" % (cachefile, to_bytes(e))) display.warning("error while trying to write to %s : %s" % (cachefile, to_bytes(e)))
pass pass
else: else:
f.write(jsonify(value)) f.write(jsonify(value))
@ -102,7 +110,7 @@ class CacheModule(BaseCacheModule):
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
return False return False
else: else:
self._display.warning("error while trying to stat %s : %s" % (cachefile, to_bytes(e))) display.warning("error while trying to stat %s : %s" % (cachefile, to_bytes(e)))
pass pass
if time.time() - st.st_mtime <= self._timeout: if time.time() - st.st_mtime <= self._timeout:
@ -128,13 +136,13 @@ class CacheModule(BaseCacheModule):
if self.has_expired(key): if self.has_expired(key):
return False return False
try: try:
st = os.stat(cachefile) os.stat(cachefile)
return True return True
except (OSError,IOError) as e: except (OSError,IOError) as e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
return False return False
else: else:
self._display.warning("error while trying to stat %s : %s" % (cachefile, to_bytes(e))) display.warning("error while trying to stat %s : %s" % (cachefile, to_bytes(e)))
pass pass
def delete(self, key): def delete(self, key):