diff --git a/python/examples/rpm-e.py b/python/examples/rpm-e.py new file mode 100755 index 0000000000..4a6bc21f4e --- /dev/null +++ b/python/examples/rpm-e.py @@ -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, "") diff --git a/python/examples/rpm-i.py b/python/examples/rpm-i.py new file mode 100755 index 0000000000..33e346a33b --- /dev/null +++ b/python/examples/rpm-i.py @@ -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, "") diff --git a/python/examples/rpm-q.py b/python/examples/rpm-q.py new file mode 100755 index 0000000000..bf7b8cd3c5 --- /dev/null +++ b/python/examples/rpm-q.py @@ -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]) diff --git a/python/examples/rpm-qa.py b/python/examples/rpm-qa.py new file mode 100755 index 0000000000..518142cf95 --- /dev/null +++ b/python/examples/rpm-qa.py @@ -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]) diff --git a/python/examples/rpm-qi.py b/python/examples/rpm-qi.py new file mode 100755 index 0000000000..4f8458ab4f --- /dev/null +++ b/python/examples/rpm-qi.py @@ -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" + )