2025-12-27 06:02:09 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2026-02-13 19:27:57 +00:00
|
|
|
⚡ Bolt: Optimized repository sanity checks.
|
|
|
|
|
Consolidated loops, chunked reading for NUL bytes, and size-first validation.
|
2025-12-27 06:02:09 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-02-13 19:27:57 +00:00
|
|
|
import os
|
2025-12-27 06:02:09 +00:00
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
MQL5_DIR = REPO_ROOT / "mt5" / "MQL5"
|
2026-02-13 19:27:57 +00:00
|
|
|
MAX_FILE_SIZE = 5_000_000 # 5MB
|
|
|
|
|
CHUNK_SIZE = 65536 # 64KB
|
2025-12-27 06:02:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def fail(msg: str) -> None:
|
|
|
|
|
print(f"ERROR: {msg}", file=sys.stderr)
|
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
|
|
|
2026-02-13 19:27:57 +00:00
|
|
|
def validate_repo() -> list[str]:
|
|
|
|
|
"""⚡ Bolt: Consolidated validation in a single pass for maximum performance."""
|
2025-12-27 06:02:09 +00:00
|
|
|
if not MQL5_DIR.exists():
|
|
|
|
|
fail(f"Missing directory: {MQL5_DIR}")
|
|
|
|
|
|
2026-02-13 19:27:57 +00:00
|
|
|
found_files: list[str] = []
|
|
|
|
|
|
|
|
|
|
# Use os.walk for efficient recursive traversal (faster than rglob for this purpose)
|
|
|
|
|
for root, _, files in os.walk(MQL5_DIR):
|
|
|
|
|
for filename in files:
|
|
|
|
|
if not (filename.lower().endswith('.mq5') or filename.lower().endswith('.mqh')):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
filepath = os.path.join(root, filename)
|
|
|
|
|
rel_path = os.path.relpath(filepath, REPO_ROOT)
|
|
|
|
|
found_files.append(rel_path)
|
|
|
|
|
|
|
|
|
|
# ⚡ Bolt: Check size BEFORE reading to avoid memory pressure and unnecessary I/O.
|
|
|
|
|
try:
|
|
|
|
|
file_stat = os.stat(filepath)
|
|
|
|
|
if file_stat.st_size > MAX_FILE_SIZE:
|
|
|
|
|
fail(f"Unexpectedly large source file (>5MB): {rel_path} ({file_stat.st_size} bytes)")
|
|
|
|
|
|
|
|
|
|
# ⚡ Bolt: Read in chunks to efficiently find NUL bytes without loading whole file.
|
|
|
|
|
# Allows early exit if NUL is found in the first chunk.
|
|
|
|
|
with open(filepath, 'rb') as f:
|
|
|
|
|
while True:
|
|
|
|
|
chunk = f.read(CHUNK_SIZE)
|
|
|
|
|
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 validate {rel_path}: {e}")
|
|
|
|
|
|
|
|
|
|
if not found_files:
|
|
|
|
|
fail(f"No .mq5/.mqh files found under {MQL5_DIR}")
|
2025-12-27 06:02:09 +00:00
|
|
|
|
2026-02-13 19:27:57 +00:00
|
|
|
return sorted(found_files)
|
2025-12-27 06:02:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> int:
|
2026-02-13 19:27:57 +00:00
|
|
|
files = validate_repo()
|
2025-12-27 06:02:09 +00:00
|
|
|
|
|
|
|
|
print("OK: found source files:")
|
2026-02-13 19:27:57 +00:00
|
|
|
for r in files:
|
2025-12-27 06:02:09 +00:00
|
|
|
print(f"- {r}")
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
raise SystemExit(main())
|