File: check-prerequisites.py

package info (click to toggle)
kmc 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,716 kB
  • sloc: cpp: 38,308; python: 664; makefile: 216; perl: 179; sh: 34
file content (50 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3

import hashlib
import os
import sys
import subprocess

if len(sys.argv) < 2:
    print("Usage: {} <data_dir>".format(sys.argv[0]))
    sys.exit(1)

script_path = os.path.dirname(os.path.abspath(__file__))

def compute_md5(path):
    with open(path, "rb") as f:
        file_hash = hashlib.md5()
        while chunk := f.read(8192):
            file_hash.update(chunk)        
        return file_hash.hexdigest()

def verify_md5(baseDir, filePath, pattern):    
    cached_path_md5_file = os.path.join(baseDir, "cached", "md5", filePath + ".md5")    
    
    if not os.path.exists(os.path.dirname(cached_path_md5_file)):
        os.makedirs(os.path.dirname(cached_path_md5_file))
    if not os.path.exists(cached_path_md5_file):        
        md5 = compute_md5(os.path.join(baseDir, filePath))
        with open (cached_path_md5_file, "w") as f:
            f.write(md5)
    else:
        with open (cached_path_md5_file) as f:
            md5 = f.read()

    return md5 == pattern

with open(os.path.join(script_path, "prerequisite-files.txt")) as f:
    for line in f:
        line = line.strip()        
        path = line[0:-33]
        md5 = line[-32:]
        
        full_path = os.path.join(sys.argv[1], path)
        if not os.path.exists(full_path):
            print("{} does not exists".format(full_path))
            sys.exit(1)
        print("checking {}".format(full_path))
    
        if not verify_md5(sys.argv[1], path, md5):        
            print("Invalid md5 for {}".format(full_path))
            sys.exit(1)