~launchpad-pqm/launchpad/devel

2604 by Canonical.com Patch Queue Manager
Auto depwait tool r=stevea
1
#!/usr/bin/env python2.4
2
# Copyright 2004 Canonical Ltd.  All rights reserved.
3
"""Automatically give back MANUALDEPWAIT build records."""
4
5
import _pythonpath
6
7
import logging
8
import sys
9
import time
10
11
from optparse import OptionParser
12
13
from zope.component import getUtility
14
15
from canonical.lp import initZopeless, dbschema
16
from canonical.launchpad.scripts import (
17
    execute_zcml_for_scripts, logger_options, logger)
18
19
from canonical.launchpad.scripts.lockfile import LockFile
20
from canonical.launchpad.interfaces import IBuildSet, IDistributionSet
21
22
MANUALDEPWAIT = dbschema.BuildStatus.MANUALDEPWAIT
23
24
HOUR = 60
25
DAY = HOUR * 24
26
27
def minutes_since(timestamp):
28
    """Return the number of minutes since the given timestamp."""
29
    return (time.time() - time.mktime(timestamp.timetuple())) / 60
30
31
def main():
32
    options_parser = OptionParser()
33
    logger_options(options_parser)
34
35
    options_parser.add_option(
36
        "-d", "--distro", action="store", type="string", dest="distro",
37
        metavar="DISTRO", help="Distribution to give back from",
38
        default="ubuntu")
39
    options_parser.add_option(
40
        "-m", "--minage", action="store", type="int", dest="minage",
41
        metavar="MINAGE", help="Minimum age in minutes before give-back",
42
        default=8*HOUR)
43
    options_parser.add_option(
44
        "-M", "--maxage", action="store", type="int", dest="maxage",
45
        metavar="MAXAGE",
46
        help="Maximum age in minutes before we don't give-back",
47
        default=3*DAY)
48
    
49
    options, args = options_parser.parse_args(sys.argv)
50
51
    log = logger(options, 'auto-giveback')
52
53
    lockfile = LockFile('/var/lock/launchpad-auto-giveback.lock', logger=log)
54
    lockfile.acquire()
55
56
    try:
57
        log.info("Initialising...")
58
        ztm = initZopeless(dbuser="fiera")
59
        log.info("Executing ZCML...")
60
        execute_zcml_for_scripts()
61
62
        log.info("Finding distribution '%s'..." % options.distro)
63
        distro = getUtility(IDistributionSet)[options.distro]
64
        log.info("Finding builds in MANUALDEPWAIT...")
65
        build_utility = getUtility(IBuildSet)
66
        builds = build_utility.getBuildsForDistribution(distro, MANUALDEPWAIT)
67
        log.info("Changing builds to NEEDSBUILD...")
68
        changed_build_count = 0
69
        for build in builds:
70
            full_age = minutes_since(build.datecreated)
71
            since_last_build = minutes_since(build.datebuilt)
72
            if (since_last_build >= options.minage and
73
                full_age <= options.maxage):
74
                build.buildstate = dbschema.BuildStatus.NEEDSBUILD
75
                changed_build_count += 1
76
        log.info("Changed %d" % changed_build_count)
77
        log.info("Committing...")
78
        ztm.commit()
79
        log.info("Done.")
80
        
81
    finally:
82
        lockfile.release()
83
84
if __name__ == "__main__":
85
    main()