~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=C0103,W0403

from zope.component import getUtility

from canonical.database.sqlbase import sqlvalues
from canonical.launchpad.webapp.interfaces import (
    DEFAULT_FLAVOR,
    IStoreSelector,
    MAIN_STORE,
    )
from lp.services.scripts.base import LaunchpadCronScript
from lp.soyuz.enums import ArchivePurpose

# PPAs that we never want to expire.
BLACKLISTED_PPAS = """
adobe-isv
chelsea-team
dennis-team
elvis-team
fluendo-isv
natick-team
netbook-remix-team
netbook-team
oem-solutions-group
payson
transyl
ubuntu-mobile
wheelbarrow
bzr
bzr-beta-ppa
bzr-nightly-ppa
""".split()


class ArchiveExpirer(LaunchpadCronScript):
    """Helper class for expiring old PPA binaries.

    Any PPA binary older than 30 days that is superseded or deleted
    will be marked for immediate expiry.
    """
    blacklist = BLACKLISTED_PPAS

    def add_my_options(self):
        """Add script command line options."""
        self.parser.add_option(
            "-n", "--dry-run", action="store_true",
            dest="dryrun", metavar="DRY_RUN", default=False,
            help="If set, no transactions are committed")
        self.parser.add_option(
            "-e", "--expire-after", action="store", type="int",
            dest="num_days", metavar="DAYS", default=15,
            help=("The number of days after which to expire binaries. "
                  "Must be specified."))

    def determineSourceExpirables(self, num_days):
        """Return expirable libraryfilealias IDs."""
        stay_of_execution = '%d days' % num_days
        archive_types = (ArchivePurpose.PPA, ArchivePurpose.PARTNER)

        # The subquery here has to repeat the checks for privacy and
        # blacklisting on *other* publications that are also done in
        # the main loop for the archive being considered.
        results = self.store.execute("""
            SELECT lfa.id
            FROM
                LibraryFileAlias AS lfa,
                Archive,
                SourcePackageReleaseFile AS sprf,
                SourcePackageRelease AS spr,
                SourcePackagePublishingHistory AS spph
            WHERE
                lfa.id = sprf.libraryfile
                AND spr.id = sprf.sourcepackagerelease
                AND spph.sourcepackagerelease = spr.id
                AND spph.dateremoved < (
                    CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - interval %s)
                AND spph.archive = archive.id
                AND archive.purpose IN %s
                AND lfa.expires IS NULL
            EXCEPT
            SELECT sprf.libraryfile
            FROM
                SourcePackageRelease AS spr,
                SourcePackageReleaseFile AS sprf,
                SourcePackagePublishingHistory AS spph,
                Archive AS a,
                Person AS p
            WHERE
                spr.id = sprf.sourcepackagerelease
                AND spph.sourcepackagerelease = spr.id
                AND spph.archive = a.id
                AND p.id = a.owner
                AND (
                    (p.name IN %s AND a.purpose = %s)
                    OR a.private IS TRUE
                    OR a.purpose NOT IN %s
                    OR dateremoved >
                        CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - interval %s
                    OR dateremoved IS NULL);
            """ % sqlvalues(
                stay_of_execution, archive_types, self.blacklist,
                ArchivePurpose.PPA, archive_types, stay_of_execution))

        lfa_ids = results.get_all()
        return lfa_ids

    def determineBinaryExpirables(self, num_days):
        """Return expirable libraryfilealias IDs."""
        stay_of_execution = '%d days' % num_days
        archive_types = (ArchivePurpose.PPA, ArchivePurpose.PARTNER)

        # The subquery here has to repeat the checks for privacy and
        # blacklisting on *other* publications that are also done in
        # the main loop for the archive being considered.
        results = self.store.execute("""
            SELECT lfa.id
            FROM
                LibraryFileAlias AS lfa,
                Archive,
                BinaryPackageFile AS bpf,
                BinaryPackageRelease AS bpr,
                BinaryPackagePublishingHistory AS bpph
            WHERE
                lfa.id = bpf.libraryfile
                AND bpr.id = bpf.binarypackagerelease
                AND bpph.binarypackagerelease = bpr.id
                AND bpph.dateremoved < (
                    CURRENT_TIMESTAMP AT TIME ZONE 'UTC' -
                    interval %(stay_of_execution)s)
                AND bpph.archive = archive.id
                AND archive.purpose IN %(archive_types)s
                AND lfa.expires IS NULL
            EXCEPT
            SELECT bpf.libraryfile
            FROM
                BinaryPackageRelease AS bpr,
                BinaryPackageFile AS bpf,
                BinaryPackagePublishingHistory AS bpph,
                Archive AS a,
                Person AS p
            WHERE
                bpr.id = bpf.binarypackagerelease
                AND bpph.binarypackagerelease = bpr.id
                AND bpph.archive = a.id
                AND p.id = a.owner
                AND (
                    (p.name IN %(blacklist)s AND a.purpose = %(ppa)s)
                    OR a.private IS TRUE
                    OR a.purpose NOT IN %(archive_types)s
                    OR dateremoved > (
                        CURRENT_TIMESTAMP AT TIME ZONE 'UTC' -
                        interval %(stay_of_execution)s)
                    OR dateremoved IS NULL)
            """ % sqlvalues(
                stay_of_execution=stay_of_execution,
                archive_types=archive_types,
                blacklist=self.blacklist,
                ppa=ArchivePurpose.PPA))

        lfa_ids = results.get_all()
        return lfa_ids

    def main(self):
        self.logger.info('Starting the PPA binary expiration')
        num_days = self.options.num_days
        self.logger.info("Expiring files up to %d days ago" % num_days)

        self.store = getUtility(IStoreSelector).get(
            MAIN_STORE, DEFAULT_FLAVOR)

        lfa_ids = self.determineSourceExpirables(num_days)
        lfa_ids.extend(self.determineBinaryExpirables(num_days))
        batch_count = 0
        batch_limit = 500
        for id in lfa_ids:
            self.logger.info("Expiring libraryfilealias %s" % id)
            self.store.execute("""
                UPDATE libraryfilealias
                SET expires = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
                WHERE id = %s
                """ % id)
            batch_count += 1
            if batch_count % batch_limit == 0:
                if self.options.dryrun:
                    self.logger.info(
                        "%s done, not committing (dryrun mode)" % batch_count)
                    self.txn.abort()
                else:
                    self.logger.info(
                        "%s done, committing transaction" % batch_count)
                    self.txn.commit()

        if self.options.dryrun:
            self.txn.abort()
        else:
            self.txn.commit()

        self.logger.info('Finished PPA binary expiration')