Skip to content

Commit

Permalink
[BREAKING] Exclude subprocess data sources when root_dir is specified
Browse files Browse the repository at this point in the history
The global idea here is about preferring no data instead of false ones
if `root_dir` parameter is set (as this may result in calling a
binary returning, for instance, host information).

Developers used to set `root_dir` and purposely including LSB, uname
or oslevel commands will now get a `ValueError` exception during class
initialization.

See #311 for complete thread and rationale.
  • Loading branch information
Samuel FORESTIER committed Feb 13, 2022
1 parent bfe115d commit bee21c1
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/distro/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +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: bool = True,
include_oslevel: Optional[bool] = None,
) -> None:
"""
The initialization method of this class gathers information from the
Expand Down Expand Up @@ -685,7 +685,8 @@ 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
Expand Down Expand Up @@ -719,6 +720,9 @@ def __init__(
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.
Expand Down Expand Up @@ -749,9 +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
self.include_oslevel = include_oslevel

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 Down

0 comments on commit bee21c1

Please sign in to comment.