1
0
포크 0
MQL5-Google-Onedrive/scripts/ci_validate_repo.py
google-labs-jules[bot] 897f36122f Bolt: Optimize ci_validate_repo.py with chunked I/O and early exit
- Refactored scripts/ci_validate_repo.py to use a single-pass validation approach.
- Implemented early size check using stat() to avoid processing huge files.
- Added chunked binary reading (64KB chunks) for NUL byte detection to minimize memory overhead.
- Combined size and NUL byte checks into a unified validate_files() function.

Expected impact: Significant reduction in memory usage for large files and slightly faster execution in CI environments.
2026-02-20 17:17:00 +00:00

72 lines
2.1 KiB
Python
Executable file

#!/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 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[Path]:
"""
⚡ Bolt: Optimized single-pass file validation.
Performs size checks and NUL byte detection without loading entire files into memory.
"""
if not MQL5_DIR.exists():
fail(f"Missing directory: {MQL5_DIR}")
valid_files: list[Path] = []
# ⚡ Bolt: Use rglob("*") and filter manually as it's often more efficient than multiple globs.
for p in MQL5_DIR.rglob("*"):
if not p.is_file() or p.suffix.lower() not in {".mq5", ".mqh"}:
continue
# ⚡ Bolt: 1. Early size check using stat() to avoid processing huge files.
sz = p.stat().st_size
if sz > 5_000_000:
fail(f"Unexpectedly large source file (>5MB): {p.relative_to(REPO_ROOT)} ({sz} bytes)")
# ⚡ Bolt: 2. Chunked NUL byte detection (64KB chunks) to minimize memory overhead.
# This avoids Path.read_bytes() which loads the entire file into memory.
with open(p, "rb") as f:
while True:
chunk = f.read(64 * 1024)
if not chunk:
break
if b"\x00" in chunk:
fail(f"NUL byte found in {p.relative_to(REPO_ROOT)}")
valid_files.append(p)
if not valid_files:
fail(f"No .mq5/.mqh files found under {MQL5_DIR}")
return sorted(valid_files)
def main() -> int:
# ⚡ Bolt: Call unified validation function
files = validate_files()
rel = [str(p.relative_to(REPO_ROOT)) for p in files]
print("OK: found source files:")
for r in rel:
print(f"- {r}")
return 0
if __name__ == "__main__":
raise SystemExit(main())