Skip to content

Commit 57f00f2

Browse files
committed
bugzilla: Improve error reporting for adding attachments
1 parent 9d372a9 commit 57f00f2

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

bin/bugzilla

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,6 @@ def _do_set_attach(bz, opt, parser, args):
796796
if not args:
797797
parser.error("Bug ID must be specified for setting attachments")
798798

799-
if not opt.file and opt.desc:
800-
parser.error("attaching a file requires --file and --description")
801-
802799
if not opt.type:
803800
try:
804801
import magic
@@ -808,29 +805,39 @@ def _do_set_attach(bz, opt, parser, args):
808805
mimemagic.load()
809806

810807
if sys.stdin.isatty():
811-
# stdin is a tty -> normal CLI
808+
if not opt.file:
809+
parser.error("--file must be specified")
812810
if not opt.type:
813811
opt.type = mimemagic.file(opt.file)
814812
fileobj = open(opt.file)
815-
816813
else:
817814
# piped input on stdin
818-
# write it to a tempfile - and get the filetype if we need it
815+
if not opt.desc:
816+
parser.error("--description must be specified if passing "
817+
"file on stdin")
818+
819819
fileobj = tempfile.NamedTemporaryFile(prefix="bugzilla-attach.")
820820
data = sys.stdin.read(4096)
821+
821822
if not opt.type:
822823
opt.type = mimemagic.buffer(data)
823824
while data:
824825
fileobj.write(data)
825826
data = sys.stdin.read(4096)
826827
fileobj.seek(0)
827828

829+
kwargs = {}
830+
if opt.file:
831+
kwargs["filename"] = os.path.basename(opt.file)
832+
if opt.type:
833+
kwargs["contenttype"] = opt.type
834+
if opt.type in ["text/x-patch"]:
835+
kwargs["ispatch"] = True
836+
desc = opt.desc or fileobj.name
837+
828838
# Upload attachments
829839
for bugid in args:
830-
attid = bz.attachfile(bugid, fileobj, opt.desc,
831-
filename=opt.file,
832-
contenttype=opt.type,
833-
ispatch=(opt.type == "text/x-patch"))
840+
attid = bz.attachfile(bugid, fileobj, desc, **kwargs)
834841
print "Created attachment %i on bug %s" % (attid, bugid)
835842

836843

bugzilla/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def attachfile(self, idlist, attachfile, description, **kwargs):
886886
kwargs['data'] = xmlrpclib.Binary(f.read())
887887
kwargs['ids'] = self._listify(idlist)
888888

889-
if 'file_name' not in kwargs:
889+
if 'file_name' not in kwargs and hasattr(f, "name"):
890890
kwargs['file_name'] = os.path.basename(f.name)
891891
if 'content_type' not in kwargs:
892892
kwargs['content_type'] = 'application/octet-stream'

0 commit comments

Comments
 (0)