@@ -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
0 commit comments