mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +00:00
Refactor scripts/install/lint.sh (~870 LOC bash) into a Python package
under utils/install/. scripts/install/lint.sh becomes a 7-line wrapper
that delegates to `python3 -m utils.install.lint`.
Layout:
- utils/install/{primitives,pip,npm,github_release,system_pkg}.py:
channel-agnostic installers
- utils/install/lint/<tool>.py: one ensure() per tool (actionlint,
shfmt, mbake, ruff, ansible_lint, galaxy_importer, markdownlint_cli2,
eslint, shellcheck, ansible_commands, ansible_collections)
- utils/install/lint/__main__.py: CLI + group dispatch + stamp logic
INFINITO_LINT_RUNNER dispatch:
- New env var (host | docker, default docker), tracked in env/static.env
+ passthrough handler + drift test.
- scripts/install/wrapper.sh routes `make install-lint` to host or
container based on the runner.
- scripts/lint/wrapper.sh already dispatched; container branch now uses
bash (not --login) so /etc/profile does not reset PATH, and forwards
BASH_ENV so the .env keys land in the lint script's env.
Stamp logic:
- Per-interpreter stamp at build/install-lint-<sha256(sys.executable)>.stamp
so host venv and container venv track installs independently despite
bind-mounted build/.
- `--force` drops the stamp; group args bypass the stamp.
Tests + docs:
- 43 unit tests under tests/unit/utils/install/{,lint/} cover primitives,
pip/npm/github_release/system_pkg fallback chains, and the main CLI
(stamp logic, group dispatch, error path).
- utils/install/README.md + utils/install/lint/README.md document the
structure and the per-tool ensure() contract.
- docs/contributing/tools/make.md install-lint row updated to mention
the runner dispatch + per-env stamp + install-lint-force.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
"""System-package-manager dispatch (pacman / apt-get / dnf / yum / brew)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
import shutil
|
|
import subprocess
|
|
|
|
from utils.install.primitives import log, run_privileged, warn
|
|
|
|
_SUPPORTED = ("pacman", "apt-get", "dnf", "yum", "brew")
|
|
|
|
|
|
def detect_package_manager() -> str:
|
|
for manager in _SUPPORTED:
|
|
if shutil.which(manager) is not None:
|
|
return manager
|
|
raise RuntimeError("No supported package manager found")
|
|
|
|
|
|
def _prepare_manager(manager: str) -> None:
|
|
if manager == "apt-get":
|
|
run_privileged(["apt-get", "update"])
|
|
elif manager == "dnf":
|
|
with contextlib.suppress(subprocess.CalledProcessError):
|
|
run_privileged(["dnf", "-y", "install", "dnf-plugins-core"])
|
|
with contextlib.suppress(subprocess.CalledProcessError):
|
|
run_privileged(["dnf", "-y", "install", "epel-release"])
|
|
elif manager == "yum":
|
|
with contextlib.suppress(subprocess.CalledProcessError):
|
|
run_privileged(["yum", "-y", "install", "yum-utils"])
|
|
with contextlib.suppress(subprocess.CalledProcessError):
|
|
run_privileged(["yum", "-y", "install", "epel-release"])
|
|
|
|
|
|
def _install_one(manager: str, package: str) -> bool:
|
|
log(f"Installing package '{package}' via {manager}")
|
|
try:
|
|
if manager == "pacman":
|
|
run_privileged(["pacman", "-Syu", "--noconfirm", "--needed", package])
|
|
elif manager == "apt-get":
|
|
run_privileged(
|
|
["apt-get", "install", "-y", "--no-install-recommends", package]
|
|
)
|
|
elif manager == "dnf":
|
|
run_privileged(["dnf", "-y", "install", package])
|
|
elif manager == "yum":
|
|
run_privileged(["yum", "-y", "install", package])
|
|
elif manager == "brew":
|
|
subprocess.run(["brew", "install", package], check=True)
|
|
else:
|
|
warn(f"Unsupported package manager: {manager}")
|
|
return False
|
|
except subprocess.CalledProcessError:
|
|
return False
|
|
return True
|
|
|
|
|
|
def install_package_candidates(manager: str, packages: list[str]) -> None:
|
|
_prepare_manager(manager)
|
|
for package in packages:
|
|
if _install_one(manager, package):
|
|
return
|
|
raise RuntimeError(f"All package candidates failed via {manager}: {packages}")
|
|
|
|
|
|
_COMMAND_PACKAGES: dict[str, dict[str, list[str]]] = {
|
|
"ansible-playbook": {
|
|
"pacman": ["ansible-core", "ansible"],
|
|
"apt-get": ["ansible-core", "ansible"],
|
|
"dnf": ["ansible-core", "ansible"],
|
|
"yum": ["ansible-core", "ansible"],
|
|
"brew": ["ansible"],
|
|
},
|
|
"ansible-galaxy": {
|
|
"pacman": ["ansible-core", "ansible"],
|
|
"apt-get": ["ansible-core", "ansible"],
|
|
"dnf": ["ansible-core", "ansible"],
|
|
"yum": ["ansible-core", "ansible"],
|
|
"brew": ["ansible"],
|
|
},
|
|
"ruff": {m: ["ruff"] for m in _SUPPORTED},
|
|
"shfmt": {m: ["shfmt"] for m in _SUPPORTED},
|
|
"shellcheck": {m: ["shellcheck"] for m in _SUPPORTED},
|
|
"npm": {
|
|
"pacman": ["npm", "nodejs"],
|
|
"apt-get": ["npm", "nodejs"],
|
|
"dnf": ["npm", "nodejs"],
|
|
"yum": ["npm", "nodejs"],
|
|
"brew": ["node"],
|
|
},
|
|
}
|
|
|
|
|
|
def install_command_via_pkg(command_name: str) -> None:
|
|
manager = detect_package_manager()
|
|
mapping = _COMMAND_PACKAGES.get(command_name)
|
|
if mapping is None or manager not in mapping:
|
|
raise RuntimeError(
|
|
f"No installer mapping defined for '{command_name}' on '{manager}'."
|
|
)
|
|
|
|
log(f"Missing command '{command_name}'. Attempting installation via {manager}.")
|
|
install_package_candidates(manager, mapping[manager])
|
|
|
|
|
|
__all__ = [
|
|
"detect_package_manager",
|
|
"install_command_via_pkg",
|
|
"install_package_candidates",
|
|
]
|