~launchpad-pqm/launchpad/devel

2075 by Canonical.com Patch Queue Manager
add better distrorelease translation pages, r=salgado
1
#!/usr/bin/env python
2
# Copyright 2004 Canonical Ltd.  All rights reserved.
3
4
# This script updates the cached stats in the system
5
6
import sys
7
8
from optparse import OptionParser
9
10
from zope.component import getUtility
11
12
from canonical.lp import initZopeless
13
from canonical.launchpad.interfaces import IDistributionSet
14
from canonical.launchpad.scripts import (execute_zcml_for_scripts,
15
    logger_options, logger)
16
from canonical.launchpad.scripts.lockfile import LockFile
17
18
default_lock_file = '/var/lock/launchpad-stats.lock'
19
20
def parse_options(args):
21
    """Parse a set of command line options.
22
23
    Return an optparse.Values object.
24
    """
25
    parser = OptionParser()
26
    parser.add_option("-l", "--lockfile", dest="lockfilename",
27
        default=default_lock_file,
28
        help="The file the script should use to lock the process.")
29
30
    # Add the verbose/quiet options.
31
    logger_options(parser)
32
33
    (options, args) = parser.parse_args(args)
34
35
    return options
36
37
def main(argv):
38
    options = parse_options(argv[1:])
39
40
    # Get the global logger for this task.
41
    logger_object = logger(options, 'launchpad-stats')
42
43
    # Create a lock file so we don't have two daemons running at the same time.
44
    lockfile = LockFile(options.lockfilename, logger=logger_object)
45
    try:
46
        lockfile.acquire()
47
    except OSError:
48
        logger_object.info("lockfile %s already exists, exiting",
49
                           options.lockfilename)
50
        return 0
51
52
    # Setup zcml machinery to be able to use getUtility
53
    execute_zcml_for_scripts()
54
    ztm = initZopeless()
55
56
    # Bare except clause: so that the lockfile is reliably deleted.
57
58
    try:
59
        # Do the stats update
60
        logger_object.debug('Starting the stats update')
61
        distroset = getUtility(IDistributionSet)
62
        for distro in distroset:
63
            for distrorelease in distro.releases:
64
                distrorelease.updateStatistics()
65
        ztm.commit()
66
        logger_object.debug('Finished the stats update')
67
    except:
68
        # Release the lock for the next invocation.
69
        logger_object.exception('An unexpected exception ocurred!')
70
        lockfile.release()
71
        return 1
72
73
    # Release the lock for the next invocation.
74
    lockfile.release()
75
    return 0
76
77
if __name__ == '__main__':
78
    sys.exit(main(sys.argv))
79