|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# 规范化 diff:对比 pathlib 真值(posix_ref.py / windows_ref.py)与 goldfish 审计 |
| 3 | +# (posix_audit.scm / windows_audit.scm)的输出。 |
| 4 | +# |
| 5 | +# 用法: python3 pathlib-ref/normalize.py posix |
| 6 | +# python3 pathlib-ref/normalize.py windows |
| 7 | +# |
| 8 | +# 按 label 对齐两边,把 value 归一化后逐行比较,只输出不一致的行(并标注差异)。 |
| 9 | +# value 归一化:去引号、统一 list 括号为 (...)、布尔 True/False -> #t/#f。 |
| 10 | + |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import re |
| 14 | + |
| 15 | +ROOT = subprocess.DEVNULL # placeholder |
| 16 | +# resolve repo root from this file's location |
| 17 | +import os |
| 18 | +REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 19 | +GF = os.path.join(REPO, "bin", "gf") |
| 20 | + |
| 21 | + |
| 22 | +def norm(v): |
| 23 | + s = v.strip() |
| 24 | + # python bool |
| 25 | + if s in ("True", "False"): |
| 26 | + return "#t" if s == "True" else "#f" |
| 27 | + # list:[a,b](python) 或 ()(scheme) -> 统一为 (a b) |
| 28 | + if (s.startswith("[") and s.endswith("]")) or (s.startswith("(") and s.endswith(")")): |
| 29 | + inner = s[1:-1].strip() |
| 30 | + if not inner: |
| 31 | + return "()" |
| 32 | + items = [norm(x.strip()) for x in split_top(inner)] |
| 33 | + return "(" + " ".join(items) + ")" |
| 34 | + # python str '...' / "..." -> 去引号 |
| 35 | + if (s.startswith("'") and s.endswith("'")) or (s.startswith('"') and s.endswith('"')): |
| 36 | + return python_unescape(s[1:-1]) |
| 37 | + # scheme str "..." -> 去引号 |
| 38 | + if s.startswith('"') and s.endswith('"'): |
| 39 | + return s[1:-1] |
| 40 | + return s |
| 41 | + |
| 42 | + |
| 43 | +def split_top(s): |
| 44 | + """按逗号或空格切分顶层(忽略括号/引号内)。python 用逗号,scheme 用空格分隔 list 元素。""" |
| 45 | + out, cur, depth, in_str, q = [], "", 0, False, None |
| 46 | + for ch in s: |
| 47 | + if in_str: |
| 48 | + cur += ch |
| 49 | + if ch == q: |
| 50 | + in_str = False |
| 51 | + elif ch in "'\"": |
| 52 | + in_str = True |
| 53 | + q = ch |
| 54 | + cur += ch |
| 55 | + elif ch in "[(": |
| 56 | + depth += 1 |
| 57 | + cur += ch |
| 58 | + elif ch in "])": |
| 59 | + depth -= 1 |
| 60 | + cur += ch |
| 61 | + elif ch == "," and depth == 0: |
| 62 | + out.append(cur) |
| 63 | + cur = "" |
| 64 | + elif ch == " " and depth == 0: |
| 65 | + if cur: |
| 66 | + out.append(cur) |
| 67 | + cur = "" |
| 68 | + else: |
| 69 | + cur += ch |
| 70 | + if cur.strip(): |
| 71 | + out.append(cur) |
| 72 | + return out |
| 73 | + |
| 74 | + |
| 75 | +def python_unescape(s): |
| 76 | + # 把 python repr 的转义还原(\', \\, \n ...) |
| 77 | + return s.encode().decode("unicode_escape") if "\\" in s else s |
| 78 | + |
| 79 | + |
| 80 | +def parse(path): |
| 81 | + """解析 `label => value` 行为 (label, raw_value)。""" |
| 82 | + rows = {} |
| 83 | + with open(path) as f: |
| 84 | + for line in f: |
| 85 | + line = line.rstrip("\n") |
| 86 | + if " => " not in line: |
| 87 | + continue |
| 88 | + label, val = line.split(" => ", 1) |
| 89 | + rows[label.strip()] = val |
| 90 | + return rows |
| 91 | + |
| 92 | + |
| 93 | +def run(cmd): |
| 94 | + return subprocess.check_output(cmd, cwd=REPO, text=True) |
| 95 | + |
| 96 | + |
| 97 | +def main(flavor): |
| 98 | + py_file = os.path.join(REPO, "pathlib-ref", f"{flavor}_ref.py") |
| 99 | + scm_file = os.path.join(REPO, "pathlib-ref", f"{flavor}_audit.scm") |
| 100 | + py_out = run(["python3", py_file]) |
| 101 | + scm_out = run([GF, scm_file]) |
| 102 | + |
| 103 | + py_rows = {} |
| 104 | + scm_rows = {} |
| 105 | + for line in py_out.splitlines(): |
| 106 | + if " => " in line: |
| 107 | + lab, val = line.split(" => ", 1) |
| 108 | + py_rows[lab.strip()] = val |
| 109 | + for line in scm_out.splitlines(): |
| 110 | + if " => " in line: |
| 111 | + lab, val = line.split(" => ", 1) |
| 112 | + scm_rows[lab.strip()] = val |
| 113 | + |
| 114 | + labels = list(dict.fromkeys(list(py_rows.keys()) + list(scm_rows.keys()))) |
| 115 | + diffs = 0 |
| 116 | + only_py, only_scm = 0, 0 |
| 117 | + for lab in labels: |
| 118 | + if lab not in scm_rows: |
| 119 | + print(f"[仅 python] {lab} => {py_rows[lab]}") |
| 120 | + only_py += 1 |
| 121 | + diffs += 1 |
| 122 | + continue |
| 123 | + if lab not in py_rows: |
| 124 | + print(f"[仅 scheme] {lab} => {scm_rows[lab]}") |
| 125 | + only_scm += 1 |
| 126 | + diffs += 1 |
| 127 | + continue |
| 128 | + a = norm(py_rows[lab]) |
| 129 | + b = norm(scm_rows[lab]) |
| 130 | + if a != b: |
| 131 | + print(f"[差异] {lab}\n python = {a}\n scheme = {b}") |
| 132 | + diffs += 1 |
| 133 | + print(f"\n=== {flavor}: 共 {len(labels)} 项,差异 {diffs} (仅python {only_py}, 仅scheme {only_scm}) ===") |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main(sys.argv[1] if len(sys.argv) > 1 else "posix") |
0 commit comments