File size: 3,738 Bytes
3f15e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Verify the engine source inventories used by the release runner."""
from __future__ import annotations

import argparse
import hashlib
import json
from pathlib import Path


def digest(path: Path) -> str:
    sha = hashlib.sha256()
    with path.open("rb") as stream:
        for block in iter(lambda: stream.read(1024 * 1024), b""):
            sha.update(block)
    return sha.hexdigest()


def check(root: Path) -> dict:
    engine = root / "engine"
    inventory = engine / "BASE_SHA256SUMS"
    revision = engine / "REPAIR_V2_REVISION.json"
    mismatches = []
    inventory_count = 0
    revision_count = 0

    if not inventory.is_file():
        mismatches.append({"source": "BASE_SHA256SUMS", "reason": "missing_inventory"})
    else:
        for line_number, line in enumerate(inventory.read_text(encoding="utf-8").splitlines(), 1):
            if not line.strip():
                continue
            if "  " not in line:
                mismatches.append({"source": "BASE_SHA256SUMS", "line": line_number,
                                   "reason": "invalid_format"})
                continue
            expected, name = line.split("  ", 1)
            inventory_count += 1
            path = engine / name
            actual = digest(path) if path.is_file() else None
            if actual != expected:
                mismatches.append({"source": "BASE_SHA256SUMS", "path": name,
                                   "expected": expected, "actual": actual})

    if not revision.is_file():
        mismatches.append({"source": "REPAIR_V2_REVISION.json", "reason": "missing_revision"})
    else:
        try:
            data = json.loads(revision.read_text(encoding="utf-8"))
        except (OSError, json.JSONDecodeError) as exc:
            mismatches.append({"source": "REPAIR_V2_REVISION.json", "reason": "invalid_json",
                               "detail": str(exc)})
        else:
            recorded = data.get("implementation_sha256", {})
            if not isinstance(recorded, dict):
                mismatches.append({"source": "REPAIR_V2_REVISION.json",
                                   "reason": "invalid_implementation_sha256"})
            else:
                for name, expected in recorded.items():
                    revision_count += 1
                    path = engine / name
                    actual = digest(path) if path.is_file() else None
                    if actual != expected:
                        mismatches.append({"source": "REPAIR_V2_REVISION.json", "path": name,
                                           "expected": expected, "actual": actual})
            expected_inventory = data.get("inventory_sha256")
            actual_inventory = digest(inventory) if inventory.is_file() else None
            if expected_inventory != actual_inventory:
                mismatches.append({"source": "REPAIR_V2_REVISION.json",
                                   "path": "inventory_sha256", "expected": expected_inventory,
                                   "actual": actual_inventory})

    return {
        "root": str(root),
        "inventory_files": inventory_count,
        "revision_files": revision_count,
        "mismatches": mismatches,
        "passed": not mismatches,
    }


def main(argv=None) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--root", type=Path,
                        default=Path(__file__).resolve().parents[2],
                        help="tree root containing engine/")
    args = parser.parse_args(argv)
    result = check(args.root.resolve())
    print(json.dumps(result, indent=2, sort_keys=True))
    return 0 if result["passed"] else 1


if __name__ == "__main__":
    raise SystemExit(main())