~vcs-imports/bts-lin/trunk

160 by madcoder
btsfwd to forward&pull a bug.
1
#! /usr/bin/env python
2
# vim:set encoding=utf-8:
3
###############################################################################
4
# Copyright:
5
#   © 2006 Pierre Habouzit <madcoder@debian.org>
6
#
7
# Redistribution and use in source and binary forms, with or without
8
# modification, are permitted provided that the following conditions
9
# are met:
10
# 1. Redistributions of source code must retain the above copyright
11
#    notice, this list of conditions and the following disclaimer.
12
# 2. Redistributions in binary form must reproduce the above copyright
13
#    notice, this list of conditions and the following disclaimer in the
14
#    documentation and/or other materials provided with the distribution.
15
# 3. Neither the name of the University nor the names of its contributors
16
#    may be used to endorse or promote products derived from this software
17
#    without specific prior written permission.
18
# 
19
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
# SUCH DAMAGE.
30
###############################################################################
31
32
"""
33
Usage: %s [-n] nnn [remoteUri] [, nnn2 [remoteUri2]]
34
35
    -n  --dry-run
36
               dry run
37
    -s  --short
38
               short run, don't deal with 'Done' bugs.
39
40
    nnn        the debian bug number
41
    remoteUri  the url of the remote bug
42
43
"""
44
45
import sys, os, getopt, threading, time, signal
46
import bts, utils
47
from remote import RemoteBts
48
from utils import BTSLConfig as Cnf
49
50
def warn(s):
51
    print >> sys.stderr, s
52
53
def die(s):
54
    print >> sys.stderr, s
55
    os.kill(os.getpid(), signal.SIGKILL)
56
    sys.exit(1)
57
58
def usage(exitCode = 1):
59
    die(__doc__.lstrip() % (sys.argv[0].split('/')[-1]))
60
61
class Task(threading.Thread):
62
    def __init__(self, rbts, res):
63
        self.rbts = rbts
64
        self.res  = res
65
        threading.Thread.__init__(self)
66
67
    def run(self):
68
        self.res += self.rbts.processQueue()
69
        return
70
71
if __name__ == "__main__":
72
    debug = False
73
    short = False
74
75
    opts, args = getopt.getopt(sys.argv[1:], 'ns', ['dry-run', 'short'])
76
    if len(args) < 1: usage(1)
77
    for o, v in opts:
78
        if o in ('-n', '--dry-run'):
79
            debug = True
80
        if o in ('-s', '--short'):
81
            short = True
82
83
    RemoteBts.setup(Cnf.resources())
84
    btsi = bts.BtsInterface(Cnf)
85
86
    queues  = {}
87
88
    cmds = [x.split('\007') for x in '\007'.join(args).split('\007,\007')]
89
    for c in cmds:
90
        if len(c) not in (1, 2): usage(1)
91
        if len(c) is 1:
92
            btsbug = btsi.getReport(c[0])
93
94
            if btsbug is None:
95
                warn("#%s does not exist" % (c[0]))
96
                continue
97
            if not btsbug.forward:
98
                if not btsbug.fwdTo:
99
                    warn("#%s has no forwards" % (c[0]))
100
                if len(btsbug.fwdTo) is not 1:
101
                    warn("#%s has more than one forward" % (c[0]))
102
                continue
103
104
            fwd = btsbug.forward
105
        else:
106
            btsbug = btsi.getReport(c[0])
107
            if btsbug is None:
108
                warn("#%s does not exist" % (c[0]))
109
                continue
110
111
            fwd = c[1]
112
113
        if short and btsbug.done:
114
            continue
115
116
        rbts = RemoteBts.find(fwd)
117
        if not rbts:
118
            warn("#%s: not understood: %s" % (btsbug.id, fwd))
119
            continue
120
        rbts.enqueue(btsbug, fwd)
121
122
    try:
123
        res = []
124
        for _, v in RemoteBts.resources.iteritems():
125
            Task(v['bts'], res).start()
126
127
        while threading.activeCount() > 1:
128
            time.sleep(1)
129
130
        per_src = {}
131
        for btsbug, cmds in res:
132
            if btsbug.srcpackage in per_src:
133
                per_src[btsbug.srcpackage] += cmds
134
            else:
135
                per_src[btsbug.srcpackage] = cmds
136
137
        mailer = bts.BtsMailer(debug)
138
139
        for spkg, cmds in per_src.iteritems():
140
            precmds = []
141
            precmds.append("#")
142
            precmds.append("# bts-link upstream status pull for source package %s" % (spkg))
143
            precmds.append("# see http://lists.debian.org/debian-devel-announce/2006/05/msg00001.html")
144
            precmds.append("#")
145
            precmds.append("")
146
            precmds.append("user %s" % (Cnf.get('general', 'user')))
147
            precmds.append("")
148
149
            cmds.append('thanks')
150
151
            msg = mailer.BtsMail('\n'.join(precmds + cmds))
152
            msg['From']    = Cnf.sender()
153
            msg['To']      = 'control@bugs.debian.org'
154
            msg['Cc']      = "%s, %s@packages.debian.org" % (Cnf.sender(), spkg)
155
            msg['Subject'] = '[bts-link] source package %s' % (spkg)
156
157
            mailer.sendmail(msg['From'], [msg['To'], msg['From'], "%s@packages.debian.org" % (spkg)], msg)
158
159
        mailer.unlink()
160
    except KeyboardInterrupt:
161
        die("*** ^C...")
162
163
# vim:set foldmethod=indent foldnestmax=1: