Utility function to calculate md5 hashes while being light on memory usage.

    By reading the fileobject piece by piece, we are able to process content that
    is larger than available memory

def calculate_md5(fileobject, size=2**16):
    """Utility function to calculate md5 hashes while being light on memory usage.

    By reading the fileobject piece by piece, we are able to process content that
    is larger than available memory"""
    fileobject.seek(0)
    md5 = hashlib.md5()
    for data in iter(lambda: fileobject.read(size), b''):
        if not data: break
        if isinstance(data, six.text_type):
            data = data.encode('utf-8') # md5 needs a byte string
        md5.update(data)
    fileobject.seek(0) # rewind read head
    return md5.hexdigest()