2
# -*- coding: UTF-8 -*-
3
"""Sets the "duplicate of" bug of a bug and its dups."""
5
# Copyright (c) 2009 Canonical Ltd.
7
# lp-set-dup is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by the
9
# Free Software Foundation; either version 2, or (at your option) any
12
# lp-set-dup is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
# General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with lp-set-dup; see the file COPYING. If not, write to the Free
19
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23
# Loïc Minier <lool@dooz.org>
26
from optparse import OptionParser
28
from launchpadlib.launchpad import Launchpad
29
from launchpadlib.errors import HTTPError
31
from ubuntutools.config import UDTConfig
34
print >> sys.stderr, "Fatal: " + message
38
usage = "Usage: %prog [-f] <new main bug> <bug to dup> [<bug to dup>...]"
39
opt_parser = OptionParser(usage)
40
opt_parser.add_option("-f",
41
help="Skip confirmation prompt",
42
dest="force", default=False, action="store_true")
43
opt_parser.add_option("-l", "--lpinstance", metavar="INSTANCE",
44
help="Launchpad instance to connect to "
45
"(default: production)",
46
dest="lpinstance", default=None)
47
opt_parser.add_option("--no-conf",
48
help="Don't read config files or "
49
"environment variables.",
50
dest="no_conf", default=False, action="store_true")
51
(options, args) = opt_parser.parse_args()
54
opt_parser.error("Need at least a new main bug and a bug to dup")
56
config = UDTConfig(options.no_conf)
57
if options.lpinstance is None:
58
options.lpinstance = config.get_value("LPINSTANCE")
62
print "Setting up Launchpad"
63
launchpad = Launchpad.login_with("ubuntu-dev-tools", options.lpinstance)
64
print "Launchpad setup complete"
66
suggestion = "check whether python-launchpadlib is installed"
68
die("Couldn't setup Launchpad for the ubuntu-dev-tools consumer; %s" % \
71
# check that the new main bug isn't a duplicate
73
new_main_bug = launchpad.bugs[args[0]]
74
except HTTPError, error:
75
if error.response.status == 401:
76
print >> sys.stderr, ("E: Don't have enough permissions to access "
81
new_main_dup_of = new_main_bug.duplicate_of
82
if new_main_dup_of is not None:
85
answer = raw_input("Bug %s is a duplicate of %s; would you like to "
86
"use %s as the new main bug instead? [y/N]" % \
87
(new_main_bug.id, new_main_dup_of.id,
91
if answer.lower() not in ("y", "yes"):
93
new_main_bug = new_main_dup_of
95
# build list of bugs to process, first the dups then the bug
97
for bug_number in args[1:]:
98
print "Processing %s" % (bug_number)
100
bug = launchpad.bugs[bug_number]
101
except HTTPError, error:
102
if error.response.status == 401:
103
print >> sys.stderr, ("W: Don't have enough permissions to "
104
"access bug %s") % (bug_number)
105
print >> sys.stderr, "W: %s" % (error.content)
109
dups = bug.duplicates
111
bugs_to_process.extend(dups)
112
print "Found %i dups for %s" % (len(dups), bug_number)
113
bugs_to_process.append(bug)
115
# process dups first, then their main bug
116
print "Would set the following bugs as duplicates of %s: %s" % \
117
(new_main_bug.id, " ".join([str(b.id) for b in bugs_to_process]))
119
if not options.force:
122
answer = raw_input("Proceed? [y/N]")
125
if answer.lower() not in ("y", "yes"):
128
for bug in bugs_to_process:
129
print "Marking bug %s as a duplicate of %s" % (bug.id, new_main_bug.id)
130
bug.duplicate_of = new_main_bug
133
if __name__ == '__main__':