Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about rpm module in python

Tags:

python

rpm

I looked for some informations,the rpm module can only use for search installed rpm package Information.I want to use the python rpm module to search *.rpm files in the folder and know their information,like release or version.Is this possible using the rpm module?

like image 348
Mouse Avatar asked Dec 12 '25 14:12

Mouse


1 Answers

In case someone still ends up here while searching for an answer, here is how you can do it with python-rpm:

import os
import rpm

fdno = os.open(PATH_TO_RPM_FILE, os.O_RDONLY)
ts = rpm.ts()
hdr = ts.hdrFromFdno(fdno)
os.close(fdno)

(Note the call to os.close())

Now hdr holds the RPM header information. You can access individual attributes in dict style using RPMTAG_* values as keys, for example:

arch = hdr[rpm.RPMTAG_ARCH]

You can try reverse engineering all possible RPMTAG_* values using dir():

import rpm
print '\n'.join(filter(lambda x: x.startswith('RPMTAG'), dir(rpm)))

You can also call keys() on hdr, but it will return the possible keys as integers, which might not be as friendly.

I have found that using python-rpm instead of invoking the command line tool as a subprocess gives a significant performance boost when a large number of RPM files are to be handled.

For further information, see http://rpm5.org/docs/api/classRpmhdr.html

like image 141
Antti Kajander Avatar answered Dec 14 '25 08:12

Antti Kajander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!