Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

6.7 KB
Raw Blame
"""Discovery of the shells available on this system.

On Windows we detect 4 shell types and verify their executable path at
startup. On macOS/Linux we discover the installed POSIX shells (``$SHELL`` ->
``/etc/shells`` -> ``which bash/zsh/fish``). Only shells that are actually
installed land in the UI dropdown — that avoids "command not found" errors on
spawn. ``discover_shells()`` dispatches on ``detect_platform()`` (AD-6/AD-9);
the four Windows factories stay untouched (AD-7 additive coexistence).
"""
from __future__ import annotations

import os
import shutil
from dataclasses import dataclass

from dodaios.platform import detect_platform


@dataclass(frozen=True, slots=True)
class ShellInfo:
    """Describes one discovered shell installation."""

    id: str            # stable key ("pwsh", "powershell", "cmd", "bash")
    label: str         # user-facing label for the dropdown
    argv: tuple[str, ...]  # full argv list for spawn


def _powershell_7() -> ShellInfo | None:
    # pwsh.exe on PATH (default install of PowerShell 7+)
    found = shutil.which("pwsh")
    if found:
        return ShellInfo(id="pwsh", label="PowerShell 7", argv=(found, "-NoLogo"))
    # Fallback: known install path
    candidate = os.path.expandvars(r"%ProgramFiles%\PowerShell\7\pwsh.exe")
    if os.path.isfile(candidate):
        return ShellInfo(id="pwsh", label="PowerShell 7", argv=(candidate, "-NoLogo"))
    return None


def _windows_powershell() -> ShellInfo | None:
    candidate = os.path.expandvars(
        r"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
    )
    if os.path.isfile(candidate):
        return ShellInfo(
            id="powershell",
            label="Windows PowerShell 5.1",
            argv=(candidate, "-NoLogo"),
        )
    return None


def _cmd() -> ShellInfo | None:
    candidate = os.path.expandvars(r"%SystemRoot%\System32\cmd.exe")
    if os.path.isfile(candidate):
        return ShellInfo(id="cmd", label="CMD", argv=(candidate,))
    return None


def _git_bash() -> ShellInfo | None:
    candidates = [
        os.path.expandvars(r"%ProgramFiles%\Git\bin\bash.exe"),
        os.path.expandvars(r"%ProgramFiles(x86)%\Git\bin\bash.exe"),
        os.path.expandvars(r"%LOCALAPPDATA%\Programs\Git\bin\bash.exe"),
    ]
    for cand in candidates:
        if os.path.isfile(cand):
            # -i = interactive, -l = login (loads .bashrc/.profile)
            return ShellInfo(id="bash", label="Git Bash", argv=(cand, "-i", "-l"))
    found = shutil.which("bash")
    if found and "git" in found.lower():
        return ShellInfo(id="bash", label="Git Bash", argv=(found, "-i", "-l"))
    return None


def _unix_shells() -> list[ShellInfo]:
    """Discover the installed POSIX shells in preference order (AD-9).

    Order:
      1. ``$SHELL`` if set and present on disk (the user's login shell).
      2. Each path listed in ``/etc/shells`` that exists on disk.
      3. ``shutil.which("bash"/"zsh"/"fish")`` as a final fallback.

    Shells are deduplicated by their resolved (``os.path.realpath``) path so a
    symlinked ``/bin/sh -> bash`` or a ``$SHELL`` that is also in ``/etc/shells``
    is listed once. ``argv`` is ``(path, "-i")`` (interactive) on Linux — never
    an unconditional ``-l`` (a login shell on every spawn re-sources profiles
    and is slow, and Linux PATH edits live in ``~/.bashrc``, which an
    interactive shell reads anyway). macOS is the exception and gets ``-l``:
    every Mac terminal (Terminal.app, iTerm2, VS Code) starts LOGIN shells by
    convention, so user PATH lives in ``~/.zprofile``/``~/.bash_profile`` —
    Homebrew's install instructions put it exactly there, and a non-login zsh
    has no brew and none of the coding CLIs on its PATH. (tcsh ignores ``-l``
    beside other flags rather than erroring.)
    """
    results: list[ShellInfo] = []
    seen: set[str] = set()
    used_ids: set[str] = set()
    interactive_args = ("-i", "-l") if detect_platform() == "darwin" else ("-i",)

    def _add(path: str) -> None:
        if not path or not os.path.isfile(path):
            return
        resolved = os.path.realpath(path)
        if resolved in seen:
            return
        seen.add(resolved)
        base = os.path.basename(path)
        shell_id = base
        label = base
        suffix = 2
        while shell_id in used_ids:
            # Two installs sharing a basename (system bash + Homebrew bash)
            # must not share an id: the UI dropdown keys on it, and
            # ``get_shell()`` could only ever return the first.
            shell_id = f"{base}-{suffix}"
            label = f"{base} ({path})"
            suffix += 1
        used_ids.add(shell_id)
        results.append(ShellInfo(id=shell_id, label=label, argv=(path, *interactive_args)))

    # 1. $SHELL (the user's configured login shell).
    _add(os.environ.get("SHELL", ""))

    # 2. /etc/shells — the system registry of valid login shells.
    try:
        with open("/etc/shells", encoding="utf-8") as fh:
            for raw in fh:
                line = raw.strip()
                if not line or line.startswith("#"):
                    continue
                _add(line)
    except OSError:
        # No /etc/shells (some minimal containers) — skip to the which() probe.
        pass

    # 3. which() fallback for the common interactive shells.
    for name in ("bash", "zsh", "fish"):
        found = shutil.which(name)
        if found:
            _add(found)

    return results


def discover_shells() -> list[ShellInfo]:
    """Return every shell installed on this system in preference order.

    Dispatches on ``detect_platform()`` (AD-6): Windows iterates the four
    untouched Windows factories (AD-7); macOS/Linux return ``_unix_shells()``.
    """
    if detect_platform() == "win32":
        results: list[ShellInfo] = []
        for factory in (_powershell_7, _windows_powershell, _cmd, _git_bash):
            info = factory()
            if info is not None:
                results.append(info)
        return results
    return _unix_shells()


def default_shell() -> ShellInfo | None:
    """The shell to use when nobody named one — ``None`` on a host with none.

    "First in preference order" is the whole rule: ``discover_shells()`` already
    returns pwsh before Windows PowerShell before cmd, and the user's ``$SHELL``
    before anything ``/etc/shells`` lists. Callers that only need *a* working
    shell (a plain terminal pane, wrapping a command in one) ask here instead of
    each re-deriving that order and drifting apart.
    """
    shells = discover_shells()
    return shells[0] if shells else None


def get_shell(shell_id: str) -> ShellInfo | None:
    """Looks up a shell by ID — None if not installed."""
    for shell in discover_shells():
        if shell.id == shell_id:
            return shell
    return None