|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import hashlib |
| 4 | +import json |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from typing import Dict, List, NamedTuple, Tuple |
| 9 | + |
| 10 | + |
| 11 | +class BuildInfo(NamedTuple): |
| 12 | + commit_sha: str |
| 13 | + platform: str |
| 14 | + files: Dict[str, str] # relative path -> sha256 |
| 15 | + |
| 16 | + |
| 17 | +def calculate_checksum(file_path: Path) -> str: |
| 18 | + """Calculate SHA-256 checksum of a file.""" |
| 19 | + sha256_hash = hashlib.sha256() |
| 20 | + with open(file_path, "rb") as f: |
| 21 | + # Read in 1MB chunks to handle large files efficiently |
| 22 | + for byte_block in iter(lambda: f.read(4096*256), b""): |
| 23 | + sha256_hash.update(byte_block) |
| 24 | + return sha256_hash.hexdigest() |
| 25 | + |
| 26 | + |
| 27 | +def scan_deps(deps_dir: Path) -> Dict[str, str]: |
| 28 | + """Scan directory for dependency files and calculate their checksums.""" |
| 29 | + checksums = {} |
| 30 | + for file_path in deps_dir.rglob("*"): |
| 31 | + if not file_path.is_file(): |
| 32 | + continue |
| 33 | + |
| 34 | + # Only process shared and static libraries |
| 35 | + if not file_path.suffix in ['.so', '.dylib', '.a']: |
| 36 | + continue |
| 37 | + |
| 38 | + # Get path relative to deps_dir |
| 39 | + rel_path = str(file_path.relative_to(deps_dir)) |
| 40 | + try: |
| 41 | + checksums[rel_path] = calculate_checksum(file_path) |
| 42 | + except (IOError, OSError) as e: |
| 43 | + print(f"Error processing {rel_path}: {e}", file=sys.stderr) |
| 44 | + continue |
| 45 | + |
| 46 | + return checksums |
| 47 | + |
| 48 | + |
| 49 | +def generate_build_info(deps_dir: Path, platform: str, commit_sha: str) -> BuildInfo: |
| 50 | + """Generate build info for the given deps directory.""" |
| 51 | + checksums = scan_deps(deps_dir) |
| 52 | + return BuildInfo( |
| 53 | + commit_sha=commit_sha, |
| 54 | + platform=platform, |
| 55 | + files=checksums |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def verify_deps(deps_dir: Path, build_info: BuildInfo) -> Tuple[bool, List[str]]: |
| 60 | + """Verify deps directory against build info.""" |
| 61 | + mismatches = [] |
| 62 | + valid = True |
| 63 | + |
| 64 | + # Get current state of deps directory |
| 65 | + current_checksums = scan_deps(deps_dir) |
| 66 | + |
| 67 | + # Check for missing or mismatched files |
| 68 | + for rel_path, expected_checksum in build_info.files.items(): |
| 69 | + if rel_path not in current_checksums: |
| 70 | + mismatches.append(f"{rel_path}: file not found in deps directory") |
| 71 | + valid = False |
| 72 | + continue |
| 73 | + |
| 74 | + actual_checksum = current_checksums[rel_path] |
| 75 | + if actual_checksum != expected_checksum: |
| 76 | + mismatches.append( |
| 77 | + f"{rel_path}: checksum mismatch\n" |
| 78 | + f" expected: {expected_checksum}\n" |
| 79 | + f" got: {actual_checksum}" |
| 80 | + ) |
| 81 | + valid = False |
| 82 | + |
| 83 | + # Check for extra files |
| 84 | + for rel_path in current_checksums: |
| 85 | + if rel_path not in build_info.files: |
| 86 | + mismatches.append(f"{rel_path}: extra file in deps directory") |
| 87 | + valid = False |
| 88 | + |
| 89 | + return valid, mismatches |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + parser = argparse.ArgumentParser(description="Verify Lilliput dependencies") |
| 94 | + parser.add_argument("--deps-dir", required=True, type=Path, |
| 95 | + help="Directory containing dependencies (e.g., deps/linux or deps/osx)") |
| 96 | + |
| 97 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 98 | + |
| 99 | + # Generate command |
| 100 | + generate_parser = subparsers.add_parser("generate", |
| 101 | + help="Generate build info for dependencies") |
| 102 | + generate_parser.add_argument("--platform", required=True, |
| 103 | + choices=["linux", "macos"], |
| 104 | + help="Platform identifier") |
| 105 | + generate_parser.add_argument("--commit", required=True, |
| 106 | + help="Commit SHA that produced the build") |
| 107 | + generate_parser.add_argument("--output", type=Path, |
| 108 | + help="Output file (default: <deps-dir>/build-info.json)") |
| 109 | + |
| 110 | + # Verify command |
| 111 | + verify_parser = subparsers.add_parser("verify", |
| 112 | + help="Verify deps against build info") |
| 113 | + verify_parser.add_argument("--build-info", required=True, type=Path, |
| 114 | + help="Path to build info JSON file") |
| 115 | + |
| 116 | + args = parser.parse_args() |
| 117 | + |
| 118 | + if not args.deps_dir.exists(): |
| 119 | + print(f"Error: deps directory not found: {args.deps_dir}", file=sys.stderr) |
| 120 | + sys.exit(1) |
| 121 | + |
| 122 | + if args.command == "generate": |
| 123 | + build_info = generate_build_info( |
| 124 | + args.deps_dir, |
| 125 | + args.platform, |
| 126 | + args.commit |
| 127 | + ) |
| 128 | + |
| 129 | + output_file = args.output or args.deps_dir / "build-info.json" |
| 130 | + |
| 131 | + # Convert BuildInfo to dict for JSON serialization |
| 132 | + build_info_dict = build_info._asdict() |
| 133 | + |
| 134 | + try: |
| 135 | + with open(output_file, "w") as f: |
| 136 | + json.dump(build_info_dict, f, indent=4) |
| 137 | + print(f"Build info generated successfully: {output_file}") |
| 138 | + except (IOError, OSError) as e: |
| 139 | + print(f"Error writing build info: {e}", file=sys.stderr) |
| 140 | + sys.exit(1) |
| 141 | + |
| 142 | + elif args.command == "verify": |
| 143 | + try: |
| 144 | + with open(args.build_info) as f: |
| 145 | + build_info_dict = json.load(f) |
| 146 | + build_info = BuildInfo(**build_info_dict) |
| 147 | + except (IOError, OSError, json.JSONDecodeError) as e: |
| 148 | + print(f"Error reading build info: {e}", file=sys.stderr) |
| 149 | + sys.exit(1) |
| 150 | + |
| 151 | + print(f"Verifying deps against build from commit {build_info.commit_sha}") |
| 152 | + valid, mismatches = verify_deps(args.deps_dir, build_info) |
| 153 | + |
| 154 | + if not valid: |
| 155 | + print("\nVerification failed:") |
| 156 | + for mismatch in mismatches: |
| 157 | + print(f" {mismatch}") |
| 158 | + sys.exit(1) |
| 159 | + |
| 160 | + print("\nAll dependencies verified successfully") |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments