2

Pythonic way to check list of packages installed in Centos/Redhat?

In a bash script, I'd do:

 rpm -qa | grep -w packagename
jww
  • 83,594
  • 69
  • 338
  • 732
Savio Mathew
  • 563
  • 1
  • 5
  • 13
  • can do same in python using system command. – Marcin Jan 08 '15 at 05:43
  • Is this the same question? http://stackoverflow.com/questions/739993/how-can-i-get-a-list-of-locally-installed-python-modules – zehnpaard Jan 08 '15 at 06:20
  • rpm seems to have python bindings provided via `rpm-python`: http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch16s03s04.html (That's what I found, haven't used it..) – sebastian Jan 08 '15 at 07:49
  • The last time I tried to use the Python RPM API, I found almost no documentation for it. (I see the link posted by @sebastian is a start.) It's not a simple API and reverse-engineering it would take some time. It almost seems to be treated by Fedora/RH as an internal product. – Scott Lowrey Jan 09 '15 at 22:15

4 Answers4

2
import sys
import rpm

ts = rpm.TransactionSet()
mi = ts.dbMatch( 'name', sys.argv[1] )
try :
    h = mi.next()
    print "%s-%s-%s" % (h['name'], h['version'], h['release'])
except StopIteration:
    print "Package not found"
  1. TransactionSet() will open the RPM database
  2. dbMatch with no paramters will set up a match iterator to go over the entire set of installed packages, you can call next on the match iterator to get the next entry, a header object that represents one package
  3. dbMatch can also be used to query specific packages, you need to pass the name of a tag, as well as the value for that tag that you are looking for:

    dbMatch('name','mysql')
    
jww
  • 83,594
  • 69
  • 338
  • 732
0

you can use Subprocess:

import subprocess
child = subprocess.Popen("rpm -qa | grep -w packagename", stdout=subprocess.PIPE, shell=True)
output = child.communicate()[0]
print output

using os:

import os
os.system("rpm -qa | grep -w packagename")
Hackaholic
  • 15,927
  • 3
  • 44
  • 57
0
import os

present=0
notpresent=0
f3=open('INSTALLED.log','w')
f2=open('NOTINSTALLED.log','w')

f1=open('installed_packagelist.log','w')

var = os.popen("rpm -qa --queryformat '[%{NAME}\n]'").read()
f1.write(var)


lines = [line.rstrip('\n') for line in open('installed_packagelist.log')]

for index in range(len(lines)):
 contents = lines[index]
 test_str = "rpm -V " + contents
 var = os.system(test_str)
 if (var == 0):
  print contents + "file present"
  present = present +1
  f3.write(contents)

 else:
  print contents + "file not present"
  notpresent = notpresent + 1
  f2.write(contents)  

print present
print notpresent

f2.close()
f3.close()

f3=open('INSTALLED.log','r')
f2=open('NOTINSTALLED.log','r')


data=f3.read()
print data

print       "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

data=f2.read()
print data
  • Please read [How to answer](http://stackoverflow.com/help/how-to-answer) topic. You should at least provide some description, not just some piece of code. – Alex M Aug 02 '16 at 11:34
0

I could not get this answer: https://stackoverflow.com/a/51258124/498657 to work on Python 3.6.8, what did work for me was:

import sys
import rpm

ts = rpm.TransactionSet()
mi = ts.dbMatch( 'name', 'lsof' )

rpmhit=0
for h in mi:
    if h['name'] == 'lsof':
        rpmhit=1
        break

if rpmhit == 0:
    print('Error: Package lsof not installed. Install using: dnf install lsof')
    sys.exit(3)
Open Grieves
  • 301
  • 1
  • 3
  • 6