@@ -475,34 +475,30 @@ def do_query(bz, opt, parser):
475475 setattr (opt , optname , val .split ("," ))
476476
477477 include_fields = None
478- # To optimize speed and reduce network traffic through lookups, we
479- # specifically tell bugzilla the exact data we want. This allows us
480- # make one call to output the data, rather than have a follow-on
481- # getbug() to grab more info than what the default has.
482- # Testing has shown this to be a _huge_ time saver. Unfortunately,
483- # this list and the output format have to be in _sync_. Otherwise,
484- # you lose speed by doing a look-up for each new output element.
485- if opt .output == 'oneline' :
486- include_fields = ['bug_id' , 'bug_status' , 'assigned_to' ,
487- 'component' , 'target_milestone' , 'short_desc' , 'flags' ,
488- 'keywords' , 'blockedby' ]
489-
490- elif opt .output == 'normal' :
491- include_fields = ['bug_id' , 'bug_status' , 'assigned_to' ,
492- 'short_desc' ]
493-
494- elif opt .output == 'raw' :
478+ if opt .output == 'raw' :
495479 # 'raw' always does a getbug() call anyways, so just ask for ID back
496480 include_fields = ['id' ]
497481
498- if opt .outputformat :
482+ elif opt .outputformat :
499483 include_fields = []
500484 for fieldname , rest in format_field_re .findall (opt .outputformat ):
501485 if fieldname == "whiteboard" and rest :
502486 fieldname = rest + "_" + fieldname
503487 elif fieldname == "flag" :
504488 fieldname = "flags"
505- include_fields .append (fieldname )
489+ elif fieldname == "cve" :
490+ fieldname = ["keywords" , "blocks" ]
491+ elif fieldname == "__unicode__" :
492+ # Needs to be in sync with bug.__unicode__
493+ fieldname = ["id" , "status" , "assigned_to" , "summary" ]
494+
495+ flist = type (fieldname ) is list and fieldname or [fieldname ]
496+ for f in flist :
497+ if f not in include_fields :
498+ include_fields .append (f )
499+
500+ if include_fields is not None :
501+ include_fields .sort ()
506502
507503 built_query = bz .build_query (
508504 product = getattr (opt , "product" , None ),
@@ -588,113 +584,102 @@ def _do_info(bz, opt):
588584 break
589585
590586
591- def _format_output (bz , opt , buglist ):
592- if opt .outputformat :
593- def bug_field (matchobj ):
594- # whiteboard and flag allow doing
595- # %{whiteboard:devel} and %{flag:needinfo}
596- # That's what 'rest' matches
597- (fieldname , rest ) = matchobj .groups ()
587+ def _convert_to_outputformat (output ):
588+ fmt = ""
598589
599- if fieldname == "whiteboard" and rest :
600- fieldname = rest + "_" + fieldname
590+ if output == "normal" :
591+ fmt = "%{__unicode__}"
601592
602- if fieldname == "flag" :
603- val = b .get_flag_status (rest )
604- else :
605- val = getattr (b , fieldname , "" )
593+ elif output == "ids" :
594+ fmt = "%{id}"
606595
607- if type (val ) is list :
608- val = ',' .join (val )
596+ elif output == 'full' :
597+ fmt += "%{__unicode__}\n "
598+ fmt += "CC: %{cc}\n "
599+ fmt += "Blocked: %{blocks}\n "
600+ fmt += "Depends: %{depends_on}\n "
601+ fmt += "%{comments}\n "
609602
610- return str (val )
603+ elif output == 'extra' :
604+ fmt += "%{__unicode__}\n "
605+ fmt += " +Keywords: %{keywords}\n "
606+ fmt += " +QA Whiteboard: %{qa_whiteboard}\n "
607+ fmt += " +Status Whiteboard: %{status_whiteboard}\n "
608+ fmt += " +Devel Whiteboard: %{devel_whiteboard}\n "
611609
612- for b in buglist :
613- print format_field_re .sub (bug_field , opt .outputformat )
610+ elif output == 'oneline' :
611+ fmt += "#%{bug_id} %{status} %{assigned_to} %{component}\t "
612+ fmt += "[%{target_milestone}] %{flags} %{cve}"
614613
615- elif opt .output == 'ids' :
616- for b in buglist :
617- print b .bug_id
618-
619- elif opt .output == 'full' :
620- fullbuglist = bz .getbugs ([b .bug_id for b in buglist ])
621- for b in fullbuglist :
622- print b
623-
624- if hasattr (b , "cc" ):
625- print "CC: %s" % " " .join (b .cc )
626- if hasattr (b , "blocked" ):
627- print "Blocked: %s" % " " .join ([str (i )
628- for i in b .blocked or []])
629- if hasattr (b , "dependson" ):
630- print ("Depends: %s" %
631- " " .join ([str (i ) for i in b .dependson or []]))
632-
633- for c in getattr (b , "longdescs" , []):
634- print to_encoding (u"* %s - %s:\n %s\n " % (c ['time' ],
635- c ['author' ], c ['text' ]))
636-
637- elif opt .output == 'normal' :
638- for b in buglist :
639- print b
640-
641- elif opt .output == 'extra' :
642- print "Grabbing 'extra' bug information. This could take a moment."
643- fullbuglist = bz .getbugs ([b .bug_id for b in buglist ])
644- for b in fullbuglist :
645- print b
646- if hasattr (b , 'keywords' ) and b .keywords :
647- print to_encoding (u" +Keywords: %s" % b .keywords )
648- if hasattr (b , 'qa_whiteboard' ) and b .qa_whiteboard :
649- print to_encoding (u" +QA Whiteboard: %s" % b .qa_whiteboard )
650- if hasattr (b , 'status_whiteboard' ) and b .status_whiteboard :
651- print to_encoding (u" +Status Whiteboard: %s" %
652- b .status_whiteboard )
653- if hasattr (b , 'devel_whiteboard' ) and b .devel_whiteboard :
654- print to_encoding (u" +Devel Whiteboard: %s" %
655- b .devel_whiteboard )
656- print "\n Bugs listed: " , len (buglist )
657-
658- elif opt .output == 'oneline' :
614+ else :
615+ raise RuntimeError ("Unknown output type '%s'" % opt .output )
616+
617+ return fmt
618+
619+
620+ def _format_output (bz , opt , buglist ):
621+ if opt .output == 'raw' :
622+ buglist = bz .getbugs ([b .bug_id for b in buglist ])
659623 for b in buglist :
660- cve = ""
661- flags = ""
662- if hasattr (b , "flags" ):
663- for flag in b .flags :
664- flags = " " .join ([f ["name" ] + f ["status" ]
665- for f in b .flags ])
666-
667- keywords = getattr (b , "keywords" , "" )
668- if type (keywords ) is list :
669- keywords = " " .join (keywords )
670-
671- # grab CVE from keywords and blockers
672- if keywords .find ("Security" ) != - 1 and b .blockedby :
673- for bl in str (b .blockedby ).split (',' ):
674- cvebug = bz .getbug (bl )
675- for cb in cvebug .alias :
676- if cb .find ("CVE" ) != - 1 :
677- cve += cb + " "
678-
679- # bugzilla.redhat.com has component as a list
680- if type (b .component ) == list :
681- b .component = ',' .join (b .component )
682- print to_encoding (u"#%s %8s %22s %s\t [%s] %s %s" %
683- (b .bug_id , b .bug_status , b .assigned_to , b .component ,
684- b .target_milestone , flags , cve ))
685-
686- elif opt .output == 'raw' :
687- fullbuglist = bz .getbugs ([b .bug_id for b in buglist ])
688- for b in fullbuglist :
689624 print "Bugzilla %s: " % b .bug_id
690625 for a in dir (b ):
691626 if a .startswith ("__" ) and a .endswith ("__" ):
692627 continue
693628 print to_encoding (u"ATTRIBUTE[%s]: %s" % (a , getattr (b , a )))
694629 print "\n \n "
630+ return
695631
696- else :
697- raise RuntimeError ("Unknown output type '%s'" % opt .output )
632+ def bug_field (matchobj ):
633+ # whiteboard and flag allow doing
634+ # %{whiteboard:devel} and %{flag:needinfo}
635+ # That's what 'rest' matches
636+ (fieldname , rest ) = matchobj .groups ()
637+
638+ if fieldname == "whiteboard" and rest :
639+ fieldname = rest + "_" + fieldname
640+
641+ if fieldname == "flag" and rest :
642+ val = b .get_flag_status (rest )
643+
644+ elif fieldname == "flags" :
645+ val = "," .join ([f ["name" ] + f ["status" ]
646+ for f in getattr (b , "flags" , [])])
647+
648+ elif fieldname == "cve" :
649+ cves = []
650+ for key in getattr (b , "keywords" , []):
651+ # grab CVE from keywords and blockers
652+ if key .find ("Security" ) == - 1 :
653+ continue
654+ for bl in b .blocks :
655+ cvebug = bz .getbug (bl )
656+ for cb in cvebug .alias :
657+ if cb .find ("CVE" ) == - 1 :
658+ continue
659+ if cb .strip () not in cves :
660+ cves .append (cb )
661+ val = "," .join (cves )
662+
663+ elif fieldname == "comments" :
664+ val = ""
665+ for c in getattr (b , "comments" , []):
666+ val += ("\n * %s - %s:\n %s\n " %
667+ (c ['time' ], c ['author' ], c ['text' ]))
668+
669+ elif fieldname == "__unicode__" :
670+ val = unicode (b )
671+ else :
672+ val = getattr (b , fieldname , "" )
673+
674+ if type (val ) is list :
675+ val = ',' .join (val )
676+ elif type (val ) is int :
677+ val = str (val )
678+
679+ return to_encoding (val )
680+
681+ for b in buglist :
682+ print format_field_re .sub (bug_field , opt .outputformat )
698683
699684
700685def _do_new (bz , opt ):
@@ -914,6 +899,10 @@ def main(bzinstance=None):
914899 # Run the actual commands #
915900 ###########################
916901
902+ if hasattr (opt , "outputformat" ):
903+ if not opt .outputformat and opt .output not in ['raw' , None ]:
904+ opt .outputformat = _convert_to_outputformat (opt .output )
905+
917906 buglist = []
918907 if action == 'info' :
919908 if args :
0 commit comments