llama

Форк
0
/
verify-checksum-models.py 
84 строки · 2.4 Кб
1
#!/usr/bin/env python3
2

3
import logging
4
import os
5
import hashlib
6

7
logger = logging.getLogger("verify-checksum-models")
8

9

10
def sha256sum(file):
11
    block_size = 16 * 1024 * 1024  # 16 MB block size
12
    b = bytearray(block_size)
13
    file_hash = hashlib.sha256()
14
    mv = memoryview(b)
15
    with open(file, 'rb', buffering=0) as f:
16
        while True:
17
            n = f.readinto(mv)
18
            if not n:
19
                break
20
            file_hash.update(mv[:n])
21

22
    return file_hash.hexdigest()
23

24

25
# Define the path to the llama directory (parent folder of script directory)
26
llama_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
29
hash_list_file = os.path.join(llama_path, "SHA256SUMS")
30

31
# Check if the hash list file exists
32
if not os.path.exists(hash_list_file):
33
    logger.error(f"Hash list file not found: {hash_list_file}")
34
    exit(1)
35

36
# Read the hash file content and split it into an array of lines
37
with open(hash_list_file, "r") as f:
38
    hash_list = f.read().splitlines()
39

40
# Create an array to store the results
41
results = []
42

43
# Loop over each line in the hash list
44
for line in hash_list:
45
    # Split the line into hash and filename
46
    hash_value, filename = line.split("  ")
47

48
    # Get the full path of the file by joining the llama path and the filename
49
    file_path = os.path.join(llama_path, filename)
50

51
    # Informing user of the progress of the integrity check
52
    logger.info(f"Verifying the checksum of {file_path}")
53

54
    # Check if the file exists
55
    if os.path.exists(file_path):
56
        # Calculate the SHA256 checksum of the file using hashlib
57
        file_hash = sha256sum(file_path)
58

59
        # Compare the file hash with the expected hash
60
        if file_hash == hash_value:
61
            valid_checksum = "V"
62
            file_missing = ""
63
        else:
64
            valid_checksum = ""
65
            file_missing = ""
66
    else:
67
        valid_checksum = ""
68
        file_missing = "X"
69

70
    # Add the results to the array
71
    results.append({
72
        "filename": filename,
73
        "valid checksum": valid_checksum,
74
        "file missing": file_missing
75
    })
76

77

78
# Print column headers for results table
79
print("filename".ljust(40) + "valid checksum".center(20) + "file missing".center(20)) # noqa: NP100
80
print("-" * 80) # noqa: NP100
81

82
# Output the results as a table
83
for r in results:
84
    print(f"{r['filename']:40} {r['valid checksum']:^20} {r['file missing']:^20}") # noqa: NP100
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.