#!/usr/bin/python3

import tlsh


def compute_hash(path):
    h = tlsh.Tlsh()
    with open(path, 'rb') as f:
        for buf in iter(lambda: f.read(512), b''):
            h.update(buf)
    h.final()
    return h


def compare_files(left_path, right_path):
    left_hash = compute_hash(left_path)
    right_hash = compute_hash(right_path)
    return left_hash.diff(right_hash)


def main():
    score = compare_files('debian/tests/data/file1', 'debian/tests/data/file1')
    print('Identical:', score)
    if score != 0:
        raise Exception('Should be identical')
    score = compare_files('debian/tests/data/file1', 'debian/tests/data/file2')
    print('Similar:', score)
    if score >= 40:
        raise Exception('Should be similar')
    score = compare_files('debian/tests/data/file1', 'debian/tests/data/quick')
    print('Different:', score)
    if score <= 300:
        raise Exception('Should be different')


if __name__ == '__main__':
    main()
