verify-checksum-models.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import os
  2. import hashlib
  3. def sha256sum(file):
  4. block_size = 16 * 1024 * 1024 # 16 MB block size
  5. b = bytearray(block_size)
  6. file_hash = hashlib.sha256()
  7. mv = memoryview(b)
  8. with open(file, 'rb', buffering=0) as f:
  9. while True:
  10. n = f.readinto(mv)
  11. if not n:
  12. break
  13. file_hash.update(mv[:n])
  14. return file_hash.hexdigest()
  15. # Define the path to the llama directory (parent folder of script directory)
  16. llama_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
  17. # Define the file with the list of hashes and filenames
  18. hash_list_file = os.path.join(llama_path, "SHA256SUMS")
  19. # Check if the hash list file exists
  20. if not os.path.exists(hash_list_file):
  21. print(f"Hash list file not found: {hash_list_file}")
  22. exit(1)
  23. # Read the hash file content and split it into an array of lines
  24. with open(hash_list_file, "r") as f:
  25. hash_list = f.read().splitlines()
  26. # Create an array to store the results
  27. results = []
  28. # Loop over each line in the hash list
  29. for line in hash_list:
  30. # Split the line into hash and filename
  31. hash_value, filename = line.split(" ")
  32. # Get the full path of the file by joining the llama path and the filename
  33. file_path = os.path.join(llama_path, filename)
  34. # Informing user of the progress of the integrity check
  35. print(f"Verifying the checksum of {file_path}")
  36. # Check if the file exists
  37. if os.path.exists(file_path):
  38. # Calculate the SHA256 checksum of the file using hashlib
  39. file_hash = sha256sum(file_path)
  40. # Compare the file hash with the expected hash
  41. if file_hash == hash_value:
  42. valid_checksum = "V"
  43. file_missing = ""
  44. else:
  45. valid_checksum = ""
  46. file_missing = ""
  47. else:
  48. valid_checksum = ""
  49. file_missing = "X"
  50. # Add the results to the array
  51. results.append({
  52. "filename": filename,
  53. "valid checksum": valid_checksum,
  54. "file missing": file_missing
  55. })
  56. # Print column headers for results table
  57. print("\n" + "filename".ljust(40) + "valid checksum".center(20) + "file missing".center(20))
  58. print("-" * 80)
  59. # Output the results as a table
  60. for r in results:
  61. print(f"{r['filename']:40} {r['valid checksum']:^20} {r['file missing']:^20}")