~james-w/udd/management-commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from optparse import make_option, OptionParser
import sys

from udd import icommon
from udd.paths import paths


def get_options():
    return [
    make_option("--full", dest="full", action="store_true",
            help="Also remove the revids: DANGEROUS"),
    make_option("--zap-revids", dest="zap_revids", action="store",
            help="Also remove the revids for a given comma-separated list of series "
            "(e.g. maverick,natty,oneiric): DANGEROUS (but less than --full)",
            default=""),
    make_option("--force", dest="force", action="store_true",
            help="Requeue even if it hasn't failed"),
    make_option("--priority", dest="priority", action="store_true",
            help="Requeue with a high priority"),
    make_option("--auto", dest="auto", action="store_true",
            help="Requeue this type of problem automatically from now on."),
    make_option("--all-of-type", dest="all", action="store_true",
            help="Requeue all packages with this problem."),
    ]


def get_option_parser():
    parser = OptionParser()
    return parser


def run_script(args, opts):
    errors = 0

    if len(args) < 1:
        print "Specify a list of packages to requeue"
        sys.exit(1)

    opts.zap_revids = filter(None, opts.zap_revids.split(','))

    conn = icommon.get_sqlite_connection(paths.sqlite_file)
    db = icommon.StatusDatabase(paths.sqlite_file)

    for package in args:
        lock = icommon.lock_package(package)
        if lock is None:
            print "Can't lock %s" % package
            errors += 1
            continue
        try:
            retried = db.retry(package, force=opts.force,
                               priority=opts.priority,
                               auto=opts.auto, all=opts.all)
            if not retried:
                print "%s has not failed" % package
                errors += 1
                continue
            elif opts.full:
                # FIXME: do this in one transaction somehow
                with icommon.Cursor(conn) as c:
                    c.execute('delete from %s where package=?'
                              % icommon.RevidDatabase.REVID_TABLE, (package,))
                    c.execute(icommon.CommitDatabase.COMMIT_TABLE_DELETE,
                              (package,))
                    c.execute(icommon.RevidDatabase.DELETE_WORKING, (package,))
            elif opts.zap_revids:
                # FIXME: do this in one transaction somehow
                with icommon.Cursor(conn) as c:
                    for suite in opts.zap_revids:
                        c.execute('delete from %s where package=? and suite=?'
                                  % icommon.RevidDatabase.REVID_TABLE,
                                  (package, suite))
                    c.execute(icommon.CommitDatabase.COMMIT_TABLE_DELETE,
                              (package,))
                    c.execute(icommon.RevidDatabase.DELETE_WORKING, (package,))
        finally:
            lock.close()
        if not opts.all:
            print "%s requeued" % package
        else:
            print "%s and all similar requeued" % package

    if errors > 0:
        sys.exit(1)


def main():
    parser = get_option_parser()
    opts, args = parser.parse_args()
    run_script(args, opts)