[yum-commits] Makefile yum-utils.spec yumdb.py

James Antill james at osuosl.org
Wed Apr 1 19:53:31 UTC 2009


 Makefile       |    2 
 yum-utils.spec |    1 
 yumdb.py       |  139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+), 1 deletion(-)

New commits:
commit ae4d9299e661004f620c3b2bc35feee807238965
Author: James Antill <james at and.org>
Date:   Wed Apr 1 15:16:38 2009 -0400

    Add yumdb command to look at and alter the yumdb

diff --git a/Makefile b/Makefile
index 038f05b..bb65bf6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 SUBDIRS = docs
 PKGNAME = yum-utils
 UTILS = package-cleanup debuginfo-install repoclosure repomanage repoquery repo-graph repo-rss yumdownloader yum-builddep repotrack reposync repodiff yum-debug-dump yum-debug-restore verifytree yum-groups-manager find-repos-of-install
-UTILSROOT = yum-complete-transaction 
+UTILSROOT = yum-complete-transaction yumdb
 VERSION=$(shell awk '/Version:/ { print $$2 }' ${PKGNAME}.spec)
 RELEASE=$(shell awk -F%: '/Release:/ { print $$2 }' ${PKGNAME}.spec ')
 SRPM_RELEASE=$(shell awk '/Release:/ { split($$2,a,"%"); print a[1] }' ${PKGNAME}.spec )
diff --git a/yum-utils.spec b/yum-utils.spec
index b2b126d..9212013 100644
--- a/yum-utils.spec
+++ b/yum-utils.spec
@@ -424,6 +424,7 @@ fi
 %{_bindir}/yum-groups-manager
 %{_bindir}/yum-debug-restore
 %{_sbindir}/yum-complete-transaction
+%{_sbindir}/yumdb
 %{_mandir}/man1/yum-utils.1.*
 %{_mandir}/man1/debuginfo-install.1.*
 %{_mandir}/man1/package-cleanup.1.*
diff --git a/yumdb.py b/yumdb.py
new file mode 100755
index 0000000..2274af7
--- /dev/null
+++ b/yumdb.py
@@ -0,0 +1,139 @@
+#!/usr/bin/python -tt
+
+import os
+import sys
+import re
+import string
+import optparse
+import fnmatch
+
+import yum
+import shlex
+
+def setup_opts():
+    version = "0.0.1"
+    vers_txt = "Manage yum groups metadata version %s" % version
+    usage_txt = """\
+%prog <command> ...
+      get       <key> [pkg-wildcard]...
+      set       <key> <value> [pkg-wildcard]...
+      del       <key> [pkg-wildcard]...
+      search    <key> <wildcard>...
+      exist?    <key> [pkg-wildcard]...
+      unset?    <key> [pkg-wildcard]...
+      info      [pkg-wildcard]...
+      shell     [filename]...
+"""
+    parser =  optparse.OptionParser(usage = usage_txt, version = vers_txt)
+
+    parser.add_option("--noplugins", action="store_false", default=True,
+                      dest="plugins",
+                      help="disable yum plugin support")
+    parser.add_option("-c", "--config",
+                      dest="conffile", help="config file location")
+
+    return parser
+
+def run_cmd(yb, args, inshell=False):
+    if False: pass
+    elif args[0] == 'get' and len(args) > 1:
+        args.pop(0)
+        ykey = args.pop(0)
+        for pkg in sorted(yb.rpmdb.returnPackages(patterns=args)):
+            print pkg
+            if ykey in pkg.yumdb_info:
+                print " " * 4, ykey, '=', getattr(pkg.yumdb_info, ykey)
+            else:
+                print " " * 4, ykey, '<unset>'
+    elif args[0] == 'set' and len(args) > 2:
+        args.pop(0)
+        ykey = args.pop(0)
+        yval = args.pop(0)
+        for pkg in sorted(yb.rpmdb.returnPackages(patterns=args)):
+            setattr(pkg.yumdb_info, ykey, yval)
+            print pkg
+            print " " * 4, ykey, '=', getattr(pkg.yumdb_info, ykey)
+    elif args[0] in ['del', 'delete', 'rm', 'remove'] and len(args) > 1:
+        args.pop(0)
+        ykey = args.pop(0)
+        for pkg in sorted(yb.rpmdb.returnPackages(patterns=args)):
+            if ykey in pkg.yumdb_info:
+                delattr(pkg.yumdb_info, ykey)
+            print pkg
+            print " " * 4, ykey, '<unset>'
+    elif args[0] == 'search' and len(args) > 2:
+        args.pop(0)
+        ykey = args.pop(0)
+        # Maybe need some API so we don't have to load everything?
+        for pkg in sorted(yb.rpmdb.returnPackages()):
+            if ykey not in pkg.yumdb_info:
+                continue
+            found = False
+            yval = getattr(pkg.yumdb_info, ykey)
+            for arg in args:
+                if fnmatch.fnmatch(yval, arg):
+                    found = True
+                    break
+            if not found:
+                continue
+            print pkg
+            print " " * 4, ykey, '=', yval
+    elif args[0] in ['exist?', 'exist', 'exists'] and len(args) > 1:
+        args.pop(0)
+        ykey = args.pop(0)
+        for pkg in sorted(yb.rpmdb.returnPackages(patterns=args)):
+            if ykey not in pkg.yumdb_info:
+                continue
+            print pkg
+    elif args[0] in ['unset?', 'unset'] and len(args) > 1:
+        args.pop(0)
+        ykey = args.pop(0)
+        for pkg in sorted(yb.rpmdb.returnPackages(patterns=args)):
+            if ykey in pkg.yumdb_info:
+                continue
+            print pkg
+    elif args[0] == 'info':
+        args.pop(0)
+        for pkg in sorted(yb.rpmdb.returnPackages(patterns=args)):
+            print pkg
+            for ykey in pkg.yumdb_info:
+                print " " * 4, ykey, '=', getattr(pkg.yumdb_info, ykey)
+    elif args[0] == 'shell' and not inshell:
+        args.pop(0)
+        if args:
+            fos = []
+            for arg in args:
+                fos.append(open(arg))
+        else:
+            fos = [sys.stdin]
+        for fo in fos:
+            print "=" * 79
+            for line in fo:
+                run_cmd(yb, shlex.split(line), inshell=True)
+                print "-" * 79
+    else:
+        print >>sys.stderr, parser.format_help()
+        sys.exit(1)
+
+def main():
+    global parser
+
+    parser = setup_opts()
+    (opts, args) = parser.parse_args()
+
+    yb = yum.YumBase()
+    if opts.conffile:
+        yb.preconf.fn = opts.conffile
+    if not opts.plugins:
+        yb.preconf.init_plugins = False
+    yb.conf
+
+    if len(args) < 1:
+        print >>sys.stderr, parser.format_help()
+        sys.exit(1)
+
+    run_cmd(yb, args)
+
+
+if __name__ == '__main__':
+    main()


More information about the Yum-commits mailing list