Debian Codegraph @

bin/lp-grab-attachments

raw
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2007, Canonical Ltd.
4 # Written by Daniel Holbach,
5 # Stefano Rivera,
6 # Brian Murray
7 #
8 # ##################################################################
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; version 3.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # See file /usr/share/common-licenses/GPL-3 for more details.
20 #
21 # ##################################################################
22
23 from optparse import OptionParser
24 import codecs
25 import errno
26 import os
27
28 from lptools import config
29
30 USAGE = "lp-grab-attachments <bug numbers>"
31
32
33 def download_attachments(bug, descriptions):
34
35 bug_folder_name = 'bug-%s' % bug.id
36
37 try:
38 os.mkdir(bug_folder_name)
39 except OSError as error:
40 if error.errno == errno.EEXIST:
41 return
42
43 if descriptions:
44 description = bug.description
45 filename = os.path.join(os.getcwd(), bug_folder_name,
46 'Description.txt')
47 local_file = codecs.open(filename, encoding="utf-8", mode="w")
48 local_file.write(description)
49 local_file.close()
50
51 for attachment in bug.attachments:
52 f = attachment.data.open()
53 filename = os.path.join(os.getcwd(), bug_folder_name, f.filename)
54 local_file = open(filename, "w")
55 local_file.write(f.read())
56 f.close()
57 local_file.close()
58
59
60 def main():
61 parser = OptionParser('Usage: %prog [options] <bug numbers>')
62 parser.add_option('-d', '--duplicates', default=False,
63 action='store_true',
64 help='Download attachments from duplicates too')
65 parser.add_option('-p', '--package',
66 help='Download attachments from all bugs with an '
67 'open task for this Ubuntu source package')
68 parser.add_option('-P', '--project',
69 help='Download attachments from all bugs with a '
70 'open task for this project')
71 parser.add_option('-D', '--descriptions', default=False,
72 action='store_true',
73 help='Also download bug descriptions as Description.txt')
74
75 opts, args = parser.parse_args()
76 if len(args) < 1 and not opts.package and not opts.project:
77 parser.error('No bug numbers provided')
78 launchpad = config.get_launchpad("grab-attachments")
79
80 if opts.package:
81 ubuntu = launchpad.projects['ubuntu']
82 src_package = ubuntu.getSourcePackage(name=opts.package)
83 if src_package is None:
84 parser.error('Unable to find package %s' % opts.package)
85 for task in src_package.searchTasks():
86 args.append(task.bug.id)
87
88 if opts.project:
89 try:
90 project = launchpad.projects['%s' % opts.project]
91 except KeyError:
92 parser.error('Unable to find project %s' % opts.project)
93 for task in project.searchTasks():
94 args.append(task.bug.id)
95
96 for arg in args:
97 try:
98 bug_number = int(arg)
99 except ValueError:
100 parser.error("'%s' is not a valid bug number." % arg)
101
102 bug = launchpad.bugs[bug_number]
103 download_attachments(bug, opts.descriptions)
104
105 if opts.duplicates is True:
106 for bug in bug.duplicates:
107 download_attachments(bug, opts.descriptions)
108
109 if __name__ == '__main__':
110 main()