77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
# uv curl git stow taskd
|
|
|
|
import tomllib
|
|
# import pyalpm
|
|
import subprocess
|
|
import pycman.config as pacman_config
|
|
from pathlib import Path
|
|
from dataclasses import dataclass
|
|
|
|
@dataclass
|
|
class Repo:
|
|
src: str
|
|
dest: str
|
|
|
|
def clone(self) -> None:
|
|
dest: Path = Path(self.dest).expanduser()
|
|
if dest.exists():
|
|
print(f"[skip] {dest} already exists")
|
|
return
|
|
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
subprocess.run(["git", "clone", "--recurse-submodules", self.src, str(self.dest)], check=True)
|
|
|
|
|
|
with open("config.toml", "rb") as f:
|
|
config = tomllib.load(f)
|
|
|
|
system_packages = config["system"]["packages"]
|
|
dotfiles: Repo = Repo(*config["repos"]["dotfiles"])
|
|
|
|
class PackageManager:
|
|
def __init__(self):
|
|
conf = pacman_config.PacmanConfig("/etc/pacman.conf")
|
|
self.handle = conf.initialize_alpm()
|
|
self.localdb = self.handle.get_localdb()
|
|
|
|
def is_installed(self, name: str) -> bool:
|
|
return self.localdb.get_pkg(name) is not None
|
|
|
|
def get_version(self, name: str) -> str | None:
|
|
pkg = self.localdb.get_pkg(name)
|
|
return pkg.version if pkg else None
|
|
|
|
def ensure_installed(self, packages: list[str]) -> None:
|
|
missing = [p for p in packages if not self.is_installed(p)]
|
|
if not missing:
|
|
return
|
|
print(f"Installing: {' '.join(missing)}")
|
|
subprocess.run(
|
|
["sudo", "pacman", "-S", "--noconfirm", *missing],
|
|
check=True
|
|
)
|
|
|
|
def ensure_removed(self, packages: list[str]) -> None:
|
|
present = [p for p in packages if self.is_installed(p)]
|
|
if not present:
|
|
return
|
|
subprocess.run(
|
|
["sudo", "pacman", "-Rns", "--noconfirm", *present],
|
|
check=True
|
|
)
|
|
|
|
def ensure_aur_installed(self, packages: list[str]) -> None:
|
|
missing = [p for p in packages if not self.is_installed(p)]
|
|
if not missing:
|
|
return
|
|
subprocess.run(
|
|
["paru", "-S", "--noconfirm", "--skipreview", *missing],
|
|
check=True
|
|
)
|
|
|
|
|
|
pm = PackageManager()
|
|
pm.ensure_installed(system_packages)
|
|
pm.ensure_aur_installed(config["system"]["aur"])
|
|
dotfiles.clone()
|