From 146b1619f12593adf789eb8eda99c98b75699da5 Mon Sep 17 00:00:00 2001 From: Brian Murray Date: Thu, 16 Jun 2011 12:02:04 -0700 Subject: [PATCH] * grab-attachments, doc/grab-attachments.1: - add in download attachments from duplicates - add in download attachments from all bugs about a package - document new options in the manpage --- debian/changelog | 8 +++++- doc/grab-attachments.1 | 10 ++++++- grab-attachments | 65 +++++++++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/debian/changelog b/debian/changelog index c1c6255..ccd5386 100644 --- a/debian/changelog +++ b/debian/changelog @@ -22,7 +22,13 @@ ubuntu-dev-tools (0.125) UNRELEASED; urgency=low * lp-project-upload: - fix a bug when new milestone wasn't specified (LP: #797170) - -- Benjamin Drung Sat, 28 May 2011 19:43:10 +0200 + [ Brian Murray ] + * grab-attachments, doc/grab-attachments.1: + - add in download attachments from duplicates + - add in download attachments from all bugs about a package + - document new options in the manpage + + -- Brian Murray Thu, 16 Jun 2011 11:59:18 -0700 ubuntu-dev-tools (0.124) unstable; urgency=low diff --git a/doc/grab-attachments.1 b/doc/grab-attachments.1 index aa833c7..edfc78d 100644 --- a/doc/grab-attachments.1 +++ b/doc/grab-attachments.1 @@ -7,7 +7,8 @@ grab\-attachments \- downloads attachments from a Launchpad bug .B grab\-attachments \-h .SH DESCRIPTION \fBgrab\-attachments\fR is a script to download all attachments from a -Launchpad bug report into the a directory named after the bug e.g. bug-1. +Launchpad bug report or bug reports with a source package task into +a directory named after the bug e.g. bug-1. .SH OPTIONS Listed below are the command line options for grab\-attachments: @@ -26,6 +27,13 @@ the default of "production". .B \-\-no\-conf Do not read any configuration files, or configuration from environment variables. +.TP +.BR \-d ", " \-\-duplicates +Download attachments from duplicates too. +.TP +.B \-p \fISRCPACKAGE\fR, \fB\-\-package\fR=\fISRCPACKAGE\fR +Download attachments from all bugs with a task for this source +package. .SH ENVIRONMENT All of the \fBCONFIGURATION VARIABLES\fR below are also supported as environment variables. diff --git a/grab-attachments b/grab-attachments index 8facbec..aba5043 100755 --- a/grab-attachments +++ b/grab-attachments @@ -2,7 +2,8 @@ # # Copyright (C) 2007, Canonical Ltd. # Written by Daniel Holbach, -# Stefano Rivera +# Stefano Rivera, +# Brian Murray # # ################################################################## # @@ -30,6 +31,27 @@ from ubuntutools.config import UDTConfig USAGE = "grab-attachments " + +def download_attachments(bug): + + bug_folder_name = 'bug-%s' % bug.id + + try: + os.mkdir(bug_folder_name) + except OSError, error: + if error.errno == errno.EEXIST: + return + + for attachment in bug.attachments: + f = attachment.data.open() + filename = os.path.join(os.getcwd(), bug_folder_name, + f.filename) + local_file = open(filename, "w") + local_file.write(f.read()) + f.close() + local_file.close() + + def main(): parser = OptionParser('Usage: %prog [options] ') parser.add_option('-l', '--lpinstance', metavar='INSTANCE', @@ -39,8 +61,15 @@ def main(): parser.add_option('--no-conf', dest='no_conf', default=False, action='store_true', help="Don't read config files or environment variables") + parser.add_option('-d', '--duplicates', default=False, + action='store_true', + help='Download attachments from duplicates too') + parser.add_option('-p', '--package', + help='Download attachments from all bugs with a ' + 'task for this source package') + opts, args = parser.parse_args() - if len(args) < 1: + if len(args) < 1 and not opts.package: parser.error('No bug numbers provided') config = UDTConfig(opts.no_conf) if opts.lpinstance is None: @@ -49,30 +78,26 @@ def main(): try: launchpad = Launchpad.login_with("ubuntu-dev-tools", opts.lpinstance) + if opts.package: + ubuntu = launchpad.projects['ubuntu'] + src_package = ubuntu.getSourcePackage(name=opts.package) + if src_package is None: + parser.error('Unable to find package %s' % opts.package) + for task in src_package.searchTasks(): + args.append(task.bug.id) + for arg in args: try: - number = int(arg) + bug_number = int(arg) except ValueError: parser.error("'%s' is not a valid bug number." % arg) - bug = launchpad.bugs[number] + bug = launchpad.bugs[bug_number] + download_attachments(bug) - bug_folder_name = 'bug-%s' % number - - try: - os.mkdir(bug_folder_name) - except OSError, error: - if error.errno == errno.EEXIST: - continue - - for attachment in bug.attachments: - f = attachment.data.open() - filename = os.path.join(os.getcwd(), bug_folder_name, - f.filename) - local_file = open(filename, "w") - local_file.write(f.read()) - f.close() - local_file.close() + if opts.duplicates is True: + for bug in bug.duplicates: + download_attachments(bug) # no LP credentials except IOError, error: