Skip to content
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

feat: Implement get_package_version in hosts/base.py #133

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
48 changes: 48 additions & 0 deletions sssd_test_framework/hosts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import csv
import re
from typing import Any

import ldap
Expand Down Expand Up @@ -264,3 +265,50 @@ def distro_minor(self) -> int:
if not self._os_release:
self._distro_information()
return self._distro_minor

def get_package_version(self, package: str = "sssd", raise_on_error: bool = True) -> dict:
"""
Parse package version and return it as a dictionary with:
major, minor, patch, prerelease, update, release
:param package: package name
:param raise_on_error: raise exeption when package is missing
:return: version dictionary
:rtype: dict
:raises OSError: If package is missing or version could not be parsed.
"""
vers = {
"major": 0,
"minor": 0,
"patch": 0,
"prerelease": "",
"update": 0,
"release": "",
}
rpm = self.conn.run("test -f /usr/bin/rpm", raise_on_error=False)
dpkg = self.conn.run("test -f /usr/bin/dpkg-query", raise_on_error=False)
if rpm.rc == 0:
ver = self.conn.run(f'rpm -q {package} --queryformat "%{{VERSION}}-%{{RELEASE}}"').stdout
elif dpkg.rc != 0:
ver = self.conn.run(f"dpkg-query -f '${{Version}}' -W {package}").stdout
else:
if raise_on_error:
raise OSError(f"Package {package} not found!")
return vers

v_match = re.match(
r"([0-9]+)(?:\.)?([0-9]+)?(?:\.)?([0-9]+)?(?:~)?([a-z0-9]+)?[\.-]?([0-9]+)?(?:\.)?(.*)?",
ver,
re.IGNORECASE,
)

if v_match is None:
if raise_on_error:
raise OSError(f"Package {package} version could not be parsed!")
return vers
vers["major"] = int(v_match.group(1))
vers["minor"] = int(v_match.group(2)) if v_match.group(2) else 0
vers["patch"] = int(v_match.group(3)) if v_match.group(3) else 0
vers["prerelease"] = v_match.group(4) if v_match.group(4) else ""
vers["update"] = int(v_match.group(5)) if v_match.group(5) else 0
vers["release"] = v_match.group(6) if v_match.group(6) else ""
return vers
Loading