Merge lp:~jelmer/lptools/lp-attach into lp:lptools

Proposed by Jelmer Vernooij
Status: Merged
Approved by: dobey
Approved revision: 39
Merged at revision: 40
Proposed branch: lp:~jelmer/lptools/lp-attach
Merge into: lp:lptools
Diff against target: 86 lines (+82/-0)
1 file modified
bin/lp-attach (+82/-0)
To merge this branch: bzr merge lp:~jelmer/lptools/lp-attach
Reviewer Review Type Date Requested Status
dobey Approve
Review via email: mp+100285@code.launchpad.net

Commit message

Import lp-attach from hydrazine.

Description of the change

Import lp-attach tool from hydrazine.

This attaches files to bugs on Launchpad.

To post a comment you must log in.
Revision history for this message
dobey (dobey) wrote :

Looks ok to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'bin/lp-attach'
--- bin/lp-attach 1970-01-01 00:00:00 +0000
+++ bin/lp-attach 2012-03-31 13:34:19 +0000
@@ -0,0 +1,82 @@
1#! /usr/bin/python
2#
3# Copyright (C) 2010 Canonical Ltd
4
5"""lp-attach Attach a file to a Launchpad bug
6
7usage: lp_attach BUGNUMBER
8
9Attaches a file (read from stdin) as an attachment on a named Launchpad bug.
10"""
11
12# TODO: option to set the attachment name, and to set whether it's a patch
13#
14# TODO: option to open the bug after attaching
15#
16# TODO: option to add a comment as well as the attachment
17#
18# TODO: option to set the subject of the comment
19#
20# TODO: option to comment on a merge proposal or question rather than a bug
21#
22# TODO: option to add a comment rather than an attachment
23#
24# TODO: option to use staging -- for now use
25# export LAUNCHPAD_API=https://api.staging.launchpad.net/
26#
27# TODO: option to set mime type
28#
29# TODO: detect mime type if not set - could use python-magic
30
31import sys
32from lptools import config
33
34
35def guess_mime_type(attachment_bytes):
36 try:
37 import magic
38 except ImportError, e:
39 sys.stderr.write("can't guess mime-types without the python-magic library: %s" % e)
40 mimetype = None
41 else:
42 mime = magic.open(magic.MAGIC_MIME)
43 mimetype = mime.buffer(attachment_bytes)
44 if mimetype is None:
45 mimetype = 'application/binary'
46 print 'attachment type %s' % mimetype
47 return mimetype
48
49
50def main(argv):
51 if len(argv) != 2 or argv[1] == '--help':
52 print __doc__
53 return 3
54
55 try:
56 bugnumber = int(argv[1])
57 except TypeError:
58 sys.stderr.write("please give a Launchpad bug number\n")
59 return 1
60
61 lp = config.get_launchpad("attach")
62 print "getting bug %s" % bugnumber
63 bug = lp.bugs[bugnumber]
64 print 'Attaching to %s' % bug
65
66 attachment_bytes = sys.stdin.read()
67 print '%d bytes to attach' % len(attachment_bytes)
68
69 mime_type = guess_mime_type(attachment_bytes)
70
71 # mime type must be specified otherwise
72 # <https://bugs.edge.launchpad.net/malone/+bug/204560> assumes it's
73 # chemical/x-mopac-input
74 print bug.addAttachment(comment='',
75 data=attachment_bytes,
76 description='',
77 filename='attachment',
78 content_type=mime_type)
79
80
81if __name__ == '__main__':
82 sys.exit(main(sys.argv))

Subscribers

People subscribed via source and target branches