Merge lp:~le-chi-thu/lava-test/enabled-file-cache into lp:lava-test/0.0

Proposed by Le Chi Thu
Status: Merged
Approved by: Zygmunt Krynicki
Approved revision: 128
Merged at revision: 126
Proposed branch: lp:~le-chi-thu/lava-test/enabled-file-cache
Merge into: lp:lava-test/0.0
Diff against target: 86 lines (+17/-13)
2 files modified
doc/changes.rst (+7/-0)
lava_test/utils.py (+10/-13)
To merge this branch: bzr merge lp:~le-chi-thu/lava-test/enabled-file-cache
Reviewer Review Type Date Requested Status
Zygmunt Krynicki (community) Approve
Le Chi Thu (community) Needs Resubmitting
Review via email: mp+96015@code.launchpad.net

Description of the change

When using out of tree tests in LAVA, the master image have network access and install the out-of-tree tests in form of json files. These files will be cached and when boot to test image, the cached file will be use and no network access is needed.

To post a comment you must log in.
Revision history for this message
Zygmunt Krynicki (zyga) wrote :

21 - try:
22 - stream = open(os.path.join(self.cache_dir, key), mode)
23 - yield stream
24 - finally:
25 - stream.close()
26
27 + stream = open(os.path.join(self.cache_dir, key), mode)
28 + return stream
29 +

Why is this change needed?

review: Needs Information
123. By Zygmunt Krynicki

Bump version to 0.5 dev

124. By Zygmunt Krynicki

Allow the user to pre-define analyzer_assigned_uuid

Revision history for this message
Le Chi Thu (le-chi-thu) wrote :

The yield statement is call one level up. You will get method is missing on generator object otherwise.

Revision history for this message
Zygmunt Krynicki (zyga) wrote :

> The yield statement is call one level up. You will get method is missing on
> generator object otherwise.

Yes, but it was supposed to be a context manger via @contextlib.contextmanager decorator (then yield is actually feeding the decorator and everything works as expected). I'd rather fix it than remove this as it prevents dangling URL connection objects.

125. By Zygmunt Krynicki

Make lava-test usable inside a virtualenv.

Revision history for this message
Le Chi Thu (le-chi-thu) wrote :

Made Cache.open_cached protected method

review: Needs Resubmitting
Revision history for this message
Zygmunt Krynicki (zyga) wrote :

 ChiThu, one more change please
 ChiThu, add an entry to doc/changes.rst
 ChiThu, and maybe rebase on top of trunk to make this easier
 ChiThu, ok?

127. By Le Chi Thu <email address hidden> <email address hidden>

Made Cache.open_cached protected method

128. By Le Chi Thu <email address hidden> <email address hidden>

Updated changes.rst file

Revision history for this message
Zygmunt Krynicki (zyga) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'doc/changes.rst'
--- doc/changes.rst 2012-03-06 17:54:26 +0000
+++ doc/changes.rst 2012-03-08 10:16:18 +0000
@@ -1,6 +1,13 @@
1Version History1Version History
2***************2***************
33
4.. _version_0_6:
5
6Version 0.6
7===========
8
9* Enabled file caching. Use when access the out-of-tree json tests.
10
4.. _version_0_5:11.. _version_0_5:
512
6Version 0.513Version 0.5
714
=== modified file 'lava_test/utils.py'
--- lava_test/utils.py 2011-09-12 09:19:10 +0000
+++ lava_test/utils.py 2012-03-08 10:16:18 +0000
@@ -20,6 +20,7 @@
20import shutil20import shutil
21import urllib221import urllib2
22import urlparse22import urlparse
23import sys
2324
24_fake_files = None25_fake_files = None
25_fake_paths = None26_fake_paths = None
@@ -181,28 +182,27 @@
181 cls._instance = cls()182 cls._instance = cls()
182 return cls._instance183 return cls._instance
183184
184 def open_cached(self, key, mode="r"):185 def _open_cached(self, key, mode="r"):
185 """186 """
186 Acts like open() but the pathname is relative to the187 Acts like open() but the pathname is relative to the
187 lava_test-specific cache directory.188 lava_test-specific cache directory.
188 """189 """
190
189 if "w" in mode and not os.path.exists(self.cache_dir):191 if "w" in mode and not os.path.exists(self.cache_dir):
190 os.makedirs(self.cache_dir)192 os.makedirs(self.cache_dir)
191 if os.path.isabs(key):193 if os.path.isabs(key):
192 raise ValueError("key cannot be an absolute path")194 raise ValueError("key cannot be an absolute path")
193 try:
194 stream = open(os.path.join(self.cache_dir, key), mode)
195 yield stream
196 finally:
197 stream.close()
198195
196 stream = open(os.path.join(self.cache_dir, key), mode)
197 return stream
198
199 def _key_for_url(self, url):199 def _key_for_url(self, url):
200 return hashlib.sha1(url).hexdigest()200 return hashlib.sha1(url).hexdigest()
201201
202 def _refresh_url_cache(self, key, url):202 def _refresh_url_cache(self, key, url):
203 with contextlib.nested(203 with contextlib.nested(
204 contextlib.closing(urllib2.urlopen(url)),204 contextlib.closing(urllib2.urlopen(url)),
205 self.open_cached(key, "wb")) as (in_stream, out_stream):205 self._open_cached(key, "wb")) as (in_stream, out_stream):
206 out_stream.write(in_stream.read())206 out_stream.write(in_stream.read())
207207
208 @contextlib.contextmanager208 @contextlib.contextmanager
@@ -212,18 +212,15 @@
212 """212 """
213 # Do not cache local files, this is not what users would expect213 # Do not cache local files, this is not what users would expect
214214
215 # workaround - not using cache at all.215 if url.startswith("file://"):
216 # TODO: fix this and use the cache
217 # if url.startswith("file://"):
218 if True:
219 stream = urllib2.urlopen(url)216 stream = urllib2.urlopen(url)
220 else:217 else:
221 key = self._key_for_url(url)218 key = self._key_for_url(url)
222 try:219 try:
223 stream = self.open_cached(key, "rb")220 stream = self._open_cached(key, "rb")
224 except IOError:221 except IOError:
225 self._refresh_url_cache(key, url)222 self._refresh_url_cache(key, url)
226 stream = self.open_cached(key, "rb")223 stream = self._open_cached(key, "rb")
227 try:224 try:
228 yield stream225 yield stream
229 finally:226 finally:

Subscribers

People subscribed via source and target branches