~ubuntu-branches/ubuntu/quantal/lptools/quantal

« back to all changes in this revision

Viewing changes to bin/lp-grab-attachments

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij, Robert Collins, Jelmer Vernooij
  • Date: 2011-09-01 22:47:11 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20110901224711-05u7hwne9anexqq9
Tags: 0.0.1~bzr28-1
[ Robert Collins ]
* Control tweaks.

[ Jelmer Vernooij ]
* New upstream snapshot.
 + Imports a few tools from ubuntu-dev-tools. Breaks/Replaces added.
* Add myself to Uploaders.
* Bump standards version to 3.9.2 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
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 errno
 
25
import os
 
26
 
 
27
from lptools import config
 
28
 
 
29
USAGE = "lp-grab-attachments <bug numbers>"
 
30
 
 
31
 
 
32
def download_attachments(bug):
 
33
 
 
34
    bug_folder_name = 'bug-%s' % bug.id
 
35
 
 
36
    try:
 
37
        os.mkdir(bug_folder_name)
 
38
    except OSError, error:
 
39
        if error.errno == errno.EEXIST:
 
40
            return
 
41
 
 
42
    for attachment in bug.attachments:
 
43
        f = attachment.data.open()
 
44
        filename = os.path.join(os.getcwd(), bug_folder_name, f.filename)
 
45
        local_file = open(filename, "w")
 
46
        local_file.write(f.read())
 
47
        f.close()
 
48
        local_file.close()
 
49
 
 
50
 
 
51
def main():
 
52
    parser = OptionParser('Usage: %prog [options] <bug numbers>')
 
53
    parser.add_option('-d', '--duplicates', default=False,
 
54
                      action='store_true',
 
55
                      help='Download attachments from duplicates too')
 
56
    parser.add_option('-p', '--package',
 
57
                      help='Download attachments from all bugs with a '
 
58
                           'task for this Ubuntu source package')
 
59
 
 
60
    opts, args = parser.parse_args()
 
61
    if len(args) < 1 and not opts.package:
 
62
        parser.error('No bug numbers provided')
 
63
    launchpad = config.get_launchpad("grab-attachments")
 
64
 
 
65
    if opts.package:
 
66
        ubuntu = launchpad.projects['ubuntu']
 
67
        src_package = ubuntu.getSourcePackage(name=opts.package)
 
68
        if src_package is None:
 
69
            parser.error('Unable to find package %s' % opts.package)
 
70
        for task in src_package.searchTasks():
 
71
            args.append(task.bug.id)
 
72
 
 
73
    for arg in args:
 
74
        try:
 
75
            bug_number = int(arg)
 
76
        except ValueError:
 
77
            parser.error("'%s' is not a valid bug number." % arg)
 
78
 
 
79
        bug = launchpad.bugs[bug_number]
 
80
        download_attachments(bug)
 
81
 
 
82
        if opts.duplicates is True:
 
83
            for bug in bug.duplicates:
 
84
                download_attachments(bug)
 
85
 
 
86
if __name__ == '__main__':
 
87
    main()