Determine If Package Installed With Yum Python Api?
TLDR; I need simple a Python call given a package name (e.g., 'make') to see if it's installed; if not, install it (I can do the latter part). Problem: So there are a few code exam
Solution 1:
import yum
yb = yum.YumBase()
if yb.rpmdb.searchNevra(name='make'):
print"installed"else:
print"not installed"
Solution 2:
You could run 'which' on the subsystem to see if the system has the binaries you are looking for:
import osos.system("which gcc")
os.system("which obscurepackagenotgoingtobefound")
Solution 3:
For anyone who stumbles across this post later, here's what I came up with. Note that "testing" and "skip_install" are flags that I parse from the script invocation.
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 missingifnot 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 transactioniflen(missing_packages) > 0:
if skip_install:
# Package not installed and set to not install, so failprint >> 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()
Solution 4:
importyumyb= yum.YumBase()
yb.isPackageInstalled('make')
Post a Comment for "Determine If Package Installed With Yum Python Api?"