from pathlib import Path
import hashlib


def check_hash(path, notebook, old_hash):
    """Check if current notebook SHA1 matches calculated hash"""
    hashsource = []
    notebook_name = str(Path(path).name).lower()
    hashsource.append(notebook_name + ":")
    for cell in notebook.get('cells', []):
        if cell.get('cell_type', '') == 'code':
            hashsource.append('[{}]'.format(cell.get('execution_count', '') or ''))
        hashsource.append(cell.get('source', '') + ';;;')
    current_hash = hashlib.sha1(''.join(hashsource).encode('utf-8')).hexdigest()
    return current_hash == old_hash