"""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]