#!/usr/bin/env python3 """Поиск маркеров задач в исходниках проекта.""" from __future__ import annotations import argparse import json import os import re from collections import Counter, defaultdict from pathlib import Path MARKERS = { "TO" + "DO": "задача", "FIX" + "ME": "исправить", "HA" + "CK": "временное решение", "XX" + "X": "проверить", } MARKER_RE = re.compile( r"\b(" + "|".join(re.escape(item) for item in MARKERS) + r")\b[:\s-]*(.*)", re.IGNORECASE, ) EXTENSIONS = { ".py", ".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".sh", ".bash", ".ps1", ".bat", ".cmd", ".go", ".rs", ".java", ".kt", ".kts", ".c", ".h", ".cpp", ".hpp", ".cs", ".rb", ".php", ".swift", ".md", ".rst", ".yaml", ".yml", ".toml", ".ini", ".cfg", } EXCLUDES = { ".git", ".hg", ".svn", ".venv", "venv", "node_modules", "__pycache__", "dist", "build", "target", "vendor", } def iter_files(root: Path): for current, dirs, files in os.walk(root): current_path = Path(current) dirs[:] = [ name for name in dirs if name not in EXCLUDES and not (current_path / name).is_symlink() ] for name in files: path = current_path / name if path.is_symlink() or path.suffix.lower() not in EXTENSIONS: continue try: if path.stat().st_size > 1024 * 1024: continue data = path.read_bytes() except OSError: continue if b"\x00" in data[:2048]: continue yield path def main() -> int: parser = argparse.ArgumentParser(description="Найти маркеры задач в проекте") parser.add_argument("--path", type=Path, default=Path("."), help="Папка проекта") parser.add_argument( "--only", action="append", choices=list(MARKERS), help="Проверять только указанный маркер", ) parser.add_argument("--json", type=Path, help="Сохранить отчёт в JSON") parser.add_argument("--max-per-file", type=int, default=200, help="Лимит на файл") args = parser.parse_args() if args.max_per_file < 1: print("Параметр --max-per-file должен быть положительным.") return 2 root = args.path.expanduser().resolve() if not root.is_dir(): print(f"Папка не найдена: {root}") return 2 wanted = {item.upper() for item in args.only} if args.only else set(MARKERS) counts: Counter[str] = Counter() by_file: dict[str, list[dict[str, object]]] = defaultdict(list) total = 0 for path in iter_files(root): try: text = path.read_text(encoding="utf-8", errors="replace") except OSError: continue found = 0 relative = str(path.relative_to(root)) for number, line in enumerate(text.splitlines(), start=1): match = MARKER_RE.search(line) if not match: continue marker = match.group(1).upper() if marker not in wanted: continue text_value = match.group(2).strip() counts[marker] += 1 by_file[relative].append( {"line": number, "marker": marker, "text": text_value[:160]} ) total += 1 found += 1 if found >= args.max_per_file: break if args.json: payload = { "root": str(root), "total": total, "counts": dict(counts), "files": by_file, } args.json.write_text( json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) if total == 0: print("Маркеры задач не найдены.") return 0 print(f"Всего маркеров: {total}") for marker, count in counts.most_common(): print(f" {marker}: {count} ({MARKERS.get(marker, '')})") print() for name in sorted(by_file, key=lambda item: (-len(by_file[item]), item)): print(f"{name} ({len(by_file[name])})") for item in by_file[name][:10]: print(f" {item['line']}: {item['marker']} {item['text']}") return 0 if __name__ == "__main__": raise SystemExit(main())