=== modified file 'doc/changes.rst'
@@ -11,6 +11,7 @@
https://bugs.launchpad.net/lava-dispatcher/+bug/934164
* Improved error output when running inside virtualenv where the apt and
lsb_release modules are not visible to python.
+ * Enabled URL cache used by the ``lava-test register`` command.
.. _version_0_4:
=== modified file 'lava_test/utils.py'
@@ -20,6 +20,7 @@
import shutil
import urllib2
import urlparse
+import sys
_fake_files = None
_fake_paths = None
@@ -181,28 +182,27 @@
cls._instance = cls()
return cls._instance
- def open_cached(self, key, mode="r"):
+ def _open_cached(self, key, mode="r"):
"""
Acts like open() but the pathname is relative to the
lava_test-specific cache directory.
"""
+
if "w" in mode and not os.path.exists(self.cache_dir):
os.makedirs(self.cache_dir)
if os.path.isabs(key):
raise ValueError("key cannot be an absolute path")
- try:
- stream = open(os.path.join(self.cache_dir, key), mode)
- yield stream
- finally:
- stream.close()
+ stream = open(os.path.join(self.cache_dir, key), mode)
+ return stream
+
def _key_for_url(self, url):
return hashlib.sha1(url).hexdigest()
def _refresh_url_cache(self, key, url):
with contextlib.nested(
contextlib.closing(urllib2.urlopen(url)),
- self.open_cached(key, "wb")) as (in_stream, out_stream):
+ self._open_cached(key, "wb")) as (in_stream, out_stream):
out_stream.write(in_stream.read())
@contextlib.contextmanager
@@ -212,18 +212,15 @@
"""
# Do not cache local files, this is not what users would expect
- # workaround - not using cache at all.
- # TODO: fix this and use the cache
- # if url.startswith("file://"):
- if True:
+ if url.startswith("file://"):
stream = urllib2.urlopen(url)
else:
key = self._key_for_url(url)
try:
- stream = self.open_cached(key, "rb")
+ stream = self._open_cached(key, "rb")
except IOError:
self._refresh_url_cache(key, url)
- stream = self.open_cached(key, "rb")
+ stream = self._open_cached(key, "rb")
try:
yield stream
finally: