import hashlib
import six


def key_to_lock_name(key):
    """
    Combine part of a key with its hash to prevent very long filenames
    """
    MAX_LENGTH = 50
    key_hash = hashlib.md5(six.b(key)).hexdigest()
    lock_name = key[:MAX_LENGTH - len(key_hash) - 1] + '_' + key_hash
    return lock_name