Merge lp:~cjohnston/launchpad/proper-pkg-link-copying into lp:launchpad

Proposed by Chris Johnston
Status: Merged
Merged at revision: 17132
Proposed branch: lp:~cjohnston/launchpad/proper-pkg-link-copying
Merge into: lp:launchpad
Diff against target: 176 lines (+110/-24)
2 files modified
lib/lp/soyuz/scripts/initialize_distroseries.py (+44/-24)
lib/lp/soyuz/scripts/tests/test_initialize_distroseries.py (+66/-0)
To merge this branch: bzr merge lp:~cjohnston/launchpad/proper-pkg-link-copying
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+228000@code.launchpad.net

Commit message

Copy packaging links only for packages which are being copied from the parent.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) wrote :

This is looking good, but it needs tests.

I'd also consider dropping the else block; the if block skips the rest of the loop body anyway, so having the SQL in an else block just adds another level of avoidable indentation and obfuscates the diff.

review: Needs Fixing (code)
Revision history for this message
William Grant (wgrant) wrote :

Most of the test lines seem to be identical setup code repeated twice. Please refactor that.

Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/soyuz/scripts/initialize_distroseries.py'
2--- lib/lp/soyuz/scripts/initialize_distroseries.py 2014-07-21 22:45:01 +0000
3+++ lib/lp/soyuz/scripts/initialize_distroseries.py 2014-07-25 00:24:39 +0000
4@@ -510,10 +510,9 @@
5 for parent in self.derivation_parents:
6 spns = self.source_names_by_parent.get(parent.id, None)
7 if spns is not None and len(spns) == 0:
8- # Some packagesets where selected but not a single
9- # source from this parent: we skip the copy since
10- # calling copy with spns=[] would copy all the packagesets
11- # from this parent.
12+ # Some packagesets may have been selected but not a single
13+ # source from this parent. We will not copy any records from
14+ # this parent.
15 continue
16 # spns=None means no packagesets selected so we need to consider
17 # all sources.
18@@ -612,7 +611,13 @@
19 # We iterate over the parents and copy into the child in
20 # sequence to avoid creating duplicates.
21 for parent_id in self.derivation_parent_ids:
22- self._store.execute("""
23+ spns = self.source_names_by_parent.get(parent_id, None)
24+ if spns is not None and len(spns) == 0:
25+ # Some packagesets may have been selected but not a single
26+ # source from this parent. We will not copy any links for this
27+ # parent
28+ continue
29+ sql = ("""
30 INSERT INTO
31 Packaging(
32 distroseries, sourcepackagename, productseries,
33@@ -634,25 +639,40 @@
34 -- Select only the packaging links that are in the parent
35 -- that are not in the child.
36 ChildSeries.id = %s
37- AND Packaging.sourcepackagename in (
38- SELECT sourcepackagename
39- FROM Packaging
40- WHERE distroseries in (
41- SELECT id
42- FROM Distroseries
43- WHERE id = %s
44- )
45- EXCEPT
46- SELECT sourcepackagename
47- FROM Packaging
48- WHERE distroseries in (
49- SELECT id
50- FROM Distroseries
51- WHERE id = ChildSeries.id
52- )
53- )
54- """ % sqlvalues(
55- parent_id, self.distroseries.id, parent_id))
56+ """ % sqlvalues(parent_id, self.distroseries.id))
57+ sql_filter = ("""
58+ AND Packaging.sourcepackagename in (
59+ SELECT
60+ Sourcepackagename.id
61+ FROM
62+ Sourcepackagename
63+ WHERE
64+ Sourcepackagename.name IN %s
65+ )
66+ """ % sqlvalues(spns))
67+ sql_end = ("""
68+ AND Packaging.sourcepackagename in (
69+ SELECT Packaging.sourcepackagename
70+ FROM Packaging
71+ WHERE distroseries in (
72+ SELECT id
73+ FROM Distroseries
74+ WHERE id = %s
75+ )
76+ EXCEPT
77+ SELECT Packaging.sourcepackagename
78+ FROM Packaging
79+ WHERE distroseries in (
80+ SELECT id
81+ FROM Distroseries
82+ WHERE id = ChildSeries.id
83+ )
84+ )
85+ """ % sqlvalues(parent_id))
86+ if spns is not None:
87+ self._store.execute(sql + sql_filter + sql_end)
88+ else:
89+ self._store.execute(sql + sql_end)
90
91 def _copy_packagesets(self):
92 """Copy packagesets from the parent distroseries."""
93
94=== modified file 'lib/lp/soyuz/scripts/tests/test_initialize_distroseries.py'
95--- lib/lp/soyuz/scripts/tests/test_initialize_distroseries.py 2014-07-23 01:37:37 +0000
96+++ lib/lp/soyuz/scripts/tests/test_initialize_distroseries.py 2014-07-25 00:24:39 +0000
97@@ -172,6 +172,31 @@
98 pocket=PackagePublishingPocket.RELEASE)
99 return parent, parent_das, parent_das2, source
100
101+ def setupPackagingTesting(self):
102+ # Setup the environment for testing the packaging links
103+ self.parent, self.parent_das = self.setupParent()
104+ test1 = getUtility(IPackagesetSet).new(
105+ u'test1', u'test 1 packageset', self.parent.owner,
106+ distroseries=self.parent)
107+ test2 = getUtility(IPackagesetSet).new(
108+ u'test2', u'test 2 packageset', self.parent.owner,
109+ distroseries=self.parent)
110+ packages_test1 = ['udev', 'chromium', 'libc6']
111+ packages_test2 = ['postgresql', 'vim']
112+ for pkg in packages_test1:
113+ test1.addSources(pkg)
114+ sp = self.parent.getSourcePackage(pkg)
115+ product_series = self.factory.makeProductSeries()
116+ product_series.setPackaging(
117+ self.parent, sp.sourcepackagename, self.parent.owner)
118+ for pkg in packages_test2:
119+ test2.addSources(pkg)
120+ sp = self.parent.getSourcePackage(pkg)
121+ product_series = self.factory.makeProductSeries()
122+ product_series.setPackaging(
123+ self.parent, sp.sourcepackagename, self.parent.owner)
124+ return packages_test1, packages_test2
125+
126
127 class TestInitializeDistroSeries(InitializationHelperTestCase):
128
129@@ -910,6 +935,47 @@
130 len(packages_test1) + len(packages_test2))
131 self.assertEqual(child.binarycount, 4) # Chromium is FTBFS
132
133+ def test_copy_packaging_links_some(self):
134+ # Test that when copying some packagesets from the parent, only
135+ # the packaging links for the copied packages are copied.
136+ packages_test1, packages_test2 = self.setupPackagingTesting()
137+ packageset1 = getUtility(IPackagesetSet).getByName(
138+ self.parent, u'test1')
139+ child = self._fullInitialize(
140+ [self.parent], packagesets=(str(packageset1.id),))
141+ packagings = child.getMostRecentlyLinkedPackagings()
142+ names = [
143+ packaging.sourcepackagename.name for packaging in packagings]
144+ self.assertEqual(
145+ 0, child.getPrioritizedUnlinkedSourcePackages().count())
146+ self.assertEqual(set(packages_test1), set(names))
147+
148+ def test_copy_packaging_links_empty(self):
149+ # Test that when copying no packagesets from the parent, none of
150+ # the packaging links for the packages are copied.
151+ self.setupPackagingTesting()
152+ child = self._fullInitialize(
153+ [self.parent], packagesets=[])
154+ packagings = child.getMostRecentlyLinkedPackagings()
155+ names = [
156+ packaging.sourcepackagename.name for packaging in packagings]
157+ self.assertEqual(
158+ 0, child.getPrioritizedUnlinkedSourcePackages().count())
159+ self.assertEqual([], names)
160+
161+ def test_copy_packaging_links_none(self):
162+ # Test that when copying all packagesets from the parent, all of
163+ # the packaging links are copied.
164+ packages_test1, packages_test2 = self.setupPackagingTesting()
165+ child = self._fullInitialize(
166+ [self.parent], packagesets=None)
167+ packagings = child.getMostRecentlyLinkedPackagings()
168+ names = [
169+ packaging.sourcepackagename.name for packaging in packagings]
170+ self.assertEqual(
171+ 0, child.getPrioritizedUnlinkedSourcePackages().count())
172+ self.assertEqual(set(packages_test1 + packages_test2), set(names))
173+
174 def test_rebuild_flag(self):
175 # No binaries will get copied if we specify rebuild=True.
176 self.parent, self.parent_das = self.setupParent()