Merge lp:~james-w/linaro-image-tools/split-package-fetcher into lp:linaro-image-tools/11.11

Proposed by James Westby
Status: Merged
Merged at revision: 74
Proposed branch: lp:~james-w/linaro-image-tools/split-package-fetcher
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~james-w/linaro-image-tools/add-dot-list-to-filenames
Diff against target: 249 lines (+100/-47)
2 files modified
hwpack/packages.py (+51/-10)
hwpack/tests/test_packages.py (+49/-37)
To merge this branch: bzr merge lp:~james-w/linaro-image-tools/split-package-fetcher
Reviewer Review Type Date Requested Status
Zygmunt Krynicki (community) Approve
Review via email: mp+35166@code.launchpad.net

Description of the change

Hi,

This split PackageFetcher in to two, adding IsolatedAptCache. This
is useful if you want that functionality, but without aiming to download
packages. For instance, I'll want this in a minute in some tests.

It leaves prepare() and cleanup() of PackageFetcher fairly untested, but
I don't want to test the behaviour of those methods on IsolatedAptCache
twice.

Thanks,

James

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

I did not follow hwpack development from the start but this seems like a simple refactoring. I ran the tests, read the diff and cannot point at anything broken.

Approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hwpack/packages.py'
2--- hwpack/packages.py 2010-09-10 20:53:40 +0000
3+++ hwpack/packages.py 2010-09-10 20:53:40 +0000
4@@ -108,14 +108,15 @@
5 (self.name, self.version, self.filename, self.size, self.md5))
6
7
8-class PackageFetcher(object):
9- """A class to fetch packages from a defined list of sources."""
10+class IsolatedAptCache(object):
11+ """A apt.cache.Cache wrapper that isolates it from the system it runs on.
12+
13+ :ivar cache: the isolated cache.
14+ :type cache: apt.cache.Cache
15+ """
16
17 def __init__(self, sources, architecture=None):
18- """Create a PackageFetcher.
19-
20- Once created a PackageFetcher should have its `prepare` method
21- called before use.
22+ """Create an IsolatedAptCache.
23
24 :param sources: a list of sources such that they can be prefixed
25 with "deb " and fed to apt.
26@@ -128,7 +129,7 @@
27 self.tempdir = None
28
29 def prepare(self):
30- """Prepare a PackageFetcher for use.
31+ """Prepare the IsolatedAptCache for use.
32
33 Should be called before use, and after any modification to the list
34 of sources.
35@@ -166,7 +167,7 @@
36 def cleanup(self):
37 """Cleanup any remaining artefacts.
38
39- Should be called on all PackageFetchers when they are finished
40+ Should be called on all IsolatedAptCache when they are finished
41 with.
42 """
43 if self.tempdir is not None and os.path.exists(self.tempdir):
44@@ -176,6 +177,46 @@
45 self.cleanup()
46 return False
47
48+
49+class PackageFetcher(object):
50+ """A class to fetch packages from a defined list of sources."""
51+
52+ def __init__(self, sources, architecture=None):
53+ """Create a PackageFetcher.
54+
55+ Once created a PackageFetcher should have its `prepare` method
56+ called before use.
57+
58+ :param sources: a list of sources such that they can be prefixed
59+ with "deb " and fed to apt.
60+ :type sources: an iterable of str
61+ :param architecture: the architecture to fetch packages for.
62+ :type architecture: str
63+ """
64+ self.cache = IsolatedAptCache(sources, architecture=architecture)
65+
66+ def prepare(self):
67+ """Prepare the PackageFetcher for use.
68+
69+ Should be called before use.
70+ """
71+ self.cache.prepare()
72+ return self
73+
74+ __enter__ = prepare
75+
76+ def cleanup(self):
77+ """Cleanup any remaining artefacts.
78+
79+ Should be called on all PackageFetchers when they are finished
80+ with.
81+ """
82+ self.cache.cleanup()
83+
84+ def __exit__(self, exc_type, exc_value, traceback):
85+ self.cleanup()
86+ return False
87+
88 def fetch_packages(self, packages):
89 """Fetch the files for the given list of package names.
90
91@@ -189,9 +230,9 @@
92 """
93 results = []
94 for package in packages:
95- candidate = self.cache[package].candidate
96+ candidate = self.cache.cache[package].candidate
97 base = os.path.basename(candidate.filename)
98- destfile = os.path.join(self.tempdir, base)
99+ destfile = os.path.join(self.cache.tempdir, base)
100 acq = apt_pkg.Acquire(DummyProgress())
101 acqfile = apt_pkg.AcquireFile(
102 acq, candidate.uri, candidate.md5, candidate.size,
103
104=== modified file 'hwpack/tests/test_packages.py'
105--- hwpack/tests/test_packages.py 2010-09-10 20:53:40 +0000
106+++ hwpack/tests/test_packages.py 2010-09-10 20:53:40 +0000
107@@ -5,6 +5,7 @@
108 from testtools import TestCase
109
110 from hwpack.packages import (
111+ IsolatedAptCache,
112 FetchedPackage,
113 get_packages_file,
114 PackageFetcher,
115@@ -119,87 +120,98 @@
116 self.assertEqual(hash(package1), hash(package2))
117
118
119-class PackageFetcherTests(TestCaseWithFixtures):
120+class AptCacheTests(TestCaseWithFixtures):
121
122 def test_cleanup_removes_tempdir(self):
123- fetcher = PackageFetcher([])
124- fetcher.prepare()
125- tempdir = fetcher.tempdir
126- fetcher.cleanup()
127+ cache = IsolatedAptCache([])
128+ cache.prepare()
129+ tempdir = cache.tempdir
130+ cache.cleanup()
131 self.assertFalse(os.path.exists(tempdir))
132
133 def test_cleanup_ignores_missing_tempdir(self):
134- fetcher = PackageFetcher([])
135- fetcher.prepare()
136- tempdir = fetcher.tempdir
137- fetcher.cleanup()
138+ cache = IsolatedAptCache([])
139+ cache.prepare()
140+ tempdir = cache.tempdir
141+ cache.cleanup()
142 # Check that there is no problem removing it again
143- fetcher.cleanup()
144+ cache.cleanup()
145
146 def test_cleanup_before_prepare(self):
147- fetcher = PackageFetcher([])
148+ cache = IsolatedAptCache([])
149 # Check that there is no problem cleaning up before we start
150- fetcher.cleanup()
151+ cache.cleanup()
152
153 def test_prepare_creates_tempdir(self):
154- fetcher = PackageFetcher([])
155- self.addCleanup(fetcher.cleanup)
156- fetcher.prepare()
157- self.assertTrue(os.path.isdir(fetcher.tempdir))
158+ cache = IsolatedAptCache([])
159+ self.addCleanup(cache.cleanup)
160+ cache.prepare()
161+ self.assertTrue(os.path.isdir(cache.tempdir))
162
163 def test_prepare_creates_var_lib_dpkg_status_file(self):
164- fetcher = PackageFetcher([])
165- self.addCleanup(fetcher.cleanup)
166- fetcher.prepare()
167+ cache = IsolatedAptCache([])
168+ self.addCleanup(cache.cleanup)
169+ cache.prepare()
170 self.assertEqual(
171 '',
172 open(os.path.join(
173- fetcher.tempdir, "var", "lib", "dpkg", "status")).read())
174+ cache.tempdir, "var", "lib", "dpkg", "status")).read())
175
176 def test_prepare_creates_var_cache_apt_archives_partial_dir(self):
177- fetcher = PackageFetcher([])
178- self.addCleanup(fetcher.cleanup)
179- fetcher.prepare()
180+ cache = IsolatedAptCache([])
181+ self.addCleanup(cache.cleanup)
182+ cache.prepare()
183 self.assertTrue(
184 os.path.isdir(os.path.join(
185- fetcher.tempdir, "var", "cache", "apt", "archives",
186+ cache.tempdir, "var", "cache", "apt", "archives",
187 "partial")))
188
189 def test_prepare_creates_var_lib_apt_lists_partial_dir(self):
190- fetcher = PackageFetcher([])
191- self.addCleanup(fetcher.cleanup)
192- fetcher.prepare()
193+ cache = IsolatedAptCache([])
194+ self.addCleanup(cache.cleanup)
195+ cache.prepare()
196 self.assertTrue(
197 os.path.isdir(os.path.join(
198- fetcher.tempdir, "var", "lib", "apt", "lists", "partial")))
199+ cache.tempdir, "var", "lib", "apt", "lists", "partial")))
200
201 def test_prepare_creates_etc_apt_sources_list_file(self):
202 source1 = self.useFixture(AptSourceFixture([]))
203 source2 = self.useFixture(AptSourceFixture([]))
204- fetcher = PackageFetcher(
205+ cache = IsolatedAptCache(
206 [source1.sources_entry, source2.sources_entry])
207- self.addCleanup(fetcher.cleanup)
208- fetcher.prepare()
209+ self.addCleanup(cache.cleanup)
210+ cache.prepare()
211 self.assertEqual(
212 "deb %s\ndeb %s\n" % (
213 source1.sources_entry, source2.sources_entry),
214 open(os.path.join(
215- fetcher.tempdir, "etc", "apt", "sources.list")).read())
216+ cache.tempdir, "etc", "apt", "sources.list")).read())
217
218 def test_prepare_with_arch_creates_etc_apt_apt_conf(self):
219- fetcher = PackageFetcher([], architecture="arch")
220- self.addCleanup(fetcher.cleanup)
221- fetcher.prepare()
222+ cache = IsolatedAptCache([], architecture="arch")
223+ self.addCleanup(cache.cleanup)
224+ cache.prepare()
225 self.assertEqual(
226 'Apt {\nArchitecture "arch";\n}\n',
227 open(os.path.join(
228- fetcher.tempdir, "etc", "apt", "apt.conf")).read())
229+ cache.tempdir, "etc", "apt", "apt.conf")).read())
230+
231+ def test_context_manager(self):
232+ # A smoketest that IsolatedAptCache can be used as a context
233+ # manager
234+ with IsolatedAptCache([]) as cache:
235+ tempdir = cache.tempdir
236+ self.assertTrue(os.path.isdir(tempdir))
237+ self.assertFalse(os.path.exists(tempdir))
238+
239+
240+class PackageFetcherTests(TestCaseWithFixtures):
241
242 def test_context_manager(self):
243 # A smoketest that PackageFetcher can be used as a context
244 # manager
245 with PackageFetcher([]) as fetcher:
246- tempdir = fetcher.tempdir
247+ tempdir = fetcher.cache.tempdir
248 self.assertTrue(os.path.isdir(tempdir))
249 self.assertFalse(os.path.exists(tempdir))
250

Subscribers

People subscribed via source and target branches