summaryrefslogtreecommitdiffstats
path: root/openstack/common/fileutils.py
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-05-31 14:23:44 +0000
committerGerrit Code Review <review@openstack.org>2013-05-31 14:23:44 +0000
commitbabe5fd2f3cb5b3fb08597d537e9a44aa39609bd (patch)
tree05470882de79ebb0d00e20991aa0557efe93296b /openstack/common/fileutils.py
parent3eac3ba29caa58a57b6c997eb3af6a129b44d94f (diff)
parent1091b4f3bed9b0ad8c23e5db6d4a5cee15fc786c (diff)
downloadoslo-babe5fd2f3cb5b3fb08597d537e9a44aa39609bd.tar.gz
oslo-babe5fd2f3cb5b3fb08597d537e9a44aa39609bd.tar.xz
oslo-babe5fd2f3cb5b3fb08597d537e9a44aa39609bd.zip
Merge "Reduce duplicated code related to policies"
Diffstat (limited to 'openstack/common/fileutils.py')
-rw-r--r--openstack/common/fileutils.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/openstack/common/fileutils.py b/openstack/common/fileutils.py
index b988ad0..bb2ef61 100644
--- a/openstack/common/fileutils.py
+++ b/openstack/common/fileutils.py
@@ -19,6 +19,13 @@
import errno
import os
+from openstack.common.gettextutils import _
+from openstack.common import log as logging
+
+LOG = logging.getLogger(__name__)
+
+_FILE_CACHE = {}
+
def ensure_tree(path):
"""Create a directory (and any ancestor directories required)
@@ -33,3 +40,28 @@ def ensure_tree(path):
raise
else:
raise
+
+
+def read_cached_file(filename, force_reload=False):
+ """Read from a file if it has been modified.
+
+ :param force_reload: Whether to reload the file.
+ :returns: A tuple with a boolean specifying if the data is fresh
+ or not.
+ """
+ global _FILE_CACHE
+
+ if force_reload and filename in _FILE_CACHE:
+ del _FILE_CACHE[filename]
+
+ reloaded = False
+ mtime = os.path.getmtime(filename)
+ cache_info = _FILE_CACHE.setdefault(filename, {})
+
+ if not cache_info or mtime > cache_info.get('mtime', 0):
+ LOG.debug(_("Reloading cached file %s") % filename)
+ with open(filename) as fap:
+ cache_info['data'] = fap.read()
+ cache_info['mtime'] = mtime
+ reloaded = True
+ return (reloaded, cache_info['data'])