~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.7 by Karl Fogel
Add the copyright header block to more files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
5
4935.3.7 by Curtis Hovey
Added bad name suppression to cronscripts.
6
# pylint: disable-msg=C0103,W0403
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
7
8
# This script updates the cached source package information in the system.
9
# We use this for fast source package searching (as opposed to joining
10
# through gazillions of publishing tables).
11
12
import _pythonpath
13
14
from zope.component import getUtility
15
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
16
from canonical.config import config
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
17
from canonical.launchpad.interfaces import IDistributionSet
8356.1.1 by Leonard Richardson
Partial move.
18
from lp.services.scripts.base import LaunchpadCronScript
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
19
20
4264.2.1 by James Henstridge
add a LaunchpadCronScript subclass, and make cronscripts/*.py use it
21
class PackageCacheUpdater(LaunchpadCronScript):
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
22
    """Helper class for updating package caches.
23
24
    It iterates over all distributions, distroseries and archives (including
25
    PPAs) updating the package caches to reflect what is currently published
26
    in those locations.
27
    """
28
29
    def updateDistributionPackageCounters(self, distribution):
30
        """Update package counters for a given distribution."""
31
        for distroseries in distribution:
32
            distroseries.updatePackageCount()
33
            self.txn.commit()
34
            for arch in distroseries.architectures:
35
                arch.updatePackageCount()
36
                self.txn.commit()
37
38
    def updateDistributionCache(self, distribution, archive):
39
        """Update package caches for the given location.
40
41
        'archive' can be one of the main archives (PRIMARY, PARTNER or
42
        EMBARGOED) or even a PPA.
43
44
        This method commits the transaction frequently since it deal with
45
        a huge amount of data.
46
47
        PPA archives caches are consolidated in a Archive row to optimize
48
        searches across PPAs.
49
        """
9760.8.1 by Brad Crittenden
Change the non-English 'serieses' to 'series' throughout our codebase.
50
        for distroseries in distribution.series:
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
51
            self.updateDistroSeriesCache(distroseries, archive)
52
53
        distribution.removeOldCacheItems(archive, log=self.logger)
54
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
55
        updates = distribution.updateCompleteSourcePackageCache(
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
56
            archive=archive, ztm=self.txn, log=self.logger)
57
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
58
        if updates > 0:
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
59
            self.txn.commit()
60
61
    def updateDistroSeriesCache(self, distroseries, archive):
62
        """Update package caches for the given location."""
63
        self.logger.info('%s %s %s starting' % (
7976.2.1 by Celso Providelo
Renaming IArchive.title to IArchive.displayname in preparation to have user-defined titles, bug #340457.
64
            distroseries.distribution.name, distroseries.name,
65
            archive.displayname))
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
66
67
        distroseries.removeOldCacheItems(archive=archive, log=self.logger)
68
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
69
        updates = distroseries.updateCompletePackageCache(
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
70
            archive=archive, ztm=self.txn, log=self.logger)
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
71
72
        if updates > 0:
73
            self.txn.commit()
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
74
75
    def main(self):
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
76
        self.logger.debug('Starting the package cache update')
77
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
78
        # Do the package counter and cache update for each distribution.
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
79
        distroset = getUtility(IDistributionSet)
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
80
        for distribution in distroset:
81
            self.logger.info(
82
                'Updating %s package counters' % distribution.name)
83
            self.updateDistributionPackageCounters(distribution)
84
85
            self.logger.info(
86
                'Updating %s main archives' % distribution.name)
87
            for archive in distribution.all_distro_archives:
88
                self.updateDistributionCache(distribution, archive)
89
90
            self.logger.info(
91
                'Updating %s PPAs' % distribution.name)
92
            for archive in distribution.getAllPPAs():
93
                self.updateDistributionCache(distribution, archive)
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
94
                archive.updateArchiveCache()
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
95
6096.3.1 by Celso Providelo
Reducing the commit-rate on update-pkgcache by increasing the commit_chunk size in the content classes and returning the number of updates done. It also sanitize the commits done in the script itself.
96
            # Commit any remaining update for a distribution.
97
            self.txn.commit()
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
98
            self.logger.info('%s done' % distribution.name)
99
100
        self.logger.debug('Finished the package cache update')
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
101
102
if __name__ == '__main__':
5781.5.4 by Celso Providelo
Implementing concentrated PPA caches and improving tests.
103
    script = PackageCacheUpdater(
10449.1.1 by Julian Edwards
update-pkg-cache gets its own db user
104
        'update-cache', dbuser="update-pkg-cache")
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
105
    script.lock_and_run()
2705 by Canonical.com Patch Queue Manager
r=spiv, mark's soyuz loving.
106