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

Add support for AIX (closes #241) #311

Merged
merged 5 commits into from
Feb 13, 2022
Merged
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
76 changes: 65 additions & 11 deletions src/distro/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def id() -> str:
"freebsd" FreeBSD
"midnightbsd" MidnightBSD
"rocky" Rocky Linux
"aix" AIX
============== =========================================

If you have a need to get distros for reliable IDs added into this set,
Expand Down Expand Up @@ -638,11 +639,12 @@ class LinuxDistribution:

def __init__(
self,
include_lsb: bool = True,
include_lsb: Optional[bool] = None,
os_release_file: str = "",
distro_release_file: str = "",
include_uname: bool = True,
include_uname: Optional[bool] = None,
root_dir: Optional[str] = None,
include_oslevel: Optional[bool] = None,
) -> None:
"""
The initialization method of this class gathers information from the
Expand Down Expand Up @@ -683,7 +685,13 @@ def __init__(
be empty.

* ``root_dir`` (string): The absolute path to the root directory to use
to find distro-related information files.
to find distro-related information files. Note that ``include_*``
parameters must not be enabled in combination with ``root_dir``.

* ``include_oslevel`` (bool): Controls whether (AIX) oslevel command
output is included as a data source. If the oslevel command is not
available in the program execution path the data source will be
empty.

Public instance attributes:

Expand All @@ -702,15 +710,22 @@ def __init__(
parameter. This controls whether the uname information will
be loaded.

* ``include_oslevel`` (bool): The result of the ``include_oslevel``
parameter. This controls whether (AIX) oslevel information will be
loaded.

* ``root_dir`` (string): The result of the ``root_dir`` parameter.
The absolute path to the root directory to use to find distro-related
information files.

Raises:

* :py:exc:`ValueError`: Initialization parameters combination is not
supported.

* :py:exc:`OSError`: Some I/O issue with an os-release file or distro
release file.

* :py:exc:`subprocess.CalledProcessError`: The lsb_release command had
some issue (other than not being available in the program execution
path).

* :py:exc:`UnicodeError`: A data source has unexpected characters or
uses an unexpected encoding.
"""
Expand Down Expand Up @@ -738,8 +753,22 @@ def __init__(
self.os_release_file = usr_lib_os_release_file

self.distro_release_file = distro_release_file or "" # updated later
self.include_lsb = include_lsb
self.include_uname = include_uname

is_root_dir_defined = root_dir is not None
if is_root_dir_defined and (include_lsb or include_uname or include_oslevel):
raise ValueError(
"Including subprocess data sources from specific root_dir is disallowed"
" to prevent false information"
)
self.include_lsb = (
include_lsb if include_lsb is not None else not is_root_dir_defined
)
self.include_uname = (
include_uname if include_uname is not None else not is_root_dir_defined
)
self.include_oslevel = (
include_oslevel if include_oslevel is not None else not is_root_dir_defined
)

def __repr__(self) -> str:
"""Return repr of all info"""
Expand All @@ -749,10 +778,13 @@ def __repr__(self) -> str:
"distro_release_file={self.distro_release_file!r}, "
"include_lsb={self.include_lsb!r}, "
"include_uname={self.include_uname!r}, "
"include_oslevel={self.include_oslevel!r}, "
"root_dir={self.root_dir!r}, "
"_os_release_info={self._os_release_info!r}, "
"_lsb_release_info={self._lsb_release_info!r}, "
"_distro_release_info={self._distro_release_info!r}, "
"_uname_info={self._uname_info!r})".format(self=self)
"_uname_info={self._uname_info!r}, "
"_oslevel_info={self._oslevel_info!r})".format(self=self)
)

def linux_distribution(
Expand Down Expand Up @@ -840,6 +872,9 @@ def version(self, pretty: bool = False, best: bool = False) -> str:
).get("version_id", ""),
self.uname_attr("release"),
]
if self.uname_attr("id").startswith("aix"):
# On AIX platforms, prefer oslevel command output.
versions.insert(0, self.oslevel_info())
version = ""
if best:
# This algorithm uses the last version in priority order that has
Expand Down Expand Up @@ -980,6 +1015,12 @@ def uname_info(self) -> Dict[str, str]:
"""
return self._uname_info

def oslevel_info(self) -> str:
"""
Return AIX' oslevel command output.
"""
return self._oslevel_info

def os_release_attr(self, attribute: str) -> str:
"""
Return a single named information item from the os-release file data
Expand Down Expand Up @@ -1139,6 +1180,16 @@ def _uname_info(self) -> Dict[str, str]:
content = self._to_str(stdout).splitlines()
return self._parse_uname_content(content)

