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