from pathlib import Path


def find_artefacts(base_dir, type="models", target_file="model.py"):
    """
    Find a Mantra artefacts directory

    :param base_dir: Base directory to look at
    :param type: Can be `models`, `data`, `tasks`
    :param target_file: File use to identify the artefact
    :return:
    """

    base_path = Path(base_dir, type)

    all_artefacts = []

    if base_path.exists():
        subdirs = [p for p in base_path.iterdir() if p.is_dir()]

        while len(subdirs) > 0:
            subdir = subdirs.pop()
            subdir_files = [p for p in subdir.iterdir()]

            # found the target file in this subdir!
            if target_file in [p.name for p in subdir_files]:
                all_artefacts.append(str(subdir))
            else:
                # go into further subdirs
                subdirs.extend([p for p in subdir.iterdir() if p.is_dir()])

    return all_artefacts