summaryrefslogtreecommitdiff
path: root/dots_manager/stow.py
blob: 52a9e366ec8c4dd2cd2fb9e75e4f93d0def37317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pathlib import Path
from typing import Literal
from .env import Environment
from .shell import run_shell_command
from .parallel import parallelize
from .utils import is_some


def list_stowable_packages(packages: Path) -> list[Path]:
    denylist = [".", "__"]
    return [
        d
        for d in packages.iterdir()
        if d.is_dir() and all(y not in d.name for y in denylist)
    ]


def apply_stow_operation_to_packages(
    packages: Path,
    target: Path,
    stow_op: Literal["-D", "--no-folding"],
    env: Environment,
) -> bool:
    if not run_shell_command(["stow", "--version"], env.logger):
        env.logger.error("stow not installed D:")
        return False

    package_command = ["stow", "-d", str(packages), "-t", str(target), stow_op]
    _packages = list_stowable_packages(packages)
    env.logger.debug(f"found dotfile packages: {_packages}")

    commands = [package_command + [package.name] for package in _packages]
    env.logger.debug(f"stowing packages: {commands}")

    results = parallelize(
        lambda command: is_some(run_shell_command, command, env.logger)[0],
        commands,
        env,
    )
    return len(commands) == sum(1 if x else 0 for x in results)