Project Chimera
Меню

Исходники

152 строк · 6 КиБ

tools/env-dump-harness/compare.py

1#!/usr/bin/env python3
2"""Compare a bash sw_runlib env dump against a chl-cli `env` dump.
3
4Usage:
5 compare.py <baseline.dump> <bash_full.dump> <chl_output.txt> \
6 --scratch-root SCRATCH_ROOT --real-root REAL_ROOT
7
8Methodology: sw_runlib's `env | sort` dump includes the full ambient
9environment (TERM, HOME, DISPLAY, the incoming PATH, ...), which chl-cli's
10`env` command never claims to reproduce (it only prints the variables it
11manages). So the fair comparison is "everything sw_runlib's call chain
12actually SET or UNSET relative to the ambient baseline" vs "everything
13chl-runtime's Env::build() declares" -- not a literal full-environment diff.
14"""
15import sys
16import re
17import argparse
18
19
20def parse_env_dump(path):
21 d = {}
22 with open(path, encoding="utf-8", errors="replace") as f:
23 for line in f:
24 line = line.rstrip("\n")
25 if not line or line.startswith("[HARNESS]") or line.startswith("[ "):
26 continue
27 if "=" not in line:
28 continue
29 k, v = line.split("=", 1)
30 d[k] = v
31 return d
32
33
34def parse_chl_dump(path):
35 """Parse `chl env <game>` stdout. Header lines (конфиг:/сборка:/префикс:)
36 are skipped; KEY=value lines become vars; KEY=<снято> lines become unset."""
37 vars_ = {}
38 unset = set()
39 with open(path, encoding="utf-8", errors="replace") as f:
40 for line in f:
41 line = line.rstrip("\n")
42 m = re.match(r"^([A-Za-z_][A-Za-z0-9_]*)=(.*)$", line)
43 if not m:
44 continue
45 k, v = m.group(1), m.group(2)
46 if v == "<снято>":
47 unset.add(k)
48 else:
49 vars_[k] = v
50 return vars_, unset
51
52
53def main():
54 ap = argparse.ArgumentParser()
55 ap.add_argument("baseline")
56 ap.add_argument("bash_full")
57 ap.add_argument("chl_output")
58 ap.add_argument("--scratch-root", required=True)
59 ap.add_argument("--real-root", required=True)
60 ap.add_argument("--chl-root", required=True,
61 help="root chl was told to use via --root (usually same as real-root)")
62 args = ap.parse_args()
63
64 baseline = parse_env_dump(args.baseline)
65 bash_full_raw = parse_env_dump(args.bash_full)
66
67 # Normalize away the harness's scratch path so it reads as the real
68 # StartWine root chl would compute paths from -- this is a pure harness
69 # artifact (symlink tree lives under /tmp), not a real discrepancy.
70 def norm(v):
71 return v.replace(args.scratch_root, args.chl_root)
72
73 bash_full = {k: norm(v) for k, v in bash_full_raw.items()}
74
75 touched = {} # key -> value or None (unset relative to baseline)
76 for k in set(baseline) | set(bash_full):
77 bv = baseline.get(k)
78 fv = bash_full.get(k)
79 if bv != fv:
80 touched[k] = fv # None means bash's final env no longer has it
81
82 chl_vars, chl_unset = parse_chl_dump(args.chl_output)
83
84 all_keys = sorted(set(touched) | set(chl_vars) | chl_unset)
85
86 matches, mismatches, bash_only, chl_only, both_unset = [], [], [], [], []
87
88 for k in all_keys:
89 bash_touched = k in touched
90 bash_val = touched.get(k, "<untouched>") if bash_touched else None
91 in_chl_vars = k in chl_vars
92 in_chl_unset = k in chl_unset
93
94 if not bash_touched and not in_chl_vars and not in_chl_unset:
95 continue # shouldn't happen given all_keys construction
96
97 if bash_touched and bash_val is None:
98 # bash unset it (or it was present in baseline but absent at exec point)
99 if in_chl_unset:
100 both_unset.append(k)
101 elif in_chl_vars:
102 mismatches.append((k, "<снято>", chl_vars[k]))
103 else:
104 bash_only.append((k, "<снято>"))
105 elif bash_touched:
106 if in_chl_vars:
107 if chl_vars[k] == bash_val:
108 matches.append(k)
109 else:
110 mismatches.append((k, bash_val, chl_vars[k]))
111 elif in_chl_unset:
112 mismatches.append((k, bash_val, "<снято>"))
113 else:
114 bash_only.append((k, bash_val))
115 else:
116 # not touched by bash at all, but chl declares it
117 if in_chl_vars:
118 chl_only.append((k, chl_vars[k]))
119 elif in_chl_unset:
120 chl_only.append((k, "<снято>"))
121
122 print(f"=== СОВПАДЕНИЯ ({len(matches)}) ===")
123 for k in matches:
124 print(f" {k}={bash_full.get(k)}")
125
126 print(f"\n=== ОБА СНИМАЮТ ({len(both_unset)}) ===")
127 for k in both_unset:
128 print(f" {k}")
129
130 print(f"\n=== РАСХОЖДЕНИЯ ЗНАЧЕНИЙ ({len(mismatches)}) ===")
131 for k, bv, cv in mismatches:
132 print(f" {k}")
133 print(f" bash: {bv}")
134 print(f" chl: {cv}")
135
136 print(f"\n=== ТОЛЬКО В BASH (chl не выставляет вовсе) ({len(bash_only)}) ===")
137 for k, bv in bash_only:
138 print(f" {k}={bv}")
139
140 print(f"\n=== ТОЛЬКО В CHL (bash не трогает вовсе) ({len(chl_only)}) ===")
141 for k, cv in chl_only:
142 print(f" {k}={cv}")
143
144 print("\n=== ИТОГО ===")
145 print(f"совпадений: {len(matches)}, оба снимают: {len(both_unset)}, "
146 f"расхождений: {len(mismatches)}, только bash: {len(bash_only)}, "
147 f"только chl: {len(chl_only)}")
148
149
150if __name__ == "__main__":
151 main()
152