Skip to content

detect_os

build_packages_for_os()

Detect OS/distro and return the filtered package list for that distro.

Source code in src/personal_os_setup/detect_os.py
def build_packages_for_os() -> tuple[str, str, str | None, list[PackageRef]]:
    """Detect OS/distro and return the filtered package list for that distro."""
    os_info = detect_os()
    system = os_info.family
    distro = os_info.distro
    info = os_info.info
    pkg = resources.files("personal_os_setup")
    data = yaml.safe_load((pkg / "config" / "packages.yaml").read_text(encoding="utf-8")) or {}
    if not isinstance(data, dict):
        data = {}
    catalog = PackageCatalog(data=data)

    distro_block = catalog.for_distro(distro)
    packages = list(iter_packages(distro_block))
    return system, distro, info, packages

detect_os(*, system=None, os_release_path=None, is_wsl=None)

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")