~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/process-death-row.py

  • Committer: Celso Providelo
  • Date: 2008-02-19 20:07:58 UTC
  • mto: This revision was merged to the branch mainline in revision 5736.
  • Revision ID: celso.providelo@canonical.com-20080219200758-berv9rgpohhcndd4
process-death-row to using LaunchpadScript.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python2.4
2
 
"""Death row kickoff script."""
 
2
# Copyright 2004-2008 Canonical Ltd.  All rights reserved.
 
3
 
 
4
"""Death row processor script.
 
5
 
 
6
This script removes obsolete files from the selected archive(s) pool.
 
7
 
 
8
You can select a specific distribution or let it default to 'ubuntu'.
 
9
 
 
10
It operates in 2 modes:
 
11
 * all distribution archive (PRIMARY and PARTNER) [default]
 
12
 * all PPAs [--ppa]
 
13
 
 
14
You can optionally specify a different 'pool-root' path which will be used
 
15
as the base path for removing files, instead of the real archive pool root.
 
16
This feature is used to inspect the removed files without actually modifying
 
17
the archive tree.
 
18
 
 
19
There is also a 'dry-run' mode that can be used to operate on the real
 
20
archive tree without removing the files.
 
21
"""
3
22
 
4
23
import _pythonpath
5
24
 
6
 
from optparse import OptionParser
7
 
 
8
25
from zope.component import getUtility
9
26
 
 
27
from canonical.archivepublisher.deathrow import getDeathRow
 
28
from canonical.config import config
10
29
from canonical.launchpad.interfaces import IDistributionSet
11
 
from canonical.config import config
12
 
from canonical.launchpad.scripts import (
13
 
    execute_zcml_for_scripts, logger, logger_options)
14
 
from canonical.lp import initZopeless
15
 
 
16
 
from canonical.archivepublisher.deathrow import getDeathRow
17
 
 
18
 
 
19
 
def main():
20
 
    parser = OptionParser()
21
 
    parser.add_option("-n", "--dry-run", action="store_true",
22
 
                      dest="dry_run", metavar="", default=False,
23
 
                      help=("Dry run: goes through the motions but "
24
 
                            "commits to nothing."))
25
 
    parser.add_option("-d", "--distribution",
26
 
                      dest="distribution", metavar="DISTRO",
27
 
                      help="Specified the distribution name.")
28
 
    parser.add_option("-p", "--pool-root", metavar="PATH",
29
 
                      help="Override the path to the pool folder")
30
 
    parser.add_option("--ppa", action="store_true",
31
 
                      dest="ppa", metavar="PPA", default=False,
32
 
                      help="Run only over PPA archives.")
33
 
 
34
 
    logger_options(parser)
35
 
    (options, args) = parser.parse_args()
36
 
    log = logger(options, "deathrow-distro")
37
 
 
38
 
    log.debug("Initialising zopeless.")
39
 
 
40
 
    txn = initZopeless(dbuser=config.archivepublisher.dbuser)
41
 
    execute_zcml_for_scripts()
42
 
 
43
 
    distribution = getUtility(IDistributionSet).getByName(
44
 
        options.distribution)
45
 
 
46
 
    if not options.ppa:
47
 
        archives = distribution.all_distro_archives
48
 
    else:
49
 
        archives = distribution.getAllPPAs()
50
 
 
51
 
    for archive in archives:
52
 
        death_row = getDeathRow(archive, log, options.pool_root)
 
30
from canonical.launchpad.scripts.base import LaunchpadScript
 
31
 
 
32
 
 
33
class DeathRowProcessor(LaunchpadScript):
 
34
 
 
35
    def add_my_options(self):
 
36
        self.parser.add_option(
 
37
            "-n", "--dry-run", action="store_true", default=False,
 
38
            help="Dry run: goes through the motions but commits to nothing.")
 
39
 
 
40
        self.parser.add_option(
 
41
            "-d", "--distribution", metavar="DISTRO", default='ubuntu',
 
42
            help="Specified the distribution name.")
 
43
 
 
44
        self.parser.add_option(
 
45
            "-p", "--pool-root", metavar="PATH",
 
46
            help="Override the path to the pool folder")
 
47
 
 
48
        self.parser.add_option(
 
49
            "--ppa", action="store_true", default=False,
 
50
            help="Run only over PPA archives.")
 
51
 
 
52
    def main(self):
 
53
        distribution = getUtility(IDistributionSet).getByName(
 
54
            self.options.distribution)
 
55
 
 
56
        if self.options.ppa:
 
57
            archives = distribution.getAllPPAs()
 
58
        else:
 
59
            archives = distribution.all_distro_archives
 
60
 
 
61
        for archive in archives:
 
62
            self.processDeathRow(archive)
 
63
 
 
64
    def processDeathRow(self, archive):
 
65
        """Process death-row for the given archive.
 
66
 
 
67
        It handles the current DB transaction according with the results
 
68
        of the operatin just executed, i.e, commits successfull runs and
 
69
        aborts runs with errors. It also respects 'dry-run' command-line
 
70
        option.
 
71
        """
 
72
        death_row = getDeathRow(
 
73
            archive, self.logger, self.options.pool_root)
 
74
        self.logger.debug(
 
75
            "Unpublishing death row for %s." % archive.title)
53
76
        try:
54
 
            # Unpublish death row
55
 
            log.debug("Unpublishing death row for %s." % archive.title)
56
 
            death_row.reap(options.dry_run)
57
 
 
58
 
            if options.dry_run:
59
 
                log.debug("Dry run mode; rolling back.")
60
 
                txn.abort()
 
77
            death_row.reap(self.options.dry_run)
 
78
        except:
 
79
            self.logger.exception(
 
80
                "Unexpected exception while doing death-row unpublish")
 
81
            self.txn.abort()
 
82
        else:
 
83
            if self.options.dry_run:
 
84
                self.logger.debug("Dry run mode; rolling back.")
 
85
                self.txn.abort()
61
86
            else:
62
 
                log.debug("Committing")
63
 
                txn.commit()
64
 
        except:
65
 
            log.exception("Unexpected exception while doing death-row "
66
 
                          "unpublish")
67
 
            txn.abort()
68
 
            # Continue with other archives.
 
87
                self.logger.debug("Committing")
 
88
                self.txn.commit()
69
89
 
70
90
 
71
91
if __name__ == "__main__":
72
 
    main()
 
92
    script = DeathRowProcessor(
 
93
        'process-death-row', dbuser=config.archivepublisher.dbuser)
 
94
    script.lock_and_run()
 
95
 
73
96