镜像自地址
https://github.com/A6-9V/MQL5-Google-Onedrive.git
已同步 2026-04-11 04:30:56 +00:00
This commit optimizes the repository validation script (`scripts/ci_validate_repo.py`) by consolidating file discovery and validation checks into a single pass. Key improvements: - Reduced filesystem traversals from 3 passes to 1. - Consolidated per-file I/O operations (stat and read) into a single loop. - Optimized validation sequence to perform cheap size checks before expensive content reads. - Implemented 64KB chunked reading for NUL byte detection to allow early exit and minimize memory usage. - Improved script robustness against large files by enforcing size limits before reading. These changes make the CI validation process faster and more resource-efficient, especially as the repository grows.
80 行
2.4 KiB
Python
可执行文件
80 行
2.4 KiB
Python
可执行文件
#!/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 main() -> int:
|
|
if not MQL5_DIR.exists():
|
|
fail(f"Missing directory: {MQL5_DIR}")
|
|
|
|
files: list[Path] = []
|
|
|
|
# ⚡ Bolt: Single-pass iteration to minimize I/O and redundant traversals.
|
|
# By combining discovery and multiple validation checks into a single loop,
|
|
# we avoid iterating over the file list multiple times and reduce system calls.
|
|
for p in MQL5_DIR.rglob("*"):
|
|
# ⚡ Bolt: Use suffix check as a cheap filter before more expensive operations.
|
|
if p.suffix.lower() not in {".mq5", ".mqh"}:
|
|
continue
|
|
if not p.is_file():
|
|
continue
|
|
|
|
# ⚡ Bolt: Perform size check BEFORE reading content to avoid memory pressure
|
|
# from large files and skip reading if the file is invalid.
|
|
# We fetch stats once and reuse them.
|
|
st = p.stat()
|
|
sz = st.st_size
|
|
if sz > 5_000_000:
|
|
fail(f"Unexpectedly large source file (>5MB): {p.relative_to(REPO_ROOT)} ({sz} bytes)")
|
|
|
|
# ⚡ Bolt: Check for NUL bytes using chunked reading to allow early exit
|
|
# and minimize memory footprint. This is more efficient than reading the whole file.
|
|
found_nul = False
|
|
try:
|
|
with open(p, 'rb') as f:
|
|
while True:
|
|
chunk = f.read(65536) # 64KB chunks
|
|
if not chunk:
|
|
break
|
|
if b"\x00" in chunk:
|
|
found_nul = True
|
|
break
|
|
except Exception as e:
|
|
fail(f"Error reading {p.relative_to(REPO_ROOT)}: {e}")
|
|
|
|
if found_nul:
|
|
fail(f"NUL byte found in {p.relative_to(REPO_ROOT)}")
|
|
|
|
files.append(p)
|
|
|
|
if not files:
|
|
fail(f"No .mq5/.mqh files found under {MQL5_DIR}")
|
|
|
|
# Sort for stable output
|
|
files.sort()
|
|
|
|
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())
|