1
0

verify-checksum-models.py 2.3 KB

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