|
| 1 | +"""检查 json 中诸符号的 StartOffset, EndOffset, Line, Content 的一致性 |
| 2 | +(假设本文件在 src/lang 中) |
| 3 | +
|
| 4 | +例如检查本项目: |
| 5 | +
|
| 6 | + $ ./lang -d -v --no-need-comment collect go . > lang.json |
| 7 | + # 应当成功,尤其应当是 --zero_linebase(行号从 0 开始) |
| 8 | + $ python3 check.py --json lang.json --base . --zero_linebase |
| 9 | +
|
| 10 | +检查 rust 项目 |
| 11 | +
|
| 12 | + $ ./lang -d -v --no-need-comment collect rust ../../testdata/rust2 > rust2.json |
| 13 | + $ python3 check.py --json lang.json --base . --zero_linebase --implheads |
| 14 | +""" |
| 15 | +import json |
| 16 | +import os |
| 17 | +import argparse |
| 18 | +import sys |
| 19 | +from collections import defaultdict |
| 20 | + |
| 21 | + |
| 22 | +def trim_multiline(s, max_lines=5): |
| 23 | + lines = s.splitlines() |
| 24 | + if len(lines) > max_lines: |
| 25 | + return "\n".join(lines[:max_lines]) + "\n..." |
| 26 | + return s |
| 27 | + |
| 28 | + |
| 29 | +def safe_decode(b): |
| 30 | + try: |
| 31 | + return b.decode("utf-8") |
| 32 | + except UnicodeDecodeError: |
| 33 | + return b.decode("utf-8", errors="replace") |
| 34 | + |
| 35 | + |
| 36 | +def verify_function_content( |
| 37 | + json_path, |
| 38 | + base_dir=".", |
| 39 | + bail_on_error=False, |
| 40 | + filter_files=None, |
| 41 | + filter_funcs=None, |
| 42 | + zero_linebase=False, |
| 43 | + implheads=False, |
| 44 | +): |
| 45 | + with open(json_path, "r", encoding="utf-8") as f: |
| 46 | + data = json.load(f) |
| 47 | + |
| 48 | + modules = data.get("Modules", {}) |
| 49 | + errors = defaultdict(list) |
| 50 | + |
| 51 | + for module_name, module in modules.items(): |
| 52 | + packages = module.get("Packages", {}) |
| 53 | + for package_name, package in packages.items(): |
| 54 | + functions = package.get("Functions", {}) |
| 55 | + for func_name, func in functions.items(): |
| 56 | + file_name = func.get("File") |
| 57 | + if not file_name: |
| 58 | + continue |
| 59 | + if filter_files and file_name not in filter_files: |
| 60 | + continue |
| 61 | + if filter_funcs and func_name not in filter_funcs: |
| 62 | + continue |
| 63 | + |
| 64 | + file_path = os.path.join(base_dir, file_name) |
| 65 | + try: |
| 66 | + with open(file_path, "rb") as src: |
| 67 | + content_bytes = src.read() |
| 68 | + except FileNotFoundError: |
| 69 | + print(f"[ERROR] File not found: {file_path}") |
| 70 | + errors[file_name].append(func_name) |
| 71 | + if bail_on_error: |
| 72 | + sys.exit(1) |
| 73 | + continue |
| 74 | + |
| 75 | + start = func["StartOffset"] |
| 76 | + end = func["EndOffset"] |
| 77 | + expected_content = func["Content"] |
| 78 | + actual_bytes = content_bytes[start:end] |
| 79 | + actual_content = safe_decode(actual_bytes) |
| 80 | + |
| 81 | + line_number = func["Line"] |
| 82 | + content_str = safe_decode(content_bytes) |
| 83 | + file_lines = content_str.splitlines() |
| 84 | + |
| 85 | + try: |
| 86 | + if zero_linebase: |
| 87 | + actual_line_content = file_lines[line_number].strip() |
| 88 | + else: |
| 89 | + actual_line_content = file_lines[line_number - 1].strip() |
| 90 | + except IndexError: |
| 91 | + actual_line_content = "<out of range>" |
| 92 | + |
| 93 | + if implheads: |
| 94 | + offset_match = actual_content in expected_content |
| 95 | + line_match = any( |
| 96 | + line.strip() == actual_line_content.strip() |
| 97 | + for line in expected_content.splitlines() |
| 98 | + ) |
| 99 | + else: |
| 100 | + offset_match = actual_content == expected_content |
| 101 | + expected_line_start = ( |
| 102 | + expected_content.splitlines()[0].strip() |
| 103 | + if expected_content |
| 104 | + else "" |
| 105 | + ) |
| 106 | + line_match = actual_line_content == expected_line_start |
| 107 | + |
| 108 | + print(f"[{module_name}/{package_name}] Checking function: {func_name}") |
| 109 | + if not offset_match: |
| 110 | + print(" [Mismatch] Offset content does not match.") |
| 111 | + print(" Expected:\n" + trim_multiline(expected_content)) |
| 112 | + print(" Actual:\n" + trim_multiline(actual_content)) |
| 113 | + if not line_match: |
| 114 | + display_line_number = line_number if zero_linebase else line_number |
| 115 | + print(f" [Mismatch] Line {display_line_number} mismatch:") |
| 116 | + print(f" Expected line (from JSON content):") |
| 117 | + if implheads: |
| 118 | + print(f" Any line in expected content matching actual line:") |
| 119 | + else: |
| 120 | + print(f" {expected_line_start}") |
| 121 | + print(f" Actual line:") |
| 122 | + print(f" {actual_line_content}") |
| 123 | + if not offset_match or not line_match: |
| 124 | + errors[file_name].append(func_name) |
| 125 | + if bail_on_error: |
| 126 | + sys.exit(1) |
| 127 | + if offset_match and line_match: |
| 128 | + print(" [OK] Function content and line verified.") |
| 129 | + print() |
| 130 | + |
| 131 | + if errors: |
| 132 | + print("===== MISMATCH SUMMARY =====") |
| 133 | + for file, funcs in errors.items(): |
| 134 | + print(f"File: {file}") |
| 135 | + for func in funcs: |
| 136 | + print(f" - {func}") |
| 137 | + print("============================") |
| 138 | + else: |
| 139 | + print("✅ All functions verified successfully!") |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + parser = argparse.ArgumentParser( |
| 144 | + description="Verify function content from JSON and source files." |
| 145 | + ) |
| 146 | + parser.add_argument( |
| 147 | + "--json", type=str, default="input.json", help="Path to the JSON file" |
| 148 | + ) |
| 149 | + parser.add_argument( |
| 150 | + "--base", type=str, default=".", help="Base directory for source files" |
| 151 | + ) |
| 152 | + parser.add_argument( |
| 153 | + "--bail_on_error", action="store_true", help="Stop at first error" |
| 154 | + ) |
| 155 | + parser.add_argument( |
| 156 | + "--filter_file", |
| 157 | + type=str, |
| 158 | + help="Comma-separated list of files to check (e.g. 'main.go,util.go')", |
| 159 | + ) |
| 160 | + parser.add_argument( |
| 161 | + "--filter_func", |
| 162 | + type=str, |
| 163 | + help="Comma-separated list of function names to check", |
| 164 | + ) |
| 165 | + parser.add_argument( |
| 166 | + "--zero_linebase", |
| 167 | + action="store_true", |
| 168 | + help="Line numbers in JSON are 0-based instead of 1-based", |
| 169 | + ) |
| 170 | + parser.add_argument( |
| 171 | + "--implheads", |
| 172 | + action="store_true", |
| 173 | + help="Allow actual content to be a substring of expected content and lines to match any line", |
| 174 | + ) |
| 175 | + |
| 176 | + args = parser.parse_args() |
| 177 | + filter_files = set(args.filter_file.split(",")) if args.filter_file else None |
| 178 | + filter_funcs = set(args.filter_func.split(",")) if args.filter_func else None |
| 179 | + |
| 180 | + verify_function_content( |
| 181 | + json_path=args.json, |
| 182 | + base_dir=args.base, |
| 183 | + bail_on_error=args.bail_on_error, |
| 184 | + filter_files=filter_files, |
| 185 | + filter_funcs=filter_funcs, |
| 186 | + zero_linebase=args.zero_linebase, |
| 187 | + implheads=args.implheads, |
| 188 | + ) |
0 commit comments