def which()

in polymetis/python/polymetis/utils/data_dir.py [0:0]


def which(program: str):
    """Equivalent of `which <https://en.wikipedia.org/wiki/Which_(command)>`_ program.

    Taken from https://stackoverflow.com/a/377028
    Returns equivalent of $(which program), or None
    if unable to find it.

    Args:
        program: name of the executable to find.
    """

    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, _fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None