import pathlib
import random
import shutil


def prepare_containerfile(anchor: str, dockerfile: str) -> str:
    """Ensure that Containerfile, or a proxy Containerfile is in context_dir.

    Args:
        anchor: Build context directory
        dockerfile: Path to Dockerfile/Containerfile

    Returns:
        path to Dockerfile/Containerfile in root of context directory
    """
    anchor_path = pathlib.Path(anchor)
    dockerfile_path = pathlib.Path(dockerfile)

    if dockerfile_path.parent.samefile(anchor_path):
        return dockerfile_path.name

    proxy_path = anchor_path / f".containerfile.{random.getrandbits(160):x}"
    shutil.copy2(dockerfile_path, proxy_path, follow_symlinks=False)
    return proxy_path.name