Adjust reported package number in python instead of shell (#26)

Create a data structure that holds the command
to list all packages, as well as a number to adjust
the reported number of packages by.
Use this instead of piping through `tail` for RHEL-like distros,
since using pipes in the command doesn't seem to work.

Fixes #24

Signed-off-by: David Thompson <davthomp@redhat.com>
This commit is contained in:
David Thompson 2023-03-07 14:28:06 -05:00 committed by GitHub
parent 455299ffe3
commit ec82eb613c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,28 +1,37 @@
from subprocess import check_output from subprocess import check_output
commands = [ class PackagesCommand:
"pacman -Qq --color never", # Arch Linux def __init__(self, command: str, adjust_amt: int = 0):
"dpkg-query -f '.\n' -W", # Debian, Ubuntu, Mint """
"dnf list installed | tail -n +2", # Fedora, RHEL Represents a command that retries a newline seperated list of all packages on the system.
"rpm -qa", # RHEL, Fedora Core, CentOS :param command: the command to run
"yum list installed | tail -n +2", # RHEL, Fedora Core, CentOS :param adjust_amt: the amount to add/remove to the number reported by the command
"xbps-query -l", # Void Linux """
"zypper search -i", # openSUSE self.command = command
"kiss l", # KISS Linux self.adjust_amt = adjust_amt
"equery list '*'", # Gentoo
"qlist -I", # Gentoo packages_commands: list[PackagesCommand] = [
"pkg info -a", # BSDs PackagesCommand("pacman -Qq --color never"), # Arch Linux
"pkg_info", # BSDs PackagesCommand("dpkg-query -f '.\n' -W"), # Debian, Ubuntu, Mint
"apk info", # Alpine PackagesCommand("dnf list installed -q", -1), # Fedora, RHEL
"nix-store -qR /run/current-system/sw", # Nix PackagesCommand("yum list installed -q", -1), # RHEL, Fedora Core, CentOS
PackagesCommand("rpm -qa"), # RHEL, Fedora Core, CentOS
PackagesCommand("xbps-query -l"), # Void Linux
PackagesCommand("zypper search -i"), # openSUSE
PackagesCommand("kiss l"), # KISS Linux
PackagesCommand("equery list '*'"), # Gentoo
PackagesCommand("qlist -I"), # Gentoo
PackagesCommand("pkg info -a"), # BSDs
PackagesCommand("pkg_info"), # BSDs
PackagesCommand("apk info"), # Alpine
PackagesCommand("nix-store -qR /run/current-system/sw"), # Nix
] ]
def get_num_packages() -> (int, bool): def get_num_packages() -> (int, bool):
for command in commands: for packages_command in packages_commands:
try: try:
# Get the length of the output of the command - the number of packages # Get the length of the output of the command - the number of packages
return len(check_output(command.split(" ")).decode("utf-8").split("\n")) - 1 return len(check_output(packages_command.command.split(" ")).decode("utf-8").split("\n")) - 1 + packages_command.adjust_amt
except FileNotFoundError: except FileNotFoundError:
# If the command doesn't exist, skip it # If the command doesn't exist, skip it