[Branch,~linaro-validation/lava-test/trunk] Rev 126: Fix and re-enable Cache class

Message ID 20120308144109.14129.7260.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Zygmunt Krynicki March 8, 2012, 2:41 p.m.
Merge authors:
  Le Chi Thu le.chi.thu@linaro.org <le.chi.thu@linaro.org>
  Zygmunt Krynicki (zkrynicki)
Related merge proposals:
  https://code.launchpad.net/~le-chi-thu/lava-test/enabled-file-cache/+merge/96015
  proposed by: Le Chi Thu (le-chi-thu)
  review: Approve - Zygmunt Krynicki (zkrynicki)
  review: Resubmit - Le Chi Thu (le-chi-thu)
------------------------------------------------------------
revno: 126 [merge]
committer: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
branch nick: trunk
timestamp: Thu 2012-03-08 15:39:04 +0100
message:
  Fix and re-enable Cache class
modified:
  doc/changes.rst
  lava_test/utils.py


--
lp:lava-test
https://code.launchpad.net/~linaro-validation/lava-test/trunk

You are subscribed to branch lp:lava-test.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-test/trunk/+edit-subscription

Patch

=== modified file 'doc/changes.rst'
--- doc/changes.rst	2012-03-06 17:54:26 +0000
+++ doc/changes.rst	2012-03-08 10:21:25 +0000
@@ -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'
--- lava_test/utils.py	2011-09-12 09:19:10 +0000
+++ lava_test/utils.py	2012-03-08 09:21:36 +0000
@@ -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: