#!/usr/bin/env python3 """Запуск доступных линтеров по типам файлов проекта.""" from __future__ import annotations import argparse import os import shutil import subprocess from pathlib import Path EXCLUDES = {".git", ".venv", "venv", "node_modules", "dist", "build", "target", "vendor"} def find_files(root: Path, suffixes: tuple[str, ...], limit: int = 300) -> list[Path]: result = [] 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 suffixes: continue result.append(path) if len(result) >= limit: return result return result def run(command: list[str], cwd: Path) -> int: print("$ " + " ".join(str(item) for item in command)) result = subprocess.run(command, cwd=cwd, text=True) return result.returncode def main() -> int: parser = argparse.ArgumentParser(description="Запустить линтеры проекта") parser.add_argument("--path", type=Path, default=Path("."), help="Папка проекта") parser.add_argument("--fix", action="store_true", help="Применить автоисправления") parser.add_argument( "--tool", choices=["auto", "ruff", "shellcheck", "eslint"], default="auto", help="Ограничить одним инструментом", ) args = parser.parse_args() root = args.path.expanduser().resolve() if not root.is_dir(): print(f"Папка не найдена: {root}") return 2 selected = {args.tool} if args.tool != "auto" else {"ruff", "shellcheck", "eslint"} failures = 0 ran = 0 if "ruff" in selected: has_python = (root / "pyproject.toml").exists() or bool(find_files(root, (".py",), 1)) if has_python: if shutil.which("ruff"): ran += 1 command = ["ruff", "check", str(root)] if args.fix: command.append("--fix") if run(command, root) != 0: failures += 1 else: print("Ruff не установлен, проверка Python пропущена.") if "shellcheck" in selected: shells = find_files(root, (".sh", ".bash")) if shells: if shutil.which("shellcheck"): ran += 1 command = ["shellcheck", *[str(path) for path in shells]] if run(command, root) != 0: failures += 1 else: print("ShellCheck не установлен, проверка shell пропущена.") if "eslint" in selected: if (root / "package.json").exists(): if shutil.which("eslint"): ran += 1 command = ["eslint", str(root), "--ext", ".js,.jsx,.ts,.tsx"] if args.fix: command.append("--fix") if run(command, root) != 0: failures += 1 else: print("ESLint не установлен, проверка JavaScript пропущена.") print() if ran == 0: print("Не найдено подходящих линтеров или файлов.") return 0 if failures: print(f"Линтеры сообщили об ошибках в {failures} запусках.") return 1 print("Проверки линтеров пройдены.") return 0 if __name__ == "__main__": raise SystemExit(main())