1
0

verify-checksum-models.py 2.4 KB

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