Skip to content

Commit 2a89f76

Browse files
Samuel FORESTIERHorlogeSkynet
Samuel FORESTIER
authored andcommitted
[BREAKING] Exclude subprocess data sources when root_dir is specified
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.
1 parent 8055ef4 commit 2a89f76

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/distro/distro.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,12 @@ class LinuxDistribution:
640640

641641
def __init__(
642642
self,
643-
include_lsb: bool = True,
643+
include_lsb: Optional[bool] = None,
644644
os_release_file: str = "",
645645
distro_release_file: str = "",
646-
include_uname: bool = True,
646+
include_uname: Optional[bool] = None,
647647
root_dir: Optional[str] = None,
648-
include_oslevel: bool = True,
648+
include_oslevel: Optional[bool] = None,
649649
) -> None:
650650
"""
651651
The initialization method of this class gathers information from the
@@ -686,7 +686,8 @@ def __init__(
686686
be empty.
687687
688688
* ``root_dir`` (string): The absolute path to the root directory to use
689-
to find distro-related information files.
689+
to find distro-related information files. Note that ``include_*``
690+
parameters must not be enabled in combination with ``root_dir``.
690691
691692
* ``include_oslevel`` (bool): Controls whether (AIX) oslevel command
692693
output is included as a data source. If the oslevel command is not
@@ -720,6 +721,9 @@ def __init__(
720721
721722
Raises:
722723
724+
* :py:exc:`ValueError`: Initialization parameters combination is not
725+
supported.
726+
723727
* :py:exc:`OSError`: Some I/O issue with an os-release file or distro
724728
release file.
725729
@@ -750,9 +754,22 @@ def __init__(
750754
self.os_release_file = usr_lib_os_release_file
751755

752756
self.distro_release_file = distro_release_file or "" # updated later
753-
self.include_lsb = include_lsb
754-
self.include_uname = include_uname
755-
self.include_oslevel = include_oslevel
757+
758+
is_root_dir_defined = root_dir is not None
759+
if is_root_dir_defined and (include_lsb or include_uname or include_oslevel):
760+
raise ValueError(
761+
"Including subprocess data sources from specific root_dir is disallowed"
762+
" to prevent false information"
763+
)
764+
self.include_lsb = (
765+
include_lsb if include_lsb is not None else not is_root_dir_defined
766+
)
767+
self.include_uname = (
768+
include_uname if include_uname is not None else not is_root_dir_defined
769+
)
770+
self.include_oslevel = (
771+
include_oslevel if include_oslevel is not None else not is_root_dir_defined
772+
)
756773

757774
def __repr__(self) -> str:
758775
"""Return repr of all info"""

0 commit comments

Comments
 (0)