~launchpad-pqm/launchpad/devel

2770.1.4 by Guilherme Salgado
Change all bug reports on people's page to use the google-style lists and have search/sort widgets. Also adds a new targetnamecache column in the BugTask table to allow sorting/searching on that value. Plus *a lot* other cleanup.
1
#!/usr/bin/env python
2
# Copyright 2005 Canonical Ltd.  All rights reserved.
3
4
# This script updates the cached stats in the system
5
6
import _pythonpath
7
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 IBugTaskSet
16
from canonical.launchpad.scripts import (
17
    execute_zcml_for_scripts, logger_options, logger)
18
from canonical.launchpad.scripts.lockfile import LockFile
19
from canonical.config import config
20
21
_default_lock_file = '/var/lock/launchpad-targetnamecacheupdater.lock'
22
23
def update_bugtask_targetname_caches():
24
    """Update the targetnamecache for all IBugTasks.
25
26
    This ensures that the cache values are up-to-date even after, for example,
27
    an IDistribution being renamed.
28
    """
29
    ztm = initZopeless(dbuser=config.targetnamecacheupdater.dbuser,
30
                       implicitBegin=False)
31
    bugtaskset = getUtility(IBugTaskSet)
32
    ztm.begin()
33
    bugtask_ids = [bugtask.id for bugtask in bugtaskset]
34
    ztm.commit()
35
    for bugtask_id in bugtask_ids:
36
        ztm.begin()
37
        bugtask = bugtaskset.get(bugtask_id)
38
        bugtask.updateTargetNameCache()
39
        ztm.commit()
40
41
42
if __name__ == '__main__':
43
    parser = OptionParser()
44
    logger_options(parser)
45
    (options, arguments) = parser.parse_args()
46
    if arguments:
47
        parser.error("Unhandled arguments %s" % repr(arguments))
48
    execute_zcml_for_scripts()
49
50
    log = logger(options, 'update-bugtask-targetnamecaches')
51
    log.info("Updating targetname cache of bugtasks.")
52
53
    lockfile = LockFile(_default_lock_file, logger=log)
54
    try:
55
        lockfile.acquire()
56
    except OSError:
57
        log.info("lockfile %s already exists, exiting", _default_lock_file)
58
        sys.exit(1)
59
60
    try:
61
        update_bugtask_targetname_caches()
62
    finally:
63
        lockfile.release()
64
65
    log.info("Finished updating targetname cache of bugtasks.")
66
67
    sys.exit(0)