aboutsummaryrefslogtreecommitdiff
path: root/mu4web/util.py
blob: 7e2df8c4fce7ee559c9f92f15c6d07e9bb8dd024 (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
"""Various misc. utilities."""

import subprocess
from os import PathLike
from typing import (
    Union,
)


def find(basedir: PathLike[str] | PathLike[bytes],
         **flags: str | bytes) -> list[bytes]:
    """
    Run the shell command ``find``.

    :param basedir:
        Directory to search under.

    :param flags:
        Key-value pairs passed to find. Each key is prefixed with a
        single dash. Compound searches might be possible with some
        trickery, but extend this function instead of doing that.
    """
    cmdline: list[Union[str, bytes,
                        PathLike[str],
                        PathLike[bytes]]] = ['find', basedir]
    for key, value in flags.items():
        cmdline += [f'-{key}', value]
    cmdline.append('-print0')

    cmd = subprocess.run(cmdline, capture_output=True)
    return cmd.stdout.split(b'\0')[:-1]