Add real-life examples of Python RPM

See https://github.com/rpm-software-management/rpm-web/pull/54
This commit is contained in:
Jakub Kadlcik 2024-06-21 12:01:16 +02:00 committed by Panu Matilainen
parent f4e6aeccea
commit e927be7e19
5 changed files with 138 additions and 0 deletions

24
python/examples/rpm-e.py Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/python3
# Uninstall a package from the system.
# A Python equivalent for `rpm -e hello`
import sys
import rpm
class Callback:
def callback(self, what, amount, total, mydata, wibble):
# Transactions that only remove packages don't strictly require the
# the callback to do anything. Though, most operations require at least
# `rpm.RPMCALLBACK_INST_OPEN_FILE` and `rpm.RPMCALLBACK_INST_CLOSE_FILE`
# to be handled. See `rpm-i.py` example for more information.
pass
ts = rpm.TransactionSet()
for name in sys.argv[1:]:
ts.addErase(name)
callback = Callback()
ts.run(callback.callback, "")

34
python/examples/rpm-i.py Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/python3
# Install a package from a file stored on your system.
# A Python equivalent for `rpm -i hello-2.12.1-4.fc40.x86_64.rpm`
import os
import sys
import rpm
class Callback:
def __init__(self):
self.fdnos = {}
def callback(self, what, amount, total, mydata, wibble):
if what == rpm.RPMCALLBACK_INST_OPEN_FILE:
nvr, path = mydata
fd = os.open(path, os.O_RDONLY)
self.fdnos[nvr] = fd
return fd
elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE:
nvr, path = mydata
os.close(self.fdnos[nvr])
ts = rpm.TransactionSet()
for path in sys.argv[1:]:
with open(path, "r") as fp:
hdr = ts.hdrFromFdno(fp.fileno())
ts.addInstall(hdr, (hdr.nvr, path), "u")
callback = Callback()
ts.run(callback.callback, "")

22
python/examples/rpm-q.py Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/python
# Query one or more installed packages by their names.
# A Python equivalent for `rpm -q hello`
import sys
import rpm
if len(sys.argv) == 1:
print("rpm: no arguments given for query")
sys.exit(1)
ts = rpm.TransactionSet()
for name in sys.argv[1:]:
mi = ts.dbMatch("name", name)
if not mi:
print("package {0} is not installed".format(name))
continue
# Multiple packages can have the same name
for hdr in mi:
print(hdr[rpm.RPMTAG_NVRA])

17
python/examples/rpm-qa.py Executable file
View File

@ -0,0 +1,17 @@
#!/usr/bin/python3
# Query all packages that match part of their name with the searched string.
# If package name isn't specified, query all installed packages.
# A Python equivalent for `rpm -qa kernel*` and `rpm -qa`
import sys
import rpm
ts = rpm.TransactionSet()
mi = ts.dbMatch()
for name in sys.argv[1:]:
mi.pattern("name", rpm.RPMMIRE_GLOB, name)
for hdr in mi:
print(hdr[rpm.RPMTAG_NVRA])

41
python/examples/rpm-qi.py Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/python3
# Query one or more installed packages by their names but this time print more
# information.
# A Python equivalent for `rpm -qi hello`
import sys
import rpm
if len(sys.argv) == 1:
print("rpm: no arguments given for query")
sys.exit(1)
ts = rpm.TransactionSet()
for name in sys.argv[1:]:
mi = ts.dbMatch("name", name)
if not mi:
print("package {0} is not installed".format(name))
# Multiple packages can have the same name
for hdr in mi:
print(
f"Name : {hdr[rpm.RPMTAG_NAME]}\n"
f"Version : {hdr[rpm.RPMTAG_VERSION]}\n"
f"Release : {hdr[rpm.RPMTAG_RELEASE]}\n"
f"Architecture: {hdr[rpm.RPMTAG_ARCH]}\n"
f"Install Date: {hdr[rpm.RPMTAG_INSTALLTIME]}\n"
f"Group : {hdr[rpm.RPMTAG_GROUP]}\n"
f"Size : {hdr[rpm.RPMTAG_SIZE]}\n"
f"License : {hdr[rpm.RPMTAG_LICENSE]}\n"
f"Signature : {hdr.format("%{rsaheader:pgpsig}")}\n"
f"Source RPM : {hdr[rpm.RPMTAG_SOURCERPM]}\n"
f"Build Date : {hdr[rpm.RPMTAG_BUILDTIME]}\n"
f"Build Host : {hdr[rpm.RPMTAG_BUILDHOST]}\n"
f"Packager : {hdr[rpm.RPMTAG_PACKAGER]}\n"
f"Vendor : {hdr[rpm.RPMTAG_VENDOR]}\n"
f"URL : {hdr[rpm.RPMTAG_URL]}\n"
f"Bug URL : {hdr[rpm.RPMTAG_BUGURL]}\n"
f"Summary : {hdr[rpm.RPMTAG_SUMMARY]}\n"
f"Description :\n{hdr[rpm.RPMTAG_DESCRIPTION]}\n"
)