Determine if a package is installed with the Python Yum API?

TL; DR ; I need a simple Python call with the package name (for example, "make") to find out if it is installed; if not, install it (I can do the last part).

Problem:

So, there are a few code examples provided at http://yum.baseurl.org/wiki/YumCodeSnippets , but besides the fact that they are hidden inside ipython and guess what each method does, it doesn’t appear to be any actual documentation for the Python API for yum. Apparently, this is all tribal knowledge.

[edit] Apparently, I accidentally discovered the API documentation (after receiving an acceptable answer, of course). It is not linked to the homepage, but here it is for future reference: http://yum.baseurl.org/api/yum/

What do I need to do:

I have a script deployment configuration that relies on other system packages (make, gcc, etc.). I know that I can install them as follows: http://yum.baseurl.org/wiki/YumCodeSnippet/SimplestTransaction , but I would like to be able to request if they are already installed before that, so I can have the additional possibility of just crashing, if packages are not present, and do not force installation. What is the proper call for this (or, better, did anyone really bother to correctly document the API outside of the code samples?)

I never touched Python before this project, and I really like it, but ... some of the module documentation is more elusive than swans with a unicorn.

+8
python linux rpm yum
source share
3 answers
import yum yb = yum.YumBase() if yb.rpmdb.searchNevra(name='make'): print "installed" else: print "not installed" 
+17
source share

You can run "which" in the subsystem to find out if the system has the binaries you are looking for:

 import os os.system("which gcc") os.system("which obscurepackagenotgoingtobefound") 
+1
source share

For those who stumbled upon this post later, this is what I came up with. Note that "testing" and "skip_install" are flags that I parse from a script call.

  print "Checking for prerequisites (Apache, PHP + PHP development, autoconf, make, gcc)" prereqs = list("httpd", "php", "php-devel", "autoconf", "make", "gcc") missing_packages = set() for package in prereqs: print "Checking for {0}... ".format([package]), # Search the RPM database to check if the package is installed res = yb.rpmdb.searchNevra(name=package) if res: for pkg in res: print pkg, "installed!" else: missing_packages.add(package) print package, "not installed!" # Install the package if missing if not skip_install: if testing: print "TEST- mock install ", package else: try: yb.install(name=package) except yum.Errors.InstallError, err: print >> sys.stderr, "Failed during install of {0} package!".format(package) print >> sys.stderr, str(err) sys.exit(1) # Done processing all package requirements, resolve dependencies and finalize transaction if len(missing_packages) > 0: if skip_install: # Package not installed and set to not install, so fail print >> sys.stderr, "Please install the {0} packages and try again.".format( ",".join(str(name) for name in missing_packages)) sys.exit(1) else: if testing: print "TEST- mock resolve deps and process transaction" else: yb.resolveDeps() yb.processTransaction() 
+1
source share

All Articles