-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add real-life examples of Python RPM
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |