Merge lp:~jml/libdep-service/download-apt-file into lp:libdep-service

Proposed by Jonathan Lange
Status: Merged
Approved by: James Westby
Approved revision: 72
Merged at revision: 71
Proposed branch: lp:~jml/libdep-service/download-apt-file
Merge into: lp:libdep-service
Diff against target: 73 lines (+46/-1)
2 files modified
djlibdep/aptfile.py (+45/-0)
djlibdep/tests/test_aptfile.py (+1/-1)
To merge this branch: bzr merge lp:~jml/libdep-service/download-apt-file
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+131641@code.launchpad.net

Commit message

Add untested function to download Contents and iterate over libraries in it

Description of the change

Untested function to download Contents and iterate over it.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'djlibdep/aptfile.py'
2--- djlibdep/aptfile.py 2012-10-24 14:07:04 +0000
3+++ djlibdep/aptfile.py 2012-10-26 15:40:29 +0000
4@@ -14,7 +14,12 @@
5
6 """apt-file backend for djlibdep."""
7
8+from contextlib import contextmanager
9+import gzip
10 import os
11+from urllib2 import urlopen
12+
13+from fixtures import TempDir
14
15
16 def get_contents_url(archive, suite, architecture):
17@@ -105,3 +110,43 @@
18 for location in locations.split(','):
19 package = os.path.basename(location)
20 yield package, filename
21+
22+
23+def download_file(url, path, buffer_size=4 * 2 ** 10):
24+ """Download 'url' to 'path'."""
25+ with open(path, 'w') as target:
26+ source = urlopen(url)
27+ try:
28+ while True:
29+ chunk = source.read(buffer_size)
30+ if not chunk:
31+ break
32+ target.write(chunk)
33+ finally:
34+ source.close()
35+
36+
37+@contextmanager
38+def downloaded_file(url):
39+ """Context manager that provides a path to a downloaded file."""
40+ with TempDir() as temp_dir:
41+ filename = os.path.join(temp_dir.path, 'download')
42+ download_file(url, filename)
43+ yield filename
44+
45+
46+def iter_library_packages(archive, suite, architecture):
47+ """Iterate over library packages and the libraries they provide.
48+
49+ :param archive: The root URL of the archive,
50+ e.g. 'http://archive.ubuntu.com/ubuntu'.
51+ :param suite: Which suite to get results for.
52+ :param architecture: Which architecture. Normally 'i386' or 'amd64'.
53+ :return: A generator of ``(package, library)``, where ``package`` is
54+ the name of a package that provides ``library``.
55+ """
56+ url = get_contents_url(archive, suite, architecture)
57+ with downloaded_file(url) as contents_path:
58+ with gzip.open(contents_path, 'r') as contents:
59+ for package, library in iter_libraries_in_contents(contents):
60+ yield package, library
61
62=== modified file 'djlibdep/tests/test_aptfile.py'
63--- djlibdep/tests/test_aptfile.py 2012-10-24 14:07:04 +0000
64+++ djlibdep/tests/test_aptfile.py 2012-10-26 15:40:29 +0000
65@@ -31,7 +31,7 @@
66 (archive, suite, arch), contents_url)
67
68
69-class TestEndToEnd(TestCase):
70+class TestIterLibraries(TestCase):
71
72 def parse_contents(self, contents):
73 return list(aptfile.iter_libraries_in_contents(StringIO(contents)))

Subscribers

People subscribed via source and target branches