r""" Creates a hash file for each file in a path

    CommandLine:
        python -m utool.util_hash --test-write_hash_file

    Example:
        >>> # DISABLE_DOCTEST
        >>> import utool as ut
        >>> from utool.util_hash import *  # NOQA
        >>> fpath = ut.grab_test_imgpath('patsy.jpg')
        >>> write_hash_file(fpath, 'md5')

def write_hash_file(fpath, hash_tag='md5', recompute=False):
    r""" Creates a hash file for each file in a path

    CommandLine:
        python -m utool.util_hash --test-write_hash_file

    Example:
        >>> # DISABLE_DOCTEST
        >>> import utool as ut
        >>> from utool.util_hash import *  # NOQA
        >>> fpath = ut.grab_test_imgpath('patsy.jpg')
        >>> write_hash_file(fpath, 'md5')
    """
    hash_dict = {
        'md5'    : hashlib.md5(),
        'sha1'   : hashlib.sha1(),
        'sha256' : hashlib.sha256(),
    }
    message = "Unrecognized hashing function.  Use 'md5', 'sha1', or 'sha256"
    assert hash_tag in hash_dict, message
    if fpath.endswith('.%s' % (hash_tag, )):
        # No need to compute hashes on hashes
        return
    # Get hash path
    hash_fpath = '%s.%s' % (fpath, hash_tag, )
    if os.path.exists(hash_fpath) and not recompute:
        return
    # Assert this is a file
    file_type = util_path.get_path_type(fpath)
    if file_type == 'file':
        # Compute hash
        hasher = hash_dict[hash_tag]
        hash_local = get_file_hash(fpath, hasher=hasher, hexdigest=True)
        print('[utool] Adding:', fpath, hash_local)
        with open(hash_fpath, 'w') as hash_file:
            hash_file.write(hash_local)
        return hash_fpath