Skip to content

[WIP] refactor: enhance DebPackages and DebPackage facts to include detailed package information #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions pyinfra/facts/deb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from pyinfra.api import FactBase

from .util.packaging import parse_packages

DEB_PACKAGE_NAME_REGEX = r"[a-zA-Z0-9\+\-\.]+"
DEB_PACKAGE_VERSION_REGEX = r"[a-zA-Z0-9:~\.\-\+]+"

Expand All @@ -34,7 +32,13 @@ class DebPackages(FactBase):
.. code:: python

{
"package_name": ["version"],
"package_name": {
"desired": "Install",
"status": "Installed",
"version": "version",
"architecture": "architecture",
"description": "description",
}
}
"""

Expand All @@ -48,14 +52,49 @@ def requires_command(self) -> str:

default = dict

regex = r"^[i|h]i\s+({0}):?[a-zA-Z0-9]*\s+({1}).+$".format(
DEB_PACKAGE_NAME_REGEX,
DEB_PACKAGE_VERSION_REGEX,
)
regex = r"^([uirph]{1})([nicuhwt]{1})\s+([\w\-\.]+)\s+([\w\-\+\.:~]+)\s+(\w+)\s+(.+)$"

@override
def process(self, output):
return parse_packages(self.regex, output)
packages = {}

# Mapping of single-letter codes to their full meanings
desired_map = {
"u": "Unknown",
"i": "Install",
"r": "Remove",
"p": "Purge",
"h": "Hold",
}

status_map = {
"n": "Not-installed",
"i": "Installed",
"c": "Config-files",
"u": "Unpacked",
"h": "Half-installed",
"w": "Trigger-awaited",
"t": "Trigger-pending",
}

for line in output:
matches = re.match(self.regex, line)
if matches:
desired_code = matches.group(1) # Desired action (u,i,r,p,h)
status_code = matches.group(2) # Current status (n,i,c,u,h,w,t)
name = matches.group(3) # Package name
version = matches.group(4) # Version
arch = matches.group(5) # Architecture
description = matches.group(6).strip() # Description

packages[name] = {
"desired": desired_map.get(desired_code, "Unknown"),
"status": status_map.get(status_code, "Unknown"),
"version": version,
"architecture": arch,
"description": description,
}
return packages


class DebPackage(FactBase):
Expand All @@ -66,6 +105,9 @@ class DebPackage(FactBase):
_regexes = {
"name": r"^Package:\s+({0})$".format(DEB_PACKAGE_NAME_REGEX),
"version": r"^Version:\s+({0})$".format(DEB_PACKAGE_VERSION_REGEX),
"architecture": r"^Architecture:\s+(.*)$",
"description": r"^Description:\s+(.*)$",
"status": r"^Status:\s+(.*)$",
}

@override
Expand Down
4 changes: 3 additions & 1 deletion tests/facts/deb.DebPackage/whitespace.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
" gskssl64 (>= 8.0-55.31)"
],
"fact": {
"version": "8.1.21-0",
"name": "tivsm-api64",
"version": "8.1.21-0"
"architecture": "amd64",
"description": "This is the IBM Storage Protect Linux API"
}
}
24 changes: 21 additions & 3 deletions tests/facts/deb.DebPackages/hold_packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@
"hi wireguard 1.0.20200513-1~18.04.2 all fast, modern, secure kernel VPN tunnel (metapackage)"
],
"fact": {
"matrix-synapse-py3": ["1.37.1+bionic1"],
"miniflux": ["2.0.31"],
"wireguard": ["1.0.20200513-1~18.04.2"]
"matrix-synapse-py3": {
"desired": "Hold",
"status": "Installed",
"version": "1.37.1+bionic1",
"architecture": "amd64",
"description": "Open federated Instant Messaging and VoIP server"
},
"miniflux": {
"desired": "Hold",
"status": "Installed",
"version": "2.0.31",
"architecture": "amd64",
"description": "Minimalist Feed Reader"
},
"wireguard": {
"desired": "Hold",
"status": "Installed",
"version": "1.0.20200513-1~18.04.2",
"architecture": "all",
"description": "fast, modern, secure kernel VPN tunnel (metapackage)"
}
}
}
40 changes: 35 additions & 5 deletions tests/facts/deb.DebPackages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,40 @@
"ii x11-xkb-utils 7.7+1 amd64 X11 XKB utilitie"
],
"fact": {
"w3m": ["0.5.3-15"],
"wget": ["1.15-1ubuntu1.14.04."],
"whiptail": ["0.52.15-2ubuntu5"],
"x11-common": ["1:7.7+1ubuntu8.1"],
"x11-xkb-utils": ["7.7+1"]
"w3m": {
"desired": "Install",
"status": "Installed",
"version": "0.5.3-15",
"architecture": "amd64",
"description": "WWW browsable pager with excellent tables/frames support"
},
"wget": {
"desired": "Install",
"status": "Installed",
"version": "1.15-1ubuntu1.14.04.",
"architecture": "amd64",
"description": "retrieves files from the web"
},
"whiptail": {
"desired": "Install",
"status": "Installed",
"version": "0.52.15-2ubuntu5",
"architecture": "amd64",
"description": "Displays user-friendly dialog oxes from shell scripts"
},
"x11-common": {
"desired": "Install",
"status": "Installed",
"version": "1:7.7+1ubuntu8.1",
"architecture": "all",
"description": "X Window System (X.Org) infrastructure"
},
"x11-xkb-utils": {
"desired": "Install",
"status": "Installed",
"version": "7.7+1",
"architecture": "amd64",
"description": "X11 XKB utilitie"
}
}
}