~launchpad-pqm/launchpad/devel

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Queue management script

Tool for handling and visualisation of upload queue records.
"""

import _pythonpath

import transaction

from lp.services.config import config
from lp.services.scripts.base import (
    LaunchpadScript,
    LaunchpadScriptFailure,
    )
from lp.soyuz.scripts.queue import (
    CommandRunner,
    CommandRunnerError,
    name_queue_map,
    )


class QueueScript(LaunchpadScript):

    usage = 'Usage: %prog [options] <command>'

    def add_my_options(self):
        self.parser.add_option(
            "-Q", "--queue",
            dest="queue_name", metavar="QUEUE", default="new",
            help="Which queue to consider")

        self.parser.add_option(
            "-d", "--distribution",
            dest="distribution_name", metavar="DISTRO", default=None,
            help="Which distro to look in")

        self.parser.add_option(
            "-s", "--suite",
            dest="suite_name", metavar="DISTRORELEASE", default=None,
            help=("Which distrorelease to look in, defaults "
                  "to distribution 'currentseries'."))

        self.parser.add_option(
            "-N", "--dry-run", action="store_true",
            dest="dryrun", metavar="DRY_RUN", default=False,
            help="Whether to treat this as a dry-run or not.")

        self.parser.add_option(
            "-M", "--no-mail", action="store_true",
            dest="nomail", metavar="NO_MAIL", default=False,
            help="Whether to send announce email or not.")

        self.parser.add_option(
            "-e", "--exact-match", action="store_true",
            dest="exact_match", metavar="EXACTMATCH", default=False,
            help="Whether treat filter as a exact match or not.")

        self.parser.add_option(
            "-i", "--ignore-errors", action="store_true",
            dest="ignore_errors", metavar="IGNOREERRORS", default=False,
            help="Ignore errors when performing a list of commands.")

        self.parser.add_option(
            "-f", "--file", metavar="FILE", default=None,
            help="file containing a sequence of command lines.")

        self.parser.add_option(
            "-c", "--component", dest="component_name",
            metavar="COMPONENT", default=None,
            help="When overriding, move package to COMPONENT")

        self.parser.add_option(
            "-x", "--section", dest="section_name",
            metavar="SECTION", default=None,
            help="When overriding, move package to SECTION")

        self.parser.add_option(
            "-p", "--priority", dest="priority_name",
            metavar="PRIORITY", default=None,
            help="When overriding, move package to PRIORITY")

    def main(self):
        if self.options.queue_name not in name_queue_map:
            self.parser.error(
                'Unable to map queue name "%s"' % self.options.queue_name)

        no_mail = self.options.dryrun or self.options.nomail
        queue = name_queue_map[self.options.queue_name]

        if self.options.file:
            args_list = [self.args.strip().split() for args in
                         open(self.options.file).readlines()]
        else:
            args_list = [self.args]

        cmd_runner = CommandRunner(
            queue, self.options.distribution_name, self.options.suite_name,
            no_mail, self.options.component_name, self.options.section_name,
            self.options.priority_name, log=self.logger)

        print "Initializing connection to queue %s" % self.options.queue_name

        for single_args in args_list:
            try:
                cmd_runner.execute(single_args, self.options.exact_match)
            except CommandRunnerError, info:
                print (info)
                if self.options.ignore_errors:
                    continue
                transaction.abort()
                raise LaunchpadScriptFailure(
                    'Error encountered -- aborting current transaction')
            else:
                if not self.options.dryrun:
                    transaction.commit()
                else:
                    print "DRY RUN requested, not committing."

if __name__ == '__main__':
    QueueScript('queue', config.uploadqueue.dbuser).run()