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
1=== modified file 'doc/changes.rst'
2--- doc/changes.rst 2012-03-06 17:54:26 +0000
3+++ doc/changes.rst 2012-03-08 10:16:18 +0000
4@@ -1,6 +1,13 @@
5 Version History
6 ***************
7
8+.. _version_0_6:
9+
10+Version 0.6
11+===========
12+
13+* Enabled file caching. Use when access the out-of-tree json tests.
14+
15 .. _version_0_5:
16
17 Version 0.5
18
19=== modified file 'lava_test/utils.py'
20--- lava_test/utils.py 2011-09-12 09:19:10 +0000
21+++ lava_test/utils.py 2012-03-08 10:16:18 +0000
22@@ -20,6 +20,7 @@
23 import shutil
24 import urllib2
25 import urlparse
26+import sys
27
28 _fake_files = None
29 _fake_paths = None
30@@ -181,28 +182,27 @@
31 cls._instance = cls()
32 return cls._instance
33
34- def open_cached(self, key, mode="r"):
35+ def _open_cached(self, key, mode="r"):
36 """
37 Acts like open() but the pathname is relative to the
38 lava_test-specific cache directory.
39 """
40+
41 if "w" in mode and not os.path.exists(self.cache_dir):
42 os.makedirs(self.cache_dir)
43 if os.path.isabs(key):
44 raise ValueError("key cannot be an absolute path")
45- try:
46- stream = open(os.path.join(self.cache_dir, key), mode)
47- yield stream
48- finally:
49- stream.close()
50
51+ stream = open(os.path.join(self.cache_dir, key), mode)
52+ return stream
53+
54 def _key_for_url(self, url):
55 return hashlib.sha1(url).hexdigest()
56
57 def _refresh_url_cache(self, key, url):
58 with contextlib.nested(
59 contextlib.closing(urllib2.urlopen(url)),
60- self.open_cached(key, "wb")) as (in_stream, out_stream):
61+ self._open_cached(key, "wb")) as (in_stream, out_stream):
62 out_stream.write(in_stream.read())
63
64 @contextlib.contextmanager
65@@ -212,18 +212,15 @@
66 """
67 # Do not cache local files, this is not what users would expect
68
69- # workaround - not using cache at all.
70- # TODO: fix this and use the cache
71- # if url.startswith("file://"):
72- if True:
73+ if url.startswith("file://"):
74 stream = urllib2.urlopen(url)
75 else:
76 key = self._key_for_url(url)
77 try:
78- stream = self.open_cached(key, "rb")
79+ stream = self._open_cached(key, "rb")
80 except IOError:
81 self._refresh_url_cache(key, url)
82- stream = self.open_cached(key, "rb")
83+ stream = self._open_cached(key, "rb")
84 try:
85 yield stream
86 finally:

Subscribers

People subscribed via source and target branches