|
5 | 5 | python3 run_check.py --lang java # 只跑 java |
6 | 6 | python3 run_check.py --fix # 失败时自动修复 |
7 | 7 | python3 run_check.py --mcp # MCP server 模式 |
| 8 | +
|
| 9 | +按语言执行细节(subprocess 调用、退出码归一化、fix 复检)全部委托给 |
| 10 | +`scripts/run_per_language.py`;本文件仅负责 argparse + 报告输出。 |
8 | 11 | """ |
9 | 12 | from __future__ import annotations |
10 | 13 |
|
11 | 14 | import argparse |
12 | | -import subprocess |
13 | 15 | import sys |
14 | 16 | from pathlib import Path |
15 | 17 |
|
16 | 18 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 19 | +import run_per_language |
17 | 20 | from detect_lang import ( |
18 | | - LANG_COMMANDS, |
19 | 21 | detect_languages, |
20 | 22 | find_project_root, |
21 | 23 | load_user_config, |
22 | 24 | ) |
23 | 25 |
|
24 | 26 |
|
25 | | -def run(cmd: list[str], cwd: Path, timeout: int = 120) -> tuple[int, str, str]: |
26 | | - try: |
27 | | - proc = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True, timeout=timeout) |
28 | | - return proc.returncode, proc.stdout, proc.stderr |
29 | | - except subprocess.TimeoutExpired: |
30 | | - return 124, "", f"timeout after {timeout}s" |
31 | | - except FileNotFoundError as e: |
32 | | - return 127, "", f"command not found: {e}" |
33 | | - |
34 | | - |
35 | | -def check_one(lang: str, project_root: Path, timeout: int) -> dict: |
36 | | - cmd_def = LANG_COMMANDS.get(lang) |
37 | | - if not cmd_def: |
38 | | - return {"language": lang, "passed": False, "error": "no command defined"} |
39 | | - rc, out, err = run(cmd_def["lint"], cwd=project_root, timeout=timeout) |
40 | | - return { |
41 | | - "language": lang, |
42 | | - "passed": rc == 0, |
43 | | - "exit_code": rc, |
44 | | - "stderr_tail": err[-2000:] if err else "", |
45 | | - "stdout_tail": out[-1000:] if out else "", |
46 | | - } |
47 | | - |
48 | | - |
49 | | -def fix_one(lang: str, project_root: Path, timeout: int) -> dict: |
50 | | - cmd_def = LANG_COMMANDS.get(lang) |
51 | | - if not cmd_def: |
52 | | - return {"language": lang, "fixed": False, "error": "no command defined"} |
53 | | - rc, out, err = run(cmd_def["format"], cwd=project_root, timeout=timeout + 60) |
54 | | - return { |
55 | | - "language": lang, |
56 | | - "fixed": rc == 0, |
57 | | - "exit_code": rc, |
58 | | - "stderr_tail": err[-2000:] if err else "", |
59 | | - } |
60 | | - |
61 | | - |
62 | 27 | def cli_main(): |
63 | 28 | parser = argparse.ArgumentParser() |
64 | 29 | parser.add_argument("--lang", help="只跑指定语言(逗号分隔)") |
@@ -89,15 +54,15 @@ def cli_main(): |
89 | 54 | print(f"[codeguard] languages: {languages}") |
90 | 55 | print() |
91 | 56 |
|
92 | | - results = [] |
93 | | - for lang in languages: |
94 | | - r = check_one(lang, project_root, args.timeout) |
| 57 | + results = run_per_language.run_check( |
| 58 | + languages, project_root, |
| 59 | + timeout=args.timeout, fix=args.fix, |
| 60 | + ) |
| 61 | + for r, lang in zip(results, languages): |
| 62 | + if r.get("dry_run"): |
| 63 | + continue |
95 | 64 | if not r["passed"] and args.fix: |
96 | 65 | print(f"[codeguard] \u26a0\ufe0f {lang} lint failed, attempting auto-fix...") |
97 | | - fix = fix_one(lang, project_root, args.timeout) |
98 | | - if fix["fixed"]: |
99 | | - r = check_one(lang, project_root, args.timeout) |
100 | | - results.append(r) |
101 | 66 | status = "\u2705 passed" if r["passed"] else f"\u274c FAILED (exit={r.get('exit_code')})" |
102 | 67 | print(f" {lang:12s} {status}") |
103 | 68 | if not r["passed"] and r.get("stderr_tail"): |
|
0 commit comments