Debian Codegraph @

bin/lp-attach

raw
1 #! /usr/bin/python3
2 #
3 # Copyright (C) 2010 Canonical Ltd
4
5 """lp-attach Attach a file to a Launchpad bug
6
7 usage: lp_attach BUGNUMBER
8
9 Attaches 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
31 import sys
32 from lptools import config
33
34
35 def guess_mime_type(attachment_bytes):
36 try:
37 import magic
38 except ImportError as 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
50 def 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
81 if __name__ == '__main__':
82 sys.exit(main(sys.argv))