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
=== modified file 'hwpack/packages.py'
--- hwpack/packages.py 2010-09-10 20:53:40 +0000
+++ hwpack/packages.py 2010-09-10 20:53:40 +0000
@@ -108,14 +108,15 @@
108 (self.name, self.version, self.filename, self.size, self.md5))108 (self.name, self.version, self.filename, self.size, self.md5))
109109
110110
111class PackageFetcher(object):111class IsolatedAptCache(object):
112 """A class to fetch packages from a defined list of sources."""112 """A apt.cache.Cache wrapper that isolates it from the system it runs on.
113
114 :ivar cache: the isolated cache.
115 :type cache: apt.cache.Cache
116 """
113117
114 def __init__(self, sources, architecture=None):118 def __init__(self, sources, architecture=None):
115 """Create a PackageFetcher.119 """Create an IsolatedAptCache.
116
117 Once created a PackageFetcher should have its `prepare` method
118 called before use.
119120
120 :param sources: a list of sources such that they can be prefixed121 :param sources: a list of sources such that they can be prefixed
121 with "deb " and fed to apt.122 with "deb " and fed to apt.
@@ -128,7 +129,7 @@
128 self.tempdir = None129 self.tempdir = None
129130
130 def prepare(self):131 def prepare(self):
131 """Prepare a PackageFetcher for use.132 """Prepare the IsolatedAptCache for use.
132133
133 Should be called before use, and after any modification to the list134 Should be called before use, and after any modification to the list
134 of sources.135 of sources.
@@ -166,7 +167,7 @@
166 def cleanup(self):167 def cleanup(self):
167 """Cleanup any remaining artefacts.168 """Cleanup any remaining artefacts.
168169
169 Should be called on all PackageFetchers when they are finished170 Should be called on all IsolatedAptCache when they are finished
170 with.171 with.
171 """172 """
172 if self.tempdir is not None and os.path.exists(self.tempdir):173 if self.tempdir is not None and os.path.exists(self.tempdir):
@@ -176,6 +177,46 @@
176 self.cleanup()177 self.cleanup()
177 return False178 return False
178179
180
181class PackageFetcher(object):
182 """A class to fetch packages from a defined list of sources."""
183
184 def __init__(self, sources, architecture=None):
185 """Create a PackageFetcher.
186
187 Once created a PackageFetcher should have its `prepare` method
188 called before use.
189
190 :param sources: a list of sources such that they can be prefixed
191 with "deb " and fed to apt.
192 :type sources: an iterable of str
193 :param architecture: the architecture to fetch packages for.
194 :type architecture: str
195 """
196 self.cache = IsolatedAptCache(sources, architecture=architecture)
197
198 def prepare(self):
199 """Prepare the PackageFetcher for use.
200
201 Should be called before use.
202 """
203 self.cache.prepare()
204 return self
205
206 __enter__ = prepare
207
208 def cleanup(self):
209 """Cleanup any remaining artefacts.
210
211 Should be called on all PackageFetchers when they are finished
212 with.
213 """
214 self.cache.cleanup()
215
216 def __exit__(self, exc_type, exc_value, traceback):
217 self.cleanup()
218 return False
219
179 def fetch_packages(self, packages):220 def fetch_packages(self, packages):
180 """Fetch the files for the given list of package names.221 """Fetch the files for the given list of package names.
181222
@@ -189,9 +230,9 @@
189 """230 """
190 results = []231 results = []
191 for package in packages:232 for package in packages:
192 candidate = self.cache[package].candidate233 candidate = self.cache.cache[package].candidate
193 base = os.path.basename(candidate.filename)234 base = os.path.basename(candidate.filename)
194 destfile = os.path.join(self.tempdir, base)235 destfile = os.path.join(self.cache.tempdir, base)
195 acq = apt_pkg.Acquire(DummyProgress())236 acq = apt_pkg.Acquire(DummyProgress())
196 acqfile = apt_pkg.AcquireFile(237 acqfile = apt_pkg.AcquireFile(
197 acq, candidate.uri, candidate.md5, candidate.size,238 acq, candidate.uri, candidate.md5, candidate.size,
198239
=== modified file 'hwpack/tests/test_packages.py'
--- hwpack/tests/test_packages.py 2010-09-10 20:53:40 +0000
+++ hwpack/tests/test_packages.py 2010-09-10 20:53:40 +0000
@@ -5,6 +5,7 @@
5from testtools import TestCase5from testtools import TestCase
66
7from hwpack.packages import (7from hwpack.packages import (
8 IsolatedAptCache,
8 FetchedPackage,9 FetchedPackage,
9 get_packages_file,10 get_packages_file,
10 PackageFetcher,11 PackageFetcher,
@@ -119,87 +120,98 @@
119 self.assertEqual(hash(package1), hash(package2))120 self.assertEqual(hash(package1), hash(package2))
120121
121122
122class PackageFetcherTests(TestCaseWithFixtures):123class AptCacheTests(TestCaseWithFixtures):
123124
124 def test_cleanup_removes_tempdir(self):125 def test_cleanup_removes_tempdir(self):
125 fetcher = PackageFetcher([])126 cache = IsolatedAptCache([])
126 fetcher.prepare()127 cache.prepare()
127 tempdir = fetcher.tempdir128 tempdir = cache.tempdir
128 fetcher.cleanup()129 cache.cleanup()
129 self.assertFalse(os.path.exists(tempdir))130 self.assertFalse(os.path.exists(tempdir))
130131
131 def test_cleanup_ignores_missing_tempdir(self):132 def test_cleanup_ignores_missing_tempdir(self):
132 fetcher = PackageFetcher([])133 cache = IsolatedAptCache([])
133 fetcher.prepare()134 cache.prepare()
134 tempdir = fetcher.tempdir135 tempdir = cache.tempdir
135 fetcher.cleanup()136 cache.cleanup()
136 # Check that there is no problem removing it again137 # Check that there is no problem removing it again
137 fetcher.cleanup()138 cache.cleanup()
138139
139 def test_cleanup_before_prepare(self):140 def test_cleanup_before_prepare(self):
140 fetcher = PackageFetcher([])141 cache = IsolatedAptCache([])
141 # Check that there is no problem cleaning up before we start142 # Check that there is no problem cleaning up before we start
142 fetcher.cleanup()143 cache.cleanup()
143144
144 def test_prepare_creates_tempdir(self):145 def test_prepare_creates_tempdir(self):
145 fetcher = PackageFetcher([])146 cache = IsolatedAptCache([])
146 self.addCleanup(fetcher.cleanup)147 self.addCleanup(cache.cleanup)
147 fetcher.prepare()148 cache.prepare()
148 self.assertTrue(os.path.isdir(fetcher.tempdir))149 self.assertTrue(os.path.isdir(cache.tempdir))
149150
150 def test_prepare_creates_var_lib_dpkg_status_file(self):151 def test_prepare_creates_var_lib_dpkg_status_file(self):
151 fetcher = PackageFetcher([])152 cache = IsolatedAptCache([])
152 self.addCleanup(fetcher.cleanup)153 self.addCleanup(cache.cleanup)
153 fetcher.prepare()154 cache.prepare()
154 self.assertEqual(155 self.assertEqual(
155 '',156 '',
156 open(os.path.join(157 open(os.path.join(
157 fetcher.tempdir, "var", "lib", "dpkg", "status")).read())158 cache.tempdir, "var", "lib", "dpkg", "status")).read())
158159
159 def test_prepare_creates_var_cache_apt_archives_partial_dir(self):160 def test_prepare_creates_var_cache_apt_archives_partial_dir(self):
160 fetcher = PackageFetcher([])161 cache = IsolatedAptCache([])
161 self.addCleanup(fetcher.cleanup)162 self.addCleanup(cache.cleanup)
162 fetcher.prepare()163 cache.prepare()
163 self.assertTrue(164 self.assertTrue(
164 os.path.isdir(os.path.join(165 os.path.isdir(os.path.join(
165 fetcher.tempdir, "var", "cache", "apt", "archives",166 cache.tempdir, "var", "cache", "apt", "archives",
166 "partial")))167 "partial")))
167168
168 def test_prepare_creates_var_lib_apt_lists_partial_dir(self):169 def test_prepare_creates_var_lib_apt_lists_partial_dir(self):
169 fetcher = PackageFetcher([])170 cache = IsolatedAptCache([])
170 self.addCleanup(fetcher.cleanup)171 self.addCleanup(cache.cleanup)
171 fetcher.prepare()172 cache.prepare()
172 self.assertTrue(173 self.assertTrue(
173 os.path.isdir(os.path.join(174 os.path.isdir(os.path.join(
174 fetcher.tempdir, "var", "lib", "apt", "lists", "partial")))175 cache.tempdir, "var", "lib", "apt", "lists", "partial")))
175176
176 def test_prepare_creates_etc_apt_sources_list_file(self):177 def test_prepare_creates_etc_apt_sources_list_file(self):
177 source1 = self.useFixture(AptSourceFixture([]))178 source1 = self.useFixture(AptSourceFixture([]))
178 source2 = self.useFixture(AptSourceFixture([]))179 source2 = self.useFixture(AptSourceFixture([]))
179 fetcher = PackageFetcher(180 cache = IsolatedAptCache(
180 [source1.sources_entry, source2.sources_entry])181 [source1.sources_entry, source2.sources_entry])
181 self.addCleanup(fetcher.cleanup)182 self.addCleanup(cache.cleanup)
182 fetcher.prepare()183 cache.prepare()
183 self.assertEqual(184 self.assertEqual(
184 "deb %s\ndeb %s\n" % (185 "deb %s\ndeb %s\n" % (
185 source1.sources_entry, source2.sources_entry),186 source1.sources_entry, source2.sources_entry),
186 open(os.path.join(187 open(os.path.join(
187 fetcher.tempdir, "etc", "apt", "sources.list")).read())188 cache.tempdir, "etc", "apt", "sources.list")).read())
188189
189 def test_prepare_with_arch_creates_etc_apt_apt_conf(self):190 def test_prepare_with_arch_creates_etc_apt_apt_conf(self):
190 fetcher = PackageFetcher([], architecture="arch")191 cache = IsolatedAptCache([], architecture="arch")
191 self.addCleanup(fetcher.cleanup)192 self.addCleanup(cache.cleanup)
192 fetcher.prepare()193 cache.prepare()
193 self.assertEqual(194 self.assertEqual(
194 'Apt {\nArchitecture "arch";\n}\n',195 'Apt {\nArchitecture "arch";\n}\n',
195 open(os.path.join(196 open(os.path.join(
196 fetcher.tempdir, "etc", "apt", "apt.conf")).read())197 cache.tempdir, "etc", "apt", "apt.conf")).read())
198
199 def test_context_manager(self):
200 # A smoketest that IsolatedAptCache can be used as a context
201 # manager
202 with IsolatedAptCache([]) as cache:
203 tempdir = cache.tempdir
204 self.assertTrue(os.path.isdir(tempdir))
205 self.assertFalse(os.path.exists(tempdir))
206
207
208class PackageFetcherTests(TestCaseWithFixtures):
197209
198 def test_context_manager(self):210 def test_context_manager(self):
199 # A smoketest that PackageFetcher can be used as a context211 # A smoketest that PackageFetcher can be used as a context
200 # manager212 # manager
201 with PackageFetcher([]) as fetcher:213 with PackageFetcher([]) as fetcher:
202 tempdir = fetcher.tempdir214 tempdir = fetcher.cache.tempdir
203 self.assertTrue(os.path.isdir(tempdir))215 self.assertTrue(os.path.isdir(tempdir))
204 self.assertFalse(os.path.exists(tempdir))216 self.assertFalse(os.path.exists(tempdir))
205217

Subscribers

People subscribed via source and target branches