Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 examples given in http://yum.baseurl.org/wiki/YumCodeSnippets, but other than kludging around inside ipython and guessing at what each method does, there doesn't appear to be any actual documentation for the Python API for yum. It's apparently all tribal knowledge.

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

What I need to do:

I have a deployment configuration script that relies on other system packages (make, gcc, etc.). I know I can install them like this: http://yum.baseurl.org/wiki/YumCodeSnippet/SimplestTransaction but I'd like to have the option to query if they're already installed before doing so, so I can have the additional option of simply failing if the packages aren't present instead of forcing installation. What's the proper call to do this (or better, has anyone actually bothered to properly document the API outside of code samples?)

I've never touched Python prior to this project, and I'm really liking it, but... some of the module documentation is more elusive than unicorn-riding leprechauns.

like image 542
Aaron Burke Avatar asked Dec 08 '11 23:12

Aaron Burke


People also ask

How do I know if a package is installed Linux?

The dpkg-query command can be used to show if a specific package is installed in your system. To do it, run dpkg-query followed by the -l flag and the name of the package you want information about.


2 Answers

import yum

yb = yum.YumBase()
if yb.rpmdb.searchNevra(name='make'):
   print "installed"
else:
   print "not installed"
like image 93
jfs Avatar answered Oct 29 '22 07:10

jfs


You could run 'which' on the subsystem to see if the system has the binaries you are looking for:

import os
os.system("which gcc")
os.system("which obscurepackagenotgoingtobefound")
like image 27
eclipse Avatar answered Oct 29 '22 07:10

eclipse