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

Subscribers

People subscribed via source and target branches