Also support launchpad URLs like https://bugs.launchpad.net/bugs/XXXXX.
[jelmer/bts-link.git] / btspull
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 [-nv] nnn [remoteUri] [, nnn2 [remoteUri2]]
34
35     -n  --dry-run
36                dry run
37
38     -v  --verbose
39                be verbose
40
41     -s  --short
42                short run, don't deal with 'Done' bugs.
43
44     nnn        the debian bug number
45     remoteUri  the url of the remote bug
46
47 """
48
49 import sys, os, getopt, threading, time, signal
50 import bts, utils
51 from remote import RemoteBts
52 from utils import BTSLConfig as Cnf
53
54 def warn(s):
55     print >> sys.stderr, s
56
57 def die(s):
58     print >> sys.stderr, s
59     os.kill(os.getpid(), signal.SIGKILL)
60     sys.exit(1)
61
62 def usage(exitCode = 1):
63     die(__doc__.lstrip() % (sys.argv[0].split('/')[-1]))
64
65 class Task(threading.Thread):
66     def __init__(self, rbts, res):
67         self.rbts = rbts
68         self.res  = res
69         threading.Thread.__init__(self)
70
71     def run(self):
72         self.res += self.rbts.processQueue()
73         return
74
75 if __name__ == "__main__":
76     debug = False
77     short = False
78     verbose = False
79
80     opts, args = getopt.getopt(sys.argv[1:], 'nsv', ['dry-run', 'short', 'verbose'])
81     if len(args) < 1: usage(1)
82     for o, v in opts:
83         if o in ('-n', '--dry-run'):
84             debug = True
85         if o in ('-s', '--short'):
86             short = True
87         if o in ('-v', '--verbose'):
88             verbose = True
89
90     RemoteBts.setup(Cnf.resources())
91     btsi = bts.BtsInterface(Cnf)
92
93     for id in args:
94         btsbug = btsi.getReport(id)
95
96         if btsbug is None:
97             warn("#%s does not exist" % (id))
98             continue
99
100         if short and btsbug.done:
101             continue
102
103         if not btsbug.forward:
104             if verbose:
105                 if not btsbug.fwdTo:
106                     warn("#%s has no forwards" % (id))
107                 if len(btsbug.fwdTo) is not 1:
108                     warn("#%s has more than one forward" % (id))
109             continue
110
111         rbts = RemoteBts.find(btsbug.forward)
112         if not rbts:
113             if verbose:
114                 warn("#%s: not understood: %s" % (btsbug.id, btsbug.forward))
115             continue
116         rbts.enqueue(btsbug)
117
118     try:
119         res = []
120         for _, v in RemoteBts.resources.iteritems():
121             Task(v['bts'], res).start()
122
123         while threading.activeCount() > 1:
124             time.sleep(1)
125
126     except KeyboardInterrupt:
127         die("*** ^C...")
128
129     per_src = {}
130     for btsbug, cmds in res:
131         if btsbug.srcpackage in per_src:
132             per_src[btsbug.srcpackage] += cmds
133         else:
134             per_src[btsbug.srcpackage] = cmds
135
136     mailer = bts.BtsMailer(debug)
137
138     for spkg, cmds in per_src.iteritems():
139         precmds = []
140         precmds.append("#")
141         precmds.append("# bts-link upstream status pull for source package %s" % (spkg))
142         precmds.append("# see http://lists.debian.org/debian-devel-announce/2006/05/msg00001.html")
143         precmds.append("#")
144         precmds.append("")
145         precmds.append("user %s" % (Cnf.get('general', 'user')))
146         precmds.append("")
147
148         cmds.append('thanks')
149
150         msg = mailer.BtsMail('\n'.join(precmds + cmds))
151         msg['From']       = Cnf.sender()
152         msg['To']         = 'control@bugs.debian.org'
153         msg['Cc']         = "%s, %s@packages.debian.org" % (Cnf.sender(), spkg)
154         msg['Subject']    = '[bts-link] source package %s' % (spkg)
155         msg['X-BTS-Link'] = spkg
156         if Cnf.replyTo(): msg['Reply-To'] = Cnf.replyTo()
157
158         mailer.sendmail(msg['From'], [msg['To'], msg['From'], "%s@packages.debian.org" % (spkg)], msg)
159
160     mailer.unlink()
161
162 # vim:set foldmethod=indent foldnestmax=1: