Files
linux-image-manager/lim/system.py
Kevin Veen-Birkenbach ccdef065df refactor!: port shell scripts to Python package
Bash scripts were untestable and duplicated device/LUKS/mount logic;
the lim/ package centralizes it behind one subprocess wrapper and a
YAML image catalog (single point of truth).

BREAKING CHANGE: scripts/*.sh removed. Use `lim --type <cmd>`; new
types mount/umount/single-boot/raid1-boot/lock/unlock/import/export
replace direct script calls. --extra is deprecated and ignored.

- distributions.yml + lim/catalog.py hold the image catalog (PyYAML)
- pytest suite: 102 tests with mocked subprocess (tests/unit) and a
  250-line max file-length guard (tests/lint)
- ruff strict (select ALL), GitHub Actions CI, Dependabot; Travis gone
- Makefile: install (symlink ~/.local/bin/lim) and test targets
- fixes over bash: SUDO_USER-aware chown, mmcblk/nvme partition paths,
  sha512 checksum support, whole-pipeline failure detection, blkid
  UUID fallback for pre-mounted images, conditional fstab seeding for
  PARTUUID/LABEL images, clean errors for missing binaries
2026-07-14 11:37:19 +02:00

36 lines
995 B
Python

"""Process-level helpers: privilege handling and user resolution."""
import getpass
import os
import sys
from pathlib import Path
from lim import ui
def is_root() -> bool:
return os.geteuid() == 0
def ensure_root() -> None:
"""Re-execute the current command with sudo when not running as root."""
if is_root():
return
ui.info("Root privileges required. Re-executing with sudo...")
script = str(Path(sys.argv[0]).resolve())
# Deliberate privilege escalation: replace this process with sudo.
os.execvp("sudo", ["sudo", sys.executable, script, *sys.argv[1:]]) # noqa: S606
def real_user() -> str:
"""Return the invoking user, even when running under sudo."""
return os.environ.get("SUDO_USER") or getpass.getuser()
def real_home() -> Path:
"""Home directory of the invoking user, even when running under sudo."""
sudo_user = os.environ.get("SUDO_USER")
if sudo_user:
return Path("/home") / sudo_user
return Path.home()