Detect the current OS/distro.
The keyword-only overrides let tests exercise this function for real
(real filesystem paths, no mocking) against a Linux distro other than
the one actually running the test suite.
Source code in src/personal_os_setup/detect_os.py
| def detect_os(
*,
system: str | None = None,
os_release_path: Path | None = None,
is_wsl: bool | None = None,
) -> OSInfo:
"""Detect the current OS/distro.
The keyword-only overrides let tests exercise this function for real
(real filesystem paths, no mocking) against a Linux distro other than
the one actually running the test suite.
"""
system = (system or platform.system()).lower()
if system == "windows":
return OSInfo(family="windows", distro="windows")
if system == "darwin":
return OSInfo(family="darwin", distro="darwin")
if system == "linux":
os_release = os_release_path or Path("/etc/os-release")
if not os_release.exists():
distro = "linux"
else:
data: dict[str, str] = {}
for line in os_release.read_text(encoding="utf-8").splitlines():
if not line or "=" not in line:
continue
k, v = line.split("=", 1)
data[k.strip()] = v.strip().strip('"')
distro = data.get("ID", "linux").lower()
wsl = _is_wsl() if is_wsl is None else is_wsl
return OSInfo(family=system, distro=distro, info="OS running inside WSL" if wsl else None)
return OSInfo(family="unknown", distro="unknown")
|