| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- coding: UTF-8 -*- |
| 3 |
"""Sets the "duplicate of" bug of a bug and its dups.""" |
| 4 |
|
| 5 |
# Copyright (c) 2009 Canonical Ltd. |
| 6 |
# |
| 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 3, or (at your option) any |
| 10 |
# later version. |
| 11 |
# |
| 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. |
| 16 |
# |
| 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 |
| 20 |
# 02110-1301, USA. |
| 21 |
# |
| 22 |
# Authors: |
| 23 |
# Loïc Minier <[email protected]> |
| 24 |
|
| 25 |
import sys |
| 26 |
from optparse import OptionParser |
| 27 |
|
| 28 |
from launchpadlib.errors import HTTPError |
| 29 |
|
| 30 |
from lptools import config |
| 31 |
|
| 32 |
def die(message): |
| 33 |
print("Fatal: " + message, file=sys.stderr) |
| 34 |
sys.exit(1) |
| 35 |
|
| 36 |
def main(): |
| 37 |
usage = "Usage: %prog [-f] <new main bug> <bug to dup> [<bug to dup>...]" |
| 38 |
opt_parser = OptionParser(usage) |
| 39 |
opt_parser.add_option("-f", |
| 40 |
help="Skip confirmation prompt", |
| 41 |
dest="force", default=False, action="store_true") |
| 42 |
opt_parser.add_option("-l", "--lpinstance", metavar="INSTANCE", |
| 43 |
help="Launchpad instance to connect to " |
| 44 |
"(default: production)", |
| 45 |
dest="lpinstance", default=None) |
| 46 |
opt_parser.add_option("--no-conf", |
| 47 |
help="Don't read config files or " |
| 48 |
"environment variables.", |
| 49 |
dest="no_conf", default=False, action="store_true") |
| 50 |
(options, args) = opt_parser.parse_args() |
| 51 |
|
| 52 |
if len(args) < 2: |
| 53 |
opt_parser.error("Need at least a new main bug and a bug to dup") |
| 54 |
|
| 55 |
launchpad = config.get_launchpad("set-dup") |
| 56 |
|
| 57 |
# check that the new main bug isn't a duplicate |
| 58 |
try: |
| 59 |
new_main_bug = launchpad.bugs[args[0]] |
| 60 |
except HTTPError as error: |
| 61 |
if error.response.status == 401: |
| 62 |
print(("E: Don't have enough permissions to access " |
| 63 |
"bug %s") % (args[0]), file=sys.stderr) |
| 64 |
die(error.content) |
| 65 |
else: |
| 66 |
raise |
| 67 |
new_main_dup_of = new_main_bug.duplicate_of |
| 68 |
if new_main_dup_of is not None: |
| 69 |
answer = None |
| 70 |
try: |
| 71 |
answer = input("Bug %s is a duplicate of %s; would you like to " |
| 72 |
"use %s as the new main bug instead? [y/N]" % \ |
| 73 |
(new_main_bug.id, new_main_dup_of.id, |
| 74 |
new_main_dup_of.id)) |
| 75 |
except: |
| 76 |
die("Aborted") |
| 77 |
if answer.lower() not in ("y", "yes"): |
| 78 |
die("User aborted") |
| 79 |
new_main_bug = new_main_dup_of |
| 80 |
|
| 81 |
# build list of bugs to process, first the dups then the bug |
| 82 |
bugs_to_process = [] |
| 83 |
for bug_number in args[1:]: |
| 84 |
print("Processing %s" % (bug_number)) |
| 85 |
try: |
| 86 |
bug = launchpad.bugs[bug_number] |
| 87 |
except HTTPError as error: |
| 88 |
if error.response.status == 401: |
| 89 |
print(("W: Don't have enough permissions to " |
| 90 |
"access bug %s") % (bug_number), file=sys.stderr) |
| 91 |
print("W: %s" % (error.content), file=sys.stderr) |
| 92 |
continue |
| 93 |
else: |
| 94 |
raise |
| 95 |
dups = bug.duplicates |
| 96 |
if dups is not None: |
| 97 |
bugs_to_process.extend(dups) |
| 98 |
print("Found %i dups for %s" % (len(dups), bug_number)) |
| 99 |
bugs_to_process.append(bug) |
| 100 |
|
| 101 |
# process dups first, then their main bug |
| 102 |
print("Would set the following bugs as duplicates of %s: %s" % \ |
| 103 |
(new_main_bug.id, " ".join([str(b.id) for b in bugs_to_process]))) |
| 104 |
|
| 105 |
if not options.force: |
| 106 |
answer = None |
| 107 |
try: |
| 108 |
answer = input("Proceed? [y/N]") |
| 109 |
except: |
| 110 |
die("Aborted") |
| 111 |
if answer.lower() not in ("y", "yes"): |
| 112 |
die("User aborted") |
| 113 |
|
| 114 |
for bug in bugs_to_process: |
| 115 |
print("Marking bug %s as a duplicate of %s" % (bug.id, new_main_bug.id)) |
| 116 |
bug.duplicate_of = new_main_bug |
| 117 |
bug.lp_save() |
| 118 |
|
| 119 |
if __name__ == '__main__': |
| 120 |
main() |