import-bug-from-debian: split big main function into smaller ones

This allows better understanding of the various parts of the code, by
naming important parts and defining boundaries on the used variables.
This commit is contained in:
Florent 'Skia' Jacquet 2024-07-26 14:59:11 +02:00 committed by Benjamin Drung
parent 9a94c9dea1
commit e328dc05c2

View File

@ -40,9 +40,7 @@ Logger = getLogger()
ATTACHMENT_MAX_SIZE = 2000 ATTACHMENT_MAX_SIZE = 2000
def main(): def parse_args():
bug_re = re.compile(r"bug=(\d+)")
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
"-b", "-b",
@ -72,28 +70,15 @@ def main():
"--no-conf", action="store_true", help="Don't read config files or environment variables." "--no-conf", action="store_true", help="Don't read config files or environment variables."
) )
parser.add_argument("bugs", nargs="+", help="Bug number(s) or URL(s)") parser.add_argument("bugs", nargs="+", help="Bug number(s) or URL(s)")
options = parser.parse_args() return parser.parse_args()
config = UDTConfig(options.no_conf)
if options.lpinstance is None:
options.lpinstance = config.get_value("LPINSTANCE")
if options.dry_run: def get_bug_numbers(bug_list):
launchpad = Launchpad.login_anonymously("ubuntu-dev-tools") bug_re = re.compile(r"bug=(\d+)")
options.verbose = True
else:
launchpad = Launchpad.login_with("ubuntu-dev-tools", options.lpinstance)
if options.verbose:
Logger.setLevel(logging.DEBUG)
debian = launchpad.distributions["debian"]
ubuntu = launchpad.distributions["ubuntu"]
lp_debbugs = launchpad.bug_trackers.getByName(name="debbugs")
bug_nums = [] bug_nums = []
for bug_num in options.bugs: for bug_num in bug_list:
if bug_num.startswith("http"): if bug_num.startswith("http"):
# bug URL # bug URL
match = bug_re.search(bug_num) match = bug_re.search(bug_num)
@ -105,40 +90,29 @@ def main():
bug_num = int(bug_num) bug_num = int(bug_num)
bug_nums.append(bug_num) bug_nums.append(bug_num)
bugs = debianbts.get_status(bug_nums) return bug_nums
if not bugs:
Logger.error("Cannot find any of the listed bugs")
sys.exit(1)
err = False def walk_multipart_message(message):
for bug in bugs:
ubupackage = package = bug.source
if options.package:
ubupackage = options.package
bug_num = bug.bug_num
subject = bug.subject
log = debianbts.get_bug_log(bug_num)
message = log[0]["message"]
attachments = []
if message.is_multipart():
summary = "" summary = ""
attachments = []
i = 1 i = 1
for part in message.walk(): for part in message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
if content_type.startswith("multipart/"): if content_type.startswith("multipart/"):
# we're already iterating on multipart items # we're already iterating on multipart items
# let's just skip the multipart extra metadata # let's just skip the multipart extra metadata
continue continue
if content_type == "application/pgp-signature": if content_type == "application/pgp-signature":
# we don't want to import pgp signature # we're not interested in importing pgp signatures
continue continue
if part.is_attachment(): if part.is_attachment():
attachments.append((i, part)) attachments.append((i, part))
elif content_type.startswith("image/"): elif content_type.startswith("image/"):
# images here are not attachment, they are inline, but Launchpad can't handle # images here are not attachment, they are inline, but Launchpad can't handle that,
# that, so let's add them as attachments # so let's add them as attachments
summary += f"Message part #{i}\n" summary += f"Message part #{i}\n"
summary += f"[inline image '{part.get_filename()}']\n\n" summary += f"[inline image '{part.get_filename()}']\n\n"
attachments.append((i, part)) attachments.append((i, part))
@ -158,6 +132,27 @@ Faulty message part:
{part}""" {part}"""
) )
i += 1 i += 1
return summary, attachments
def process_bugs(bugs, launchpad, package, dry_run=True, browserless=False):
debian = launchpad.distributions["debian"]
ubuntu = launchpad.distributions["ubuntu"]
lp_debbugs = launchpad.bug_trackers.getByName(name="debbugs")
err = False
for bug in bugs:
ubupackage = package = bug.source
if package:
ubupackage = package
bug_num = bug.bug_num
subject = bug.subject
log = debianbts.get_bug_log(bug_num)
message = log[0]["message"]
attachments = []
if message.is_multipart():
summary, attachments = walk_multipart_message(message)
else: else:
summary = log[0]["message"].get_payload() summary = log[0]["message"].get_payload()
@ -193,7 +188,7 @@ Faulty message part:
else: else:
Logger.debug("[data]") Logger.debug("[data]")
if options.dry_run: if dry_run:
Logger.info("Dry-Run: not creating Ubuntu bug.") Logger.info("Dry-Run: not creating Ubuntu bug.")
continue continue
@ -210,17 +205,42 @@ Faulty message part:
comment=f"Imported from Debian bug http://bugs.debian.org/{bug_num}", comment=f"Imported from Debian bug http://bugs.debian.org/{bug_num}",
) )
d_sp = debian.getSourcePackage(name=package) d_sp = debian.getSourcePackage(name=package)
if d_sp is None and options.package: if d_sp is None and package:
d_sp = debian.getSourcePackage(name=options.package) d_sp = debian.getSourcePackage(name=package)
d_task = u_bug.addTask(target=d_sp) d_task = u_bug.addTask(target=d_sp)
d_watch = u_bug.addWatch(remote_bug=bug_num, bug_tracker=lp_debbugs) d_watch = u_bug.addWatch(remote_bug=bug_num, bug_tracker=lp_debbugs)
d_task.bug_watch = d_watch d_task.bug_watch = d_watch
d_task.lp_save() d_task.lp_save()
Logger.info("Opened %s", u_bug.web_link) Logger.info("Opened %s", u_bug.web_link)
if not options.browserless: if not browserless:
webbrowser.open(u_bug.web_link) webbrowser.open(u_bug.web_link)
if err: return err
def main():
options = parse_args()
config = UDTConfig(options.no_conf)
if options.lpinstance is None:
options.lpinstance = config.get_value("LPINSTANCE")
if options.dry_run:
launchpad = Launchpad.login_anonymously("ubuntu-dev-tools")
options.verbose = True
else:
launchpad = Launchpad.login_with("ubuntu-dev-tools", options.lpinstance)
if options.verbose:
Logger.setLevel(logging.DEBUG)
bugs = debianbts.get_status(get_bug_numbers(options.bugs))
if not bugs:
Logger.error("Cannot find any of the listed bugs")
sys.exit(1)
if process_bugs(bugs, launchpad, options.package, options.dry_run, options.browserless):
sys.exit(1) sys.exit(1)