llama
1#!/usr/bin/env python3
2
3import logging4import os5import hashlib6
7logger = logging.getLogger("verify-checksum-models")8
9
10def sha256sum(file):11block_size = 16 * 1024 * 1024 # 16 MB block size12b = bytearray(block_size)13file_hash = hashlib.sha256()14mv = memoryview(b)15with open(file, 'rb', buffering=0) as f:16while True:17n = f.readinto(mv)18if not n:19break20file_hash.update(mv[:n])21
22return file_hash.hexdigest()23
24
25# Define the path to the llama directory (parent folder of script directory)
26llama_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))27
28# Define the file with the list of hashes and filenames
29hash_list_file = os.path.join(llama_path, "SHA256SUMS")30
31# Check if the hash list file exists
32if not os.path.exists(hash_list_file):33logger.error(f"Hash list file not found: {hash_list_file}")34exit(1)35
36# Read the hash file content and split it into an array of lines
37with open(hash_list_file, "r") as f:38hash_list = f.read().splitlines()39
40# Create an array to store the results
41results = []42
43# Loop over each line in the hash list
44for line in hash_list:45# Split the line into hash and filename46hash_value, filename = line.split(" ")47
48# Get the full path of the file by joining the llama path and the filename49file_path = os.path.join(llama_path, filename)50
51# Informing user of the progress of the integrity check52logger.info(f"Verifying the checksum of {file_path}")53
54# Check if the file exists55if os.path.exists(file_path):56# Calculate the SHA256 checksum of the file using hashlib57file_hash = sha256sum(file_path)58
59# Compare the file hash with the expected hash60if file_hash == hash_value:61valid_checksum = "V"62file_missing = ""63else:64valid_checksum = ""65file_missing = ""66else:67valid_checksum = ""68file_missing = "X"69
70# Add the results to the array71results.append({72"filename": filename,73"valid checksum": valid_checksum,74"file missing": file_missing75})76
77
78# Print column headers for results table
79print("filename".ljust(40) + "valid checksum".center(20) + "file missing".center(20)) # noqa: NP10080print("-" * 80) # noqa: NP10081
82# Output the results as a table
83for r in results:84print(f"{r['filename']:40} {r['valid checksum']:^20} {r['file missing']:^20}") # noqa: NP10085