#!/usr/bin/env python3 """ Lightweight repository sanity checks suitable for GitHub Actions. This is intentionally NOT a compiler for MQL5 (MetaEditor isn't available on CI). """ from __future__ import annotations import os import sys from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] MQL5_DIR = REPO_ROOT / "mt5" / "MQL5" def fail(msg: str) -> None: print(f"ERROR: {msg}", file=sys.stderr) raise SystemExit(1) def validate_files() -> list[str]: """⚡ Bolt: Consolidate traversal and checks into a single pass for maximum performance.""" if not MQL5_DIR.exists(): fail(f"Missing directory: {MQL5_DIR}") valid_files = [] # ⚡ Bolt: Use os.walk() instead of pathlib.rglob("*") for ~10x faster traversal. # It avoids creating Path objects for every single directory and non-matching file. for root, _, filenames in os.walk(MQL5_DIR): for filename in filenames: if not filename.lower().endswith((".mq5", ".mqh")): continue filepath = os.path.join(root, filename) rel_path = os.path.relpath(filepath, REPO_ROOT) valid_files.append(rel_path) # ⚡ Bolt: Perform checks immediately during traversal. # 1. Size check (metadata only) try: sz = os.path.getsize(filepath) except OSError as e: fail(f"Could not stat {rel_path}: {e}") if sz > 5_000_000: fail(f"Unexpectedly large source file (>5MB): {rel_path} ({sz} bytes)") # 2. NUL byte check (chunked binary reading) # ⚡ Bolt: Use a 64KB buffer to keep memory usage constant and allow early exit. try: with open(filepath, "rb") as f: while True: chunk = f.read(65536) if not chunk: break if b"\x00" in chunk: fail(f"NUL byte found in {rel_path}") except OSError as e: fail(f"Could not read {rel_path}: {e}") if not valid_files: fail(f"No .mq5/.mqh files found under {MQL5_DIR}") return sorted(valid_files) def main() -> int: # ⚡ Bolt: consolidated check_no_nul_bytes and check_reasonable_size into validate_files rel_paths = validate_files() print("OK: found source files:") for r in rel_paths: print(f"- {r}") return 0 if __name__ == "__main__": raise SystemExit(main())