~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
2125 by Canonical.com Patch Queue Manager
[r=bjornt] Cronscript refactorings
6
import _pythonpath
7
2075 by Canonical.com Patch Queue Manager
add better distrorelease translation pages, r=salgado
8
import sys
9
10
from optparse import OptionParser
11
12
from zope.component import getUtility
13
14
from canonical.lp import initZopeless
15
from canonical.launchpad.interfaces import IDistributionSet
16
from canonical.launchpad.scripts import (execute_zcml_for_scripts,
17
    logger_options, logger)
18
from canonical.launchpad.scripts.lockfile import LockFile
19
20
default_lock_file = '/var/lock/launchpad-stats.lock'
21
22
def parse_options(args):
23
    """Parse a set of command line options.
24
25
    Return an optparse.Values object.
26
    """
27
    parser = OptionParser()
28
    parser.add_option("-l", "--lockfile", dest="lockfilename",
29
        default=default_lock_file,
30
        help="The file the script should use to lock the process.")
31
32
    # Add the verbose/quiet options.
33
    logger_options(parser)
34
35
    (options, args) = parser.parse_args(args)
36
37
    return options
38
39
def main(argv):
40
    options = parse_options(argv[1:])
41
42
    # Get the global logger for this task.
43
    logger_object = logger(options, 'launchpad-stats')
44
45
    # Create a lock file so we don't have two daemons running at the same time.
46
    lockfile = LockFile(options.lockfilename, logger=logger_object)
47
    try:
48
        lockfile.acquire()
49
    except OSError:
50
        logger_object.info("lockfile %s already exists, exiting",
51
                           options.lockfilename)
2125 by Canonical.com Patch Queue Manager
[r=bjornt] Cronscript refactorings
52
        return 1
2075 by Canonical.com Patch Queue Manager
add better distrorelease translation pages, r=salgado
53
54
    try:
2125 by Canonical.com Patch Queue Manager
[r=bjornt] Cronscript refactorings
55
        # Setup zcml machinery to be able to use getUtility
56
        execute_zcml_for_scripts()
57
        ztm = initZopeless()
58
2075 by Canonical.com Patch Queue Manager
add better distrorelease translation pages, r=salgado
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')
2125 by Canonical.com Patch Queue Manager
[r=bjornt] Cronscript refactorings
67
        return 0
68
    finally:
2075 by Canonical.com Patch Queue Manager
add better distrorelease translation pages, r=salgado
69
        lockfile.release()
70
71
if __name__ == '__main__':
72
    sys.exit(main(sys.argv))
73