Skip to content

Commit

Permalink
Add real-life examples of Python RPM
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyX committed Aug 7, 2024
1 parent e8a252d commit 04f5156
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
20 changes: 20 additions & 0 deletions python/examples/rpm-e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/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):
pass


ts = rpm.TransactionSet()
for name in sys.argv[1:]:
ts.addErase(name)

callback = Callback()
ts.run(callback.callback, "")
34 changes: 34 additions & 0 deletions python/examples/rpm-i.py
Original file line number Diff line number Diff line change
@@ -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), "i")

callback = Callback()
ts.run(callback.callback, "")
22 changes: 22 additions & 0 deletions python/examples/rpm-q.py
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions python/examples/rpm-qa.py
Original file line number Diff line number Diff line change
@@ -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 changes: 41 additions & 0 deletions python/examples/rpm-qi.py
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 04f5156

Please sign in to comment.