summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVishvananda Ishaya <vishvananda@gmail.com>2012-01-04 18:40:46 -0800
committerVishvananda Ishaya <vishvananda@gmail.com>2012-01-09 11:40:04 -0800
commitc27e1ccd3f1a9cda6fff70d87aece6e50eba887d (patch)
tree29cb36f9e21d24f75ecce5c613e49f1c6ca8a354
parent5b866f3ad18d497d39a35248c2b0fdb62fcfaa81 (diff)
downloadnova-c27e1ccd3f1a9cda6fff70d87aece6e50eba887d.tar.gz
nova-c27e1ccd3f1a9cda6fff70d87aece6e50eba887d.tar.xz
nova-c27e1ccd3f1a9cda6fff70d87aece6e50eba887d.zip
Refactors utils.load_cached_file
* adds a boolean return representing whether file was reloaded * ensures file is actually closed by using a context manager Change-Id: I4d998c34caa6dde65aaf780c188778477b7f6753
-rw-r--r--nova/tests/test_utils.py20
-rw-r--r--nova/utils.py26
2 files changed, 32 insertions, 14 deletions
diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py
index 92376f1f2..d221212df 100644
--- a/nova/tests/test_utils.py
+++ b/nova/tests/test_utils.py
@@ -345,20 +345,32 @@ class GenericUtilsTestCase(test.TestCase):
def test_read_modified_cached_file(self):
self.mox.StubOutWithMock(os.path, "getmtime")
self.mox.StubOutWithMock(__builtin__, 'open')
-
os.path.getmtime(mox.IgnoreArg()).AndReturn(2)
fake_contents = "lorem ipsum"
fake_file = self.mox.CreateMockAnything()
fake_file.read().AndReturn(fake_contents)
- __builtin__.open(mox.IgnoreArg()).AndReturn(fake_file)
+ fake_context_manager = self.mox.CreateMockAnything()
+ fake_context_manager.__enter__().AndReturn(fake_file)
+ fake_context_manager.__exit__(mox.IgnoreArg(),
+ mox.IgnoreArg(),
+ mox.IgnoreArg())
+
+ __builtin__.open(mox.IgnoreArg()).AndReturn(fake_context_manager)
self.mox.ReplayAll()
cache_data = {"data": 1123, "mtime": 1}
- data = utils.read_cached_file("/this/is/a/fake", cache_data)
- self.mox.VerifyAll()
+ self.reload_called = False
+
+ def test_reload(reloaded_data):
+ self.assertEqual(reloaded_data, fake_contents)
+ self.reload_called = True
+
+ data = utils.read_cached_file("/this/is/a/fake", cache_data,
+ reload_func=test_reload)
self.mox.UnsetStubs()
self.assertEqual(data, fake_contents)
+ self.assertTrue(self.reload_called)
def test_generate_password(self):
password = utils.generate_password()
diff --git a/nova/utils.py b/nova/utils.py
index 991e68b3b..f34db4aba 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -1176,18 +1176,24 @@ def sanitize_hostname(hostname):
return hostname
-def read_cached_file(filename, cache_info):
- """Return the contents of a file. If the file hasn't changed since the
- last invocation, a cached version will be returned.
+def read_cached_file(filename, cache_info, reload_func=None):
+ """Read from a file if it has been modified.
+
+ :param cache_info: dictionary to hold opaque cache.
+ :param reload_func: optional function to be called with data when
+ file is reloaded due to a modification.
+
+ :returns: data from file
+
"""
mtime = os.path.getmtime(filename)
- if cache_info and mtime == cache_info.get('mtime', None):
- return cache_info['data']
-
- data = open(filename).read()
- cache_info['data'] = data
- cache_info['mtime'] = mtime
- return data
+ if not cache_info or mtime != cache_info.get('mtime'):
+ with open(filename) as fap:
+ cache_info['data'] = fap.read()
+ cache_info['mtime'] = mtime
+ if reload_func:
+ reload_func(cache_info['data'])
+ return cache_info['data']
@contextlib.contextmanager