import hashlib
import click


def calculate_file_md5(filepath, blocksize=2 ** 20):
    """Calculate an MD5 hash for a file."""
    checksum = hashlib.md5()

    with click.open_file(filepath, "rb") as f:

        def update_chunk():
            """Add chunk to checksum."""
            buf = f.read(blocksize)
            if buf:
                checksum.update(buf)
            return bool(buf)

        while update_chunk():
            pass

    return checksum.hexdigest()