~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/ftpmaster-tools/queue

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-19 04:01:37 UTC
  • mfrom: (13970.7.23 port-to-LaunchpadScript)
  • Revision ID: launchpad@pqm.canonical.com-20110919040137-sr8154o9tfptnqir
[r=sinzui][no-qa] Port a dozen scripts to LaunchpadScript,
        removing their direct initZopeless dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
import _pythonpath
12
12
 
13
 
import sys
14
 
from optparse import OptionParser
 
13
import transaction
15
14
 
16
15
from canonical.config import config
17
 
from canonical.database.sqlbase import ISOLATION_LEVEL_READ_COMMITTED
18
 
from canonical.launchpad.scripts import execute_zcml_for_scripts
19
 
from canonical.lp import initZopeless
20
 
 
21
 
from canonical.launchpad.scripts import logger, logger_options
 
16
from lp.services.scripts.base import LaunchpadScript, LaunchpadScriptFailure
22
17
from lp.soyuz.scripts.queue import (
23
18
    CommandRunner, CommandRunnerError, name_queue_map)
24
19
 
25
20
 
26
 
def main():
27
 
    parser = OptionParser()
28
 
    logger_options(parser)
29
 
 
30
 
    parser.add_option("-Q", "--queue",
31
 
                      dest="queue_name", metavar="QUEUE", default="new",
32
 
                      help="Which queue to consider")
33
 
 
34
 
    parser.add_option("-d", "--distribution",
35
 
                      dest="distribution_name", metavar="DISTRO", default=None,
36
 
                      help="Which distro to look in")
37
 
 
38
 
    parser.add_option("-s", "--suite",
39
 
                      dest="suite_name", metavar="DISTRORELEASE",
40
 
                      default=None,
41
 
                      help=("Which distrorelease to look in, defaults "
42
 
                            "to distribution 'currentseries'."))
43
 
 
44
 
    parser.add_option("-N", "--dry-run", action="store_true",
45
 
                      dest="dryrun", metavar="DRY_RUN", default=False,
46
 
                      help="Whether to treat this as a dry-run or not.")
47
 
 
48
 
    parser.add_option("-M", "--no-mail", action="store_true",
49
 
                      dest="nomail", metavar="NO_MAIL", default=False,
50
 
                      help="Whether to send announce email or not.")
51
 
 
52
 
    parser.add_option("-e", "--exact-match", action="store_true",
53
 
                      dest="exact_match", metavar="EXACTMATCH",
54
 
                      default=False,
55
 
                      help="Whether treat filter as a exact match or not.")
56
 
 
57
 
    parser.add_option("-i", "--ignore-errors", action="store_true",
58
 
                      dest="ignore_errors", metavar="IGNOREERRORS",
59
 
                      default=False,
60
 
                      help="Ignore errors when performing a list of commands.")
61
 
 
62
 
    parser.add_option("-f", "--file", metavar="FILE", default=None,
63
 
                      help="file containing a sequence of command lines.")
64
 
 
65
 
    parser.add_option("-c", "--component", dest="component_name",
66
 
                      metavar="COMPONENT", default=None,
67
 
                      help="When overriding, move package to COMPONENT")
68
 
 
69
 
    parser.add_option("-x", "--section", dest="section_name",
70
 
                      metavar="SECTION", default=None,
71
 
                      help="When overriding, move package to SECTION")
72
 
 
73
 
    parser.add_option("-p", "--priority", dest="priority_name",
74
 
                      metavar="PRIORITY", default=None,
75
 
                      help="When overriding, move package to PRIORITY")
76
 
 
77
 
    options, args = parser.parse_args()
78
 
    log = logger(options, "ftpmaster.queue")
79
 
 
80
 
    if options.queue_name not in name_queue_map:
81
 
        print 'Unable to map queue name "%s"' % options.queue_name
82
 
        return 1
83
 
 
84
 
    no_mail = options.dryrun or options.nomail
85
 
    queue = name_queue_map[options.queue_name]
86
 
 
87
 
    if options.file:
88
 
        args_list = [args.strip().split() for args in
89
 
                     open(options.file).readlines()]
90
 
    else:
91
 
        args_list = [args]
92
 
 
93
 
    cmd_runner = CommandRunner(
94
 
        queue, options.distribution_name, options.suite_name,
95
 
        no_mail, options.component_name, options.section_name,
96
 
        options.priority_name, log=log)
97
 
 
98
 
    print ("Initializing connection to queue %s" % options.queue_name)
99
 
 
100
 
    execute_zcml_for_scripts()
101
 
    ztm = initZopeless(dbuser=config.uploadqueue.dbuser,
102
 
                       isolation=ISOLATION_LEVEL_READ_COMMITTED)
103
 
 
104
 
    for single_args in args_list:
105
 
        try:
106
 
            cmd_runner.execute(single_args, options.exact_match)
107
 
        except CommandRunnerError, info:
108
 
            print (info)
109
 
            if options.ignore_errors:
110
 
                continue
111
 
            ztm.abort()
112
 
            print 'Aborting current transaction'
113
 
            return 1
 
21
class QueueScript(LaunchpadScript):
 
22
 
 
23
    usage = 'Usage: %prog [options] <command>'
 
24
 
 
25
    def add_my_options(self):
 
26
        self.parser.add_option(
 
27
            "-Q", "--queue",
 
28
            dest="queue_name", metavar="QUEUE", default="new",
 
29
            help="Which queue to consider")
 
30
 
 
31
        self.parser.add_option(
 
32
            "-d", "--distribution",
 
33
            dest="distribution_name", metavar="DISTRO", default=None,
 
34
            help="Which distro to look in")
 
35
 
 
36
        self.parser.add_option(
 
37
            "-s", "--suite",
 
38
            dest="suite_name", metavar="DISTRORELEASE", default=None,
 
39
            help=("Which distrorelease to look in, defaults "
 
40
                  "to distribution 'currentseries'."))
 
41
 
 
42
        self.parser.add_option(
 
43
            "-N", "--dry-run", action="store_true",
 
44
            dest="dryrun", metavar="DRY_RUN", default=False,
 
45
            help="Whether to treat this as a dry-run or not.")
 
46
 
 
47
        self.parser.add_option(
 
48
            "-M", "--no-mail", action="store_true",
 
49
            dest="nomail", metavar="NO_MAIL", default=False,
 
50
            help="Whether to send announce email or not.")
 
51
 
 
52
        self.parser.add_option(
 
53
            "-e", "--exact-match", action="store_true",
 
54
            dest="exact_match", metavar="EXACTMATCH", default=False,
 
55
            help="Whether treat filter as a exact match or not.")
 
56
 
 
57
        self.parser.add_option(
 
58
            "-i", "--ignore-errors", action="store_true",
 
59
            dest="ignore_errors", metavar="IGNOREERRORS", default=False,
 
60
            help="Ignore errors when performing a list of commands.")
 
61
 
 
62
        self.parser.add_option(
 
63
            "-f", "--file", metavar="FILE", default=None,
 
64
            help="file containing a sequence of command lines.")
 
65
 
 
66
        self.parser.add_option(
 
67
            "-c", "--component", dest="component_name",
 
68
            metavar="COMPONENT", default=None,
 
69
            help="When overriding, move package to COMPONENT")
 
70
 
 
71
        self.parser.add_option(
 
72
            "-x", "--section", dest="section_name",
 
73
            metavar="SECTION", default=None,
 
74
            help="When overriding, move package to SECTION")
 
75
 
 
76
        self.parser.add_option(
 
77
            "-p", "--priority", dest="priority_name",
 
78
            metavar="PRIORITY", default=None,
 
79
            help="When overriding, move package to PRIORITY")
 
80
 
 
81
    def main(self):
 
82
        if self.options.queue_name not in name_queue_map:
 
83
            self.parser.error(
 
84
                'Unable to map queue name "%s"' % self.options.queue_name)
 
85
 
 
86
        no_mail = self.options.dryrun or self.options.nomail
 
87
        queue = name_queue_map[self.options.queue_name]
 
88
 
 
89
        if self.options.file:
 
90
            args_list = [self.args.strip().split() for args in
 
91
                         open(self.options.file).readlines()]
114
92
        else:
115
 
            if not options.dryrun:
116
 
                ztm.commit()
 
93
            args_list = [self.args]
 
94
 
 
95
        cmd_runner = CommandRunner(
 
96
            queue, self.options.distribution_name, self.options.suite_name,
 
97
            no_mail, self.options.component_name, self.options.section_name,
 
98
            self.options.priority_name, log=self.logger)
 
99
 
 
100
        print "Initializing connection to queue %s" % self.options.queue_name
 
101
 
 
102
        for single_args in args_list:
 
103
            try:
 
104
                cmd_runner.execute(single_args, self.options.exact_match)
 
105
            except CommandRunnerError, info:
 
106
                print (info)
 
107
                if self.options.ignore_errors:
 
108
                    continue
 
109
                transaction.abort()
 
110
                raise LaunchpadScriptFailure(
 
111
                    'Error encountered -- aborting current transaction')
117
112
            else:
118
 
                print "DRY RUN requested, not committing."
119
 
 
120
 
    return 0
121
 
 
 
113
                if not self.options.dryrun:
 
114
                    transaction.commit()
 
115
                else:
 
116
                    print "DRY RUN requested, not committing."
122
117
 
123
118
if __name__ == '__main__':
124
 
    sys.exit(main())
 
119
    QueueScript('queue', config.uploadqueue.dbuser).run()