@cached_property
def _oslevel_info(self) -> str:
if not self.include_oslevel:
return ""
try:
stdout = subprocess.check_output("oslevel", stderr=subprocess.DEVNULL)
except (OSError, subprocess.CalledProcessError):
return ""
return self._to_str(stdout).strip()

@staticmethod
def _parse_uname_content(lines: Sequence[str]) -> Dict[str, str]:
if not lines:
Expand Down Expand Up @@ -1305,7 +1356,10 @@ def main() -> None:

if args.root_dir:
dist = LinuxDistribution(
include_lsb=False, include_uname=False, root_dir=args.root_dir
include_lsb=False,
include_uname=False,
include_oslevel=False,
root_dir=args.root_dir,
)
else:
dist = _distro
Expand Down
3 changes: 3 additions & 0 deletions tests/resources/distros/aix72/bin/oslevel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

echo "7.2.0.0"
3 changes: 3 additions & 0 deletions tests/resources/distros/aix72/bin/uname
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

HorlogeSkynet marked this conversation as resolved.
Show resolved Hide resolved
echo "AIX 2"
22 changes: 16 additions & 6 deletions tests/test_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,10 @@ def setup_method(self, test_method: FunctionType) -> None:
root_dir = os.path.join(DISTROS_DIR, dist)
self.distro = distro.LinuxDistribution(
include_lsb=False,
include_uname=False,
include_oslevel=False,
os_release_file="",
distro_release_file="path-to-non-existing-file",
include_uname=False,
root_dir=root_dir,
)

Expand All @@ -504,7 +505,6 @@ def setup_method(self, test_method: FunctionType) -> None:
dist = test_method.__name__.split("_")[1]
self._setup_for_distro(os.path.join(DISTROS_DIR, dist))
self.distro = distro.LinuxDistribution(
include_lsb=True,
os_release_file="path-to-non-existing-file",
distro_release_file="path-to-non-existing-file",
)
Expand Down Expand Up @@ -610,7 +610,6 @@ def test_ubuntu14normal_lsb_release(self) -> None:
self._setup_for_distro(os.path.join(TESTDISTROS, "lsb", "ubuntu14_normal"))

self.distro = distro.LinuxDistribution(
include_lsb=True,
os_release_file="path-to-non-existing-file",
distro_release_file="path-to-non-existing-file",
)
Expand All @@ -630,7 +629,6 @@ def test_ubuntu14nomodules_lsb_release(self) -> None:
self._setup_for_distro(os.path.join(TESTDISTROS, "lsb", "ubuntu14_nomodules"))

self.distro = distro.LinuxDistribution(
include_lsb=True,
os_release_file="path-to-non-existing-file",
distro_release_file="path-to-non-existing-file",
)
Expand All @@ -652,7 +650,6 @@ def test_trailingblanks_lsb_release(self) -> None:
)

self.distro = distro.LinuxDistribution(
include_lsb=True,
os_release_file="path-to-non-existing-file",
distro_release_file="path-to-non-existing-file",
)
Expand All @@ -673,7 +670,6 @@ def test_lsb_release_error_level(self, errnum: str) -> None:
self._setup_for_distro(os.path.join(TESTDISTROS, "lsb", f"lsb_rc{errnum}"))

lsb_release_info = distro.LinuxDistribution(
include_lsb=True,
os_release_file="path-to-non-existing-file",
distro_release_file="path-to-non-existing-file",
)._lsb_release_info
Expand Down Expand Up @@ -1128,6 +1124,20 @@ def test_arch_release(self) -> None:
# considered a valid distro release file:
self._test_non_existing_release_file()

def test_aix72_release(self) -> None:
desired_outcome = {
"id": "aix",
"name": "AIX",
"pretty_name": "AIX 7.2.0.0",
"version": "7.2.0.0",
"pretty_version": "7.2.0.0",
"best_version": "7.2.0.0",
"major_version": "7",
"minor_version": "2",
"build_number": "0",
}
self._test_outcome(desired_outcome)

def test_centos5_release(self) -> None:
desired_outcome = {
"id": "centos",
Expand Down