~launchpad-pqm/launchpad/devel

8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
3
4983.1.2 by Curtis Hovey
Added pylint exceptions to database classes.
4
# pylint: disable-msg=E0611,W0212
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
5
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
6
"""Classes to represent a binary package in a distroarchseries."""
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
7
8
__metaclass__ = type
9
10
__all__ = [
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
11
    'DistroArchSeriesBinaryPackage',
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
12
    ]
13
7675.166.301 by Stuart Bishop
Replace In(col, i) with col.is_in(u) to work around Bug #670906 and delint
14
from storm.locals import Desc
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
15
from zope.interface import implements
16
17
from canonical.database.sqlbase import sqlvalues
14560.2.29 by Curtis Hovey
Restored lpstorm module name because it lp engineers know that name.
18
from lp.services.database.lpstorm import IStore
11270.1.3 by Tim Penhey
Changed NotFoundError imports - gee there were a lot of them.
19
from lp.app.errors import NotFoundError
11382.6.34 by Gavin Panella
Reformat imports in all files touched so far.
20
from lp.services.propertycache import cachedproperty
11382.6.44 by Gavin Panella
Fix and reformat imports.
21
from lp.soyuz.enums import PackagePublishingStatus
7675.239.1 by Stuart Bishop
Revert 8192 (Revert Storm 0.14 upgrade again)
22
from lp.soyuz.interfaces.distroarchseriesbinarypackage import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
23
    IDistroArchSeriesBinaryPackage,
24
    )
25
from lp.soyuz.model.binarypackagerelease import BinaryPackageRelease
26
from lp.soyuz.model.distroarchseriesbinarypackagerelease import (
27
    DistroArchSeriesBinaryPackageRelease,
28
    )
29
from lp.soyuz.model.publishing import BinaryPackagePublishingHistory
7675.239.1 by Stuart Bishop
Revert 8192 (Revert Storm 0.14 upgrade again)
30
31
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
32
class DistroArchSeriesBinaryPackage:
4785.3.1 by Jeroen Vermeulen
Removed whitespace before EOLs.
33
    """A Binary Package in the context of a Distro Arch Series.
1716.3.1 by kiko
Fix for bug 3516: Broken traversal on Binary package page. Fixes a silly bug in DistroArchReleaseBinaryPackage.__getitem__, adds a test for it, and as an added bonus, fixes a problem that would happen with currentrelease and real-world versions, with test (getting rid of the use of sourcerer's Version class)
34
35
    Binary Packages are "magic": they don't really exist in the
36
    database. Instead, they are synthesized based on information from
37
    the publishing and binarypackagerelease tables.
38
    """
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
39
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
40
    implements(IDistroArchSeriesBinaryPackage)
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
41
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
42
    def __init__(self, distroarchseries, binarypackagename):
43
        self.distroarchseries = distroarchseries
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
44
        self.binarypackagename = binarypackagename
45
46
    @property
47
    def name(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
48
        """See IDistroArchSeriesBinaryPackage."""
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
49
        return self.binarypackagename.name
50
51
    @property
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
52
    def distroseries(self):
53
        """See IDistroArchSeries."""
54
        return self.distroarchseries.distroseries
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
55
56
    @property
57
    def distribution(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
58
        """See IDistroArchSeries."""
59
        return self.distroseries.distribution
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
60
61
    @property
62
    def displayname(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
63
        """See IDistroArchSeriesBinaryPackage."""
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
64
        return '%s in %s %s' % (
65
            self.binarypackagename.name,
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
66
            self.distroarchseries.distroseries.name,
67
            self.distroarchseries.architecturetag)
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
68
69
    @property
70
    def title(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
71
        """See IDistroArchSeriesBinaryPackage."""
9297.1.3 by Julian Edwards
Make the title for DistroArchSeriesBinaryPackage pages look like English
72
        return '"%s" binary package in %s' % (
9297.1.9 by Julian Edwards
Noodles review suggestions
73
            self.binarypackagename.name, self.distroarchseries.displayname)
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
74
5971.4.3 by Celso Providelo
applying review comments, using cachedproperty for the package cache lookups in DistroSeriesBinaryPackage and DistroArchSeriesBinaryPackage.
75
    @cachedproperty
5971.4.1 by Celso Providelo
Fixing bug #208233 (DistroArchSeriesBinaryPackage dies when it finds PPA caches).
76
    def cache(self):
77
        """See IDistroArchSeriesBinaryPackage."""
13757.2.3 by William Grant
updateCompletePackageCache and updatePackageCache come along for the ride.
78
        from lp.soyuz.model.distroseriespackagecache import (
79
            DistroSeriesPackageCache)
5971.4.1 by Celso Providelo
Fixing bug #208233 (DistroArchSeriesBinaryPackage dies when it finds PPA caches).
80
        query = """
81
            distroseries = %s AND
82
            archive IN %s AND
83
            binarypackagename = %s
84
        """ % sqlvalues(self.distroseries,
85
                        self.distribution.all_distro_archive_ids,
86
                        self.binarypackagename)
87
88
        return DistroSeriesPackageCache.selectOne(query)
89
90
    @property
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
91
    def summary(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
92
        """See IDistroArchSeriesBinaryPackage."""
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
93
        curr = self.currentrelease
94
        if curr is not None:
95
            return curr.summary
5971.4.1 by Celso Providelo
Fixing bug #208233 (DistroArchSeriesBinaryPackage dies when it finds PPA caches).
96
        if self.cache is not None:
97
            return self.cache.summary
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
98
        return None
99
100
    @property
101
    def description(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
102
        """See IDistroArchSeriesBinaryPackage."""
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
103
        curr = self.currentrelease
104
        if curr is not None:
105
            return curr.description
5971.4.1 by Celso Providelo
Fixing bug #208233 (DistroArchSeriesBinaryPackage dies when it finds PPA caches).
106
        if self.cache is not None:
107
            return self.cache.description
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
108
        return None
109
110
    def __getitem__(self, version):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
111
        """See IDistroArchSeriesBinaryPackage."""
3030.2.2 by Celso Providelo
Fix bug # 29687, wrong IDistroArchReleaseBinaryPackage traversal.
112
        query = """
5121.2.6 by Stuart Bishop
Some required code updates
113
        BinaryPackagePublishingHistory.distroarchseries = %s AND
4376.2.6 by Julian Edwards
Fix mistakes from previous commit.
114
        BinaryPackagePublishingHistory.archive IN %s AND
3030.2.2 by Celso Providelo
Fix bug # 29687, wrong IDistroArchReleaseBinaryPackage traversal.
115
        BinaryPackagePublishingHistory.binarypackagerelease =
116
            BinaryPackageRelease.id AND
117
        BinaryPackageRelease.version = %s AND
118
        BinaryPackageRelease.binarypackagename = %s
4376.2.73 by Julian Edwards
Apply review comments.
119
        """ % sqlvalues(
120
                self.distroarchseries,
121
                self.distribution.all_distro_archive_ids,
122
                version,
123
                self.binarypackagename)
3030.2.2 by Celso Providelo
Fix bug # 29687, wrong IDistroArchReleaseBinaryPackage traversal.
124
3024.1.29 by Christian Reis
Fix bug 36208: DistroArchReleaseBinaryPackageRelease traversal fails for non-published releases. Allow traversing on DARBRP entries with any status. Also fixes bug 32605: BPPH listing assumes context has been superseded. Fixes this crasher by correctly interpreting data indicating a removal request
125
        bpph = BinaryPackagePublishingHistory.selectFirst(
126
            query, clauseTables=['binarypackagerelease'],
127
            orderBy=["-datecreated"])
3030.2.2 by Celso Providelo
Fix bug # 29687, wrong IDistroArchReleaseBinaryPackage traversal.
128
1716.3.1 by kiko
Fix for bug 3516: Broken traversal on Binary package page. Fixes a silly bug in DistroArchReleaseBinaryPackage.__getitem__, adds a test for it, and as an added bonus, fixes a problem that would happen with currentrelease and real-world versions, with test (getting rid of the use of sourcerer's Version class)
129
        if bpph is None:
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
130
            return None
3030.2.2 by Celso Providelo
Fix bug # 29687, wrong IDistroArchReleaseBinaryPackage traversal.
131
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
132
        return DistroArchSeriesBinaryPackageRelease(
133
            distroarchseries=self.distroarchseries,
1716.3.1 by kiko
Fix for bug 3516: Broken traversal on Binary package page. Fixes a silly bug in DistroArchReleaseBinaryPackage.__getitem__, adds a test for it, and as an added bonus, fixes a problem that would happen with currentrelease and real-world versions, with test (getting rid of the use of sourcerer's Version class)
134
            binarypackagerelease=bpph.binarypackagerelease)
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
135
136
    @property
137
    def releases(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
138
        """See IDistroArchSeriesBinaryPackage."""
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
139
        ret = BinaryPackageRelease.select("""
5121.2.6 by Stuart Bishop
Some required code updates
140
            BinaryPackagePublishingHistory.distroarchseries = %s AND
4376.2.6 by Julian Edwards
Fix mistakes from previous commit.
141
            BinaryPackagePublishingHistory.archive IN %s AND
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
142
            BinaryPackagePublishingHistory.binarypackagerelease =
143
                BinaryPackageRelease.id AND
144
            BinaryPackageRelease.binarypackagename = %s
4376.2.73 by Julian Edwards
Apply review comments.
145
            """ % sqlvalues(
146
                    self.distroarchseries,
147
                    self.distribution.all_distro_archive_ids,
148
                    self.binarypackagename),
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
149
            orderBy='-datecreated',
150
            distinct=True,
151
            clauseTables=['BinaryPackagePublishingHistory'])
152
        result = []
153
        versions = set()
154
        for bpr in ret:
155
            if bpr.version not in versions:
156
                versions.add(bpr.version)
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
157
                darbpr = DistroArchSeriesBinaryPackageRelease(
158
                    distroarchseries=self.distroarchseries,
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
159
                    binarypackagerelease=bpr)
160
                result.append(darbpr)
161
        return result
162
163
    @property
164
    def currentrelease(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
165
        """See IDistroArchSeriesBinaryPackage."""
1716.3.1 by kiko
Fix for bug 3516: Broken traversal on Binary package page. Fixes a silly bug in DistroArchReleaseBinaryPackage.__getitem__, adds a test for it, and as an added bonus, fixes a problem that would happen with currentrelease and real-world versions, with test (getting rid of the use of sourcerer's Version class)
166
        releases = BinaryPackageRelease.select("""
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
167
            BinaryPackageRelease.binarypackagename = %s AND
168
            BinaryPackageRelease.id =
3496.1.87 by Celso Providelo
remove all callsites and permission for Source/BinaryPackagePublishing
169
                BinaryPackagePublishingHistory.binarypackagerelease AND
5121.2.6 by Stuart Bishop
Some required code updates
170
            BinaryPackagePublishingHistory.distroarchseries = %s AND
4376.2.6 by Julian Edwards
Fix mistakes from previous commit.
171
            BinaryPackagePublishingHistory.archive IN %s AND
3496.1.87 by Celso Providelo
remove all callsites and permission for Source/BinaryPackagePublishing
172
            BinaryPackagePublishingHistory.status = %s
4376.2.73 by Julian Edwards
Apply review comments.
173
            """ % sqlvalues(
174
                    self.binarypackagename,
175
                    self.distroarchseries,
176
                    self.distribution.all_distro_archive_ids,
177
                    PackagePublishingStatus.PUBLISHED),
6557.3.1 by Julian Edwards
Pre-fetch package upload builds and remove a query caused by count() in
178
            orderBy='-datecreated',
179
            limit=1,
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
180
            distinct=True,
7675.166.301 by Stuart Bishop
Replace In(col, i) with col.is_in(u) to work around Bug #670906 and delint
181
            clauseTables=['BinaryPackagePublishingHistory'])
1716.3.1 by kiko
Fix for bug 3516: Broken traversal on Binary package page. Fixes a silly bug in DistroArchReleaseBinaryPackage.__getitem__, adds a test for it, and as an added bonus, fixes a problem that would happen with currentrelease and real-world versions, with test (getting rid of the use of sourcerer's Version class)
182
6557.3.1 by Julian Edwards
Pre-fetch package upload builds and remove a query caused by count() in
183
        # Listify to limit the SQL queries to one only.
184
        results = list(releases)
185
        if len(results) == 0:
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
186
            return None
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
187
        return DistroArchSeriesBinaryPackageRelease(
188
            distroarchseries=self.distroarchseries,
6557.3.1 by Julian Edwards
Pre-fetch package upload builds and remove a query caused by count() in
189
            binarypackagerelease=results[0])
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
190
191
    @property
192
    def publishing_history(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
193
        """See IDistroArchSeriesBinaryPackage."""
7675.239.1 by Stuart Bishop
Revert 8192 (Revert Storm 0.14 upgrade again)
194
        return IStore(BinaryPackagePublishingHistory).find(
195
            BinaryPackagePublishingHistory,
196
            BinaryPackageRelease.binarypackagename == self.binarypackagename,
197
            BinaryPackagePublishingHistory.distroarchseries ==
198
                self.distroarchseries,
7675.166.301 by Stuart Bishop
Replace In(col, i) with col.is_in(u) to work around Bug #670906 and delint
199
            BinaryPackagePublishingHistory.archiveID.is_in(
7675.239.1 by Stuart Bishop
Revert 8192 (Revert Storm 0.14 upgrade again)
200
                self.distribution.all_distro_archive_ids),
201
            BinaryPackagePublishingHistory.binarypackagereleaseID ==
7675.166.301 by Stuart Bishop
Replace In(col, i) with col.is_in(u) to work around Bug #670906 and delint
202
                BinaryPackageRelease.id).config(distinct=True).order_by(
7675.239.1 by Stuart Bishop
Revert 8192 (Revert Storm 0.14 upgrade again)
203
                Desc(BinaryPackagePublishingHistory.datecreated))
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
204
3024.3.17 by James Troup
Add current_published function to get the latest PUBLISHED entry out of publishing_history and raise a NotFoundError if the package doesn't have a PUBLISHED entry.
205
    @property
206
    def current_published(self):
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
207
        """See IDistroArchSeriesBinaryPackage."""
3147.1.21 by Celso Providelo
Fix publishing override method and added missing tests
208
        current = BinaryPackagePublishingHistory.selectFirst("""
5121.2.6 by Stuart Bishop
Some required code updates
209
            BinaryPackagePublishingHistory.distroarchseries = %s AND
4376.2.6 by Julian Edwards
Fix mistakes from previous commit.
210
            BinaryPackagePublishingHistory.archive IN %s AND
3147.1.21 by Celso Providelo
Fix publishing override method and added missing tests
211
            BinaryPackagePublishingHistory.binarypackagerelease =
212
                BinaryPackageRelease.id AND
213
            BinaryPackageRelease.binarypackagename = %s AND
214
            BinaryPackagePublishingHistory.status = %s
4376.2.73 by Julian Edwards
Apply review comments.
215
            """ % sqlvalues(
216
                    self.distroarchseries,
217
                    self.distribution.all_distro_archive_ids,
218
                    self.binarypackagename,
219
                    PackagePublishingStatus.PUBLISHED),
3147.1.21 by Celso Providelo
Fix publishing override method and added missing tests
220
            clauseTables=['BinaryPackageRelease'],
221
            orderBy='-datecreated')
3024.3.17 by James Troup
Add current_published function to get the latest PUBLISHED entry out of publishing_history and raise a NotFoundError if the package doesn't have a PUBLISHED entry.
222
3147.2.62 by Celso Providelo
Applying review comments (take 1)
223
        if current is None:
3024.3.17 by James Troup
Add current_published function to get the latest PUBLISHED entry out of publishing_history and raise a NotFoundError if the package doesn't have a PUBLISHED entry.
224
            raise NotFoundError("Binary package %s not published in %s/%s"
225
                                % (self.binarypackagename.name,
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
226
                                   self.distroarchseries.distroseries.name,
227
                                   self.distroarchseries.architecturetag))
3024.3.17 by James Troup
Add current_published function to get the latest PUBLISHED entry out of publishing_history and raise a NotFoundError if the package doesn't have a PUBLISHED entry.
228
229
        return current
230
7230.4.1 by Michael Nelson
Adding a link to the source package from the distroarchseriesbinarypackage
231
    @property
232
    def distro_source_package(self):
7230.4.2 by Michael Nelson
Updated branch with required changes from bigjools. Also updated the
233
        """See `IDistroArchSeriesBinaryPackage`."""
7230.4.3 by Michael Nelson
Updating branch bug-53171 to accommodate and test for the situation of a
234
        # We provide a source package iff we have a current release
235
        # with a source package release.
236
        current_release = self.currentrelease
237
        if current_release is None:
238
            return None
9297.1.3 by Julian Edwards
Make the title for DistroArchSeriesBinaryPackage pages look like English
239
7230.4.3 by Michael Nelson
Updating branch bug-53171 to accommodate and test for the situation of a
240
        src_pkg_release = current_release.distributionsourcepackagerelease
241
        if src_pkg_release is None:
242
            return None
243
        else:
244
            return src_pkg_release.sourcepackage