Merge lp:~james-w/linaro-image-tools/set-installed-packages into lp:linaro-image-tools/11.11

Proposed by James Westby
Status: Merged
Merged at revision: 84
Proposed branch: lp:~james-w/linaro-image-tools/set-installed-packages
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~james-w/linaro-image-tools/extra-text-in-packages-files
Diff against target: 84 lines (+45/-5)
2 files modified
hwpack/packages.py (+23/-5)
hwpack/tests/test_packages.py (+22/-0)
To merge this branch: bzr merge lp:~james-w/linaro-image-tools/set-installed-packages
Reviewer Review Type Date Requested Status
Zygmunt Krynicki (community) Approve
Review via email: mp+35339@code.launchpad.net

Description of the change

Hi,

This one relies on the internals of dpkg to some extent, but not really
any more than we already are, and I'm not sure of the last time that this
area actually changed.

What we are doing is allowing the caller to pretend that a list of packages
are installed, which will come in handy later.

We do this by writing the /var/lib/dpkg/status file with the package info
that apt would install if we asked it to install these packages (so includes
dependencies).

Unfortunately apt has no way to do this short of actually installing the
packages, which we certainly don't want to do.

Thanks,

James

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

Looks good.

I would also look at adding the test for reopen=False but I don't think that's really needed.

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-13 21:31:05 +0000
3+++ hwpack/packages.py 2010-09-13 21:31:06 +0000
4@@ -242,8 +242,6 @@
5 """
6 self.cleanup()
7 self.tempdir = tempfile.mkdtemp(prefix="hwpack-apt-cache-")
8- files = ["var/lib/dpkg/status",
9- ]
10 dirs = ["var/lib/dpkg",
11 "etc/apt/",
12 "var/cache/apt/archives/partial",
13@@ -251,9 +249,7 @@
14 ]
15 for d in dirs:
16 os.makedirs(os.path.join(self.tempdir, d))
17- for fn in files:
18- with open(os.path.join(self.tempdir, fn), 'w'):
19- pass
20+ self.set_installed_packages([], reopen=False)
21 sources_list = os.path.join(
22 self.tempdir, "etc", "apt", "sources.list")
23 with open(sources_list, 'w') as f:
24@@ -268,6 +264,28 @@
25 self.cache.open()
26 return self
27
28+ def set_installed_packages(self, packages, reopen=True):
29+ """Set a list of packages as those installed on the system.
30+
31+ This does no installing, just changes dpkg's database to have
32+ the tools think the packages are installed.
33+
34+ :param packages: a list of packages to "install" on the system,
35+ replacing any others.
36+ :type packages: an iterable of FetchedPackages.
37+ :param reopen: whether to reopen the apt cache after doing the
38+ operation. Default is to do so. Note that if it is not done,
39+ then the changes will not be visible in the cache until it
40+ is reopened.
41+ """
42+ with open(
43+ os.path.join(self.tempdir, "var/lib/dpkg/status"), "w") as f:
44+ f.write(
45+ get_packages_file(
46+ packages, extra_text="Status: install ok installed"))
47+ if reopen:
48+ self.cache.open()
49+
50 __enter__ = prepare
51
52 def cleanup(self):
53
54=== modified file 'hwpack/tests/test_packages.py'
55--- hwpack/tests/test_packages.py 2010-09-13 21:31:05 +0000
56+++ hwpack/tests/test_packages.py 2010-09-13 21:31:06 +0000
57@@ -481,6 +481,28 @@
58 self.assertTrue(os.path.isdir(tempdir))
59 self.assertFalse(os.path.exists(tempdir))
60
61+ def test_set_installed_packages(self):
62+ cache = IsolatedAptCache([])
63+ self.addCleanup(cache.cleanup)
64+ cache.prepare()
65+ packages = [DummyFetchedPackage("foo", "1.0")]
66+ cache.set_installed_packages(packages)
67+ self.assertEqual(
68+ get_packages_file(
69+ packages, extra_text="Status: install ok installed"),
70+ open(os.path.join(
71+ cache.tempdir, "var", "lib", "dpkg", "status")).read())
72+
73+ def test_set_installed_packages_empty_list(self):
74+ cache = IsolatedAptCache([])
75+ self.addCleanup(cache.cleanup)
76+ cache.prepare()
77+ cache.set_installed_packages([])
78+ self.assertEqual(
79+ "",
80+ open(os.path.join(
81+ cache.tempdir, "var", "lib", "dpkg", "status")).read())
82+
83
84 class PackageFetcherTests(TestCaseWithFixtures):
85

Subscribers

People subscribed via source and target branches