#!/usr/bin/python # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # by James Antill from yum.plugins import TYPE_INTERACTIVE, PluginYumExit import fnmatch import yum.logginglevels requires_api_version = '2.1' plugin_type = (TYPE_INTERACTIVE,) class PkgTagsCommand: def __init__(self, cmd='pkgtags'): self.cmd = cmd def getNames(self): return [self.cmd] def getUsage(self): return "pkgtags [list|info|search-names|search-tags|search-app|summary]" def getSummary(self): return "Shows pkgtags" def doCheck(self, base, basecmd, extcmds): pass def doCommand(self, base, basecmd, extcmds): ret = None vcmd = 'summary' if extcmds: vcmd = extcmds[0] extcmds = extcmds[1:] def _get_all(): # This is really hacky... search for every letter... ret = {} for let in 'abcdefghijklmnopqrstuvwxyz': res = base.pkgtags.search_names(let) ret.update(res) return ret if False: pass elif vcmd == 'summary': all_tags = set() res = _get_all() num = 0 hist = {} for name in res: n = len(res[name]) num += n if n not in hist: hist[n] = 0 hist[n] += 1 all_tags.update(res[name]) print " Package Names with a tag :", "%4u" % len(res) print " Package Tags total :", "%4u" % len(all_tags) if len(res): print " Package Tags Ave. :", "%4.2f" % (float(num) / len(res)) if base.verbose_logger.isEnabledFor(yum.logginglevels.DEBUG_3): print "" print " Histogram of number of tags per. pkg:" for n in sorted(hist): print n, "*" * hist[n] elif vcmd == 'list': all_tags = set() res = _get_all() for name in res: all_tags.update(res[name]) for tag in sorted(all_tags): if extcmds and not fnmatch.fnmatch(extcmds[0], tag): continue print " ", tag elif vcmd == 'info': all_tags = set() res = _get_all() for name in res: all_tags.update(res[name]) for tag in sorted(all_tags): if extcmds and not fnmatch.fnmatch(extcmds[0], tag): continue print " ", tag for name in sorted(res): if tag not in res[name]: continue print " ", name elif vcmd in ('search-apps', 'search-app'): if not extcmds: return 1, ['pkgtags %s: missing argument' % (vcmd,)] app_pkg_names = set() for pkg in sorted(base.searchPackageTags('Application')): app_pkg_names.add(pkg.name) self = base # Hack attack, so we can C&P args = extcmds dups = False okeys = set() akeys = set() # All keys, used to see if nothing matched mkeys = set() # "Main" set of keys for N/S search (biggest term. hit). pos = set() def _print_match_section(text): # Print them in the order they were passed used_keys = [arg for arg in args if arg in keys] print self.fmtSection(text % ", ".join(used_keys)) matching = self.searchGenerator(['name', 'summary'], args, showdups=dups, keys=True, searchtags=False) for (po, keys, matched_value) in matching: if po.name not in app_pkg_names: continue if keys != okeys: if akeys: if len(mkeys) == len(args): break print "" else: mkeys = set(keys) _print_match_section('N/S Matched: %s') okeys = keys pos.add(po) akeys.update(keys) self.matchcallback(po, matched_value, args) for arg in args: if arg not in akeys: self.logger.warning('Warning: No matches found for: %s', arg) if not akeys: return 0, ['No Matches found'] return 0, matching elif vcmd == 'search-tags': if not extcmds: return 1, ['pkgtags %s: missing argument' % (vcmd,)] print "Search tags reults for:", extcmds[0] for pkg in sorted(base.searchPackageTags(extcmds[0])): print " ", pkg elif vcmd == 'search-names': if not extcmds: return 1, ['pkgtags %s: missing argument' % (vcmd,)] print "Search name reults for:", extcmds[0] res = base.pkgtags.search_names(extcmds[0]) for name in sorted(res): print " ", name for tag in sorted(res[name]): print " ", tag else: return 1, ['pkgtags %s' % (vcmd,)] if ret is None: return 0, ['pkgtags %s' % (vcmd,)] return ret def needTs(self, base, basecmd, extcmds): return False def config_hook(conduit): conduit.registerCommand(PkgTagsCommand())