-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartdiff.py
More file actions
250 lines (202 loc) · 8.37 KB
/
smartdiff.py
File metadata and controls
250 lines (202 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
"""
smartdiff — Structure-aware diff tool.
JSON-aware (ignores key order), directory comparison, and formatted output.
Standard diff treats key reordering as changes. smartdiff doesn't.
Usage:
py smartdiff.py a.json b.json # JSON structural diff
py smartdiff.py dir1/ dir2/ # Directory comparison
py smartdiff.py file1.txt file2.txt # Text diff
py smartdiff.py a.json b.json --format json # Output diff as JSON
py smartdiff.py a.yaml b.yaml # YAML-aware diff
"""
import argparse
import json
import os
import sys
from pathlib import Path
from difflib import unified_diff
try:
import yaml
HAS_YAML = True
except ImportError:
HAS_YAML = False
def deep_diff(a, b, path="") -> list[dict]:
"""Recursively diff two data structures. Returns list of changes."""
changes = []
if type(a) != type(b):
changes.append({"path": path or "(root)", "type": "type_change",
"old": f"{type(a).__name__}: {_preview(a)}",
"new": f"{type(b).__name__}: {_preview(b)}"})
return changes
if isinstance(a, dict):
all_keys = set(a.keys()) | set(b.keys())
for key in sorted(all_keys):
child_path = f"{path}.{key}" if path else key
if key not in a:
changes.append({"path": child_path, "type": "added", "new": _preview(b[key])})
elif key not in b:
changes.append({"path": child_path, "type": "removed", "old": _preview(a[key])})
else:
changes.extend(deep_diff(a[key], b[key], child_path))
elif isinstance(a, list):
max_len = max(len(a), len(b))
for i in range(max_len):
child_path = f"{path}[{i}]"
if i >= len(a):
changes.append({"path": child_path, "type": "added", "new": _preview(b[i])})
elif i >= len(b):
changes.append({"path": child_path, "type": "removed", "old": _preview(a[i])})
else:
changes.extend(deep_diff(a[i], b[i], child_path))
else:
if a != b:
changes.append({"path": path or "(root)", "type": "changed",
"old": _preview(a), "new": _preview(b)})
return changes
def _preview(val, max_len=60) -> str:
"""Short preview of a value."""
s = str(val)
return s[:max_len] + "..." if len(s) > max_len else s
def diff_json_files(path_a: Path, path_b: Path) -> list[dict]:
"""Structural diff of two JSON files."""
a = json.loads(path_a.read_text(encoding="utf-8"))
b = json.loads(path_b.read_text(encoding="utf-8"))
return deep_diff(a, b)
def diff_yaml_files(path_a: Path, path_b: Path) -> list[dict]:
"""Structural diff of two YAML files."""
if not HAS_YAML:
raise ImportError("pyyaml not installed")
a = yaml.safe_load(path_a.read_text(encoding="utf-8"))
b = yaml.safe_load(path_b.read_text(encoding="utf-8"))
return deep_diff(a, b)
def diff_text_files(path_a: Path, path_b: Path) -> str:
"""Unified text diff."""
a_lines = path_a.read_text(encoding="utf-8", errors="ignore").splitlines(keepends=True)
b_lines = path_b.read_text(encoding="utf-8", errors="ignore").splitlines(keepends=True)
diff = unified_diff(a_lines, b_lines, fromfile=str(path_a), tofile=str(path_b))
return "".join(diff)
def diff_directories(dir_a: Path, dir_b: Path) -> dict:
"""Compare two directories."""
def get_files(d: Path) -> dict:
files = {}
for f in d.rglob("*"):
if f.is_file():
rel = f.relative_to(d)
files[str(rel)] = f
return files
files_a = get_files(dir_a)
files_b = get_files(dir_b)
keys_a = set(files_a.keys())
keys_b = set(files_b.keys())
result = {
"only_in_a": sorted(keys_a - keys_b),
"only_in_b": sorted(keys_b - keys_a),
"common": sorted(keys_a & keys_b),
"modified": [],
"identical": [],
}
for f in result["common"]:
content_a = files_a[f].read_bytes()
content_b = files_b[f].read_bytes()
if content_a == content_b:
result["identical"].append(f)
else:
size_a = len(content_a)
size_b = len(content_b)
result["modified"].append({"file": f, "size_a": size_a, "size_b": size_b,
"size_diff": size_b - size_a})
return result
def format_structural_diff(changes: list[dict], name_a: str, name_b: str) -> str:
"""Format structural diff as readable output."""
if not changes:
return f" No structural differences between {name_a} and {name_b}."
lines = []
lines.append(f" Structural diff: {name_a} vs {name_b}")
lines.append(f" Changes: {len(changes)}")
lines.append("")
for c in changes:
if c["type"] == "added":
lines.append(f" + {c['path']}: {c['new']}")
elif c["type"] == "removed":
lines.append(f" - {c['path']}: {c['old']}")
elif c["type"] == "changed":
lines.append(f" ~ {c['path']}:")
lines.append(f" old: {c['old']}")
lines.append(f" new: {c['new']}")
elif c["type"] == "type_change":
lines.append(f" ! {c['path']} (type changed):")
lines.append(f" old: {c['old']}")
lines.append(f" new: {c['new']}")
return "\n".join(lines)
def format_dir_diff(result: dict, dir_a: str, dir_b: str) -> str:
"""Format directory diff."""
lines = []
lines.append(f" Directory diff: {dir_a} vs {dir_b}")
lines.append(f" Files: {len(result['only_in_a'])} only in A, {len(result['only_in_b'])} only in B, "
f"{len(result['modified'])} modified, {len(result['identical'])} identical")
lines.append("")
if result["only_in_a"]:
lines.append(f" Only in {dir_a}:")
for f in result["only_in_a"][:20]:
lines.append(f" - {f}")
if len(result["only_in_a"]) > 20:
lines.append(f" ... and {len(result['only_in_a'])-20} more")
lines.append("")
if result["only_in_b"]:
lines.append(f" Only in {dir_b}:")
for f in result["only_in_b"][:20]:
lines.append(f" + {f}")
if len(result["only_in_b"]) > 20:
lines.append(f" ... and {len(result['only_in_b'])-20} more")
lines.append("")
if result["modified"]:
lines.append(f" Modified ({len(result['modified'])}):")
for m in result["modified"][:20]:
sign = "+" if m["size_diff"] >= 0 else ""
lines.append(f" ~ {m['file']} ({sign}{m['size_diff']} bytes)")
lines.append("")
return "\n".join(lines)
def main():
parser = argparse.ArgumentParser(description="smartdiff -- structure-aware diff tool")
parser.add_argument("a", help="First file or directory")
parser.add_argument("b", help="Second file or directory")
parser.add_argument("--format", choices=["text", "json"], default="text", help="Output format")
args = parser.parse_args()
path_a = Path(args.a)
path_b = Path(args.b)
if not path_a.exists():
print(f"Error: {path_a} not found", file=sys.stderr); sys.exit(1)
if not path_b.exists():
print(f"Error: {path_b} not found", file=sys.stderr); sys.exit(1)
# Directory diff
if path_a.is_dir() and path_b.is_dir():
result = diff_directories(path_a, path_b)
if args.format == "json":
print(json.dumps(result, indent=2))
else:
print(format_dir_diff(result, args.a, args.b))
return
# Detect format
ext_a = path_a.suffix.lower()
if ext_a in (".json",):
changes = diff_json_files(path_a, path_b)
if args.format == "json":
print(json.dumps(changes, indent=2))
else:
print(format_structural_diff(changes, args.a, args.b))
elif ext_a in (".yaml", ".yml") and HAS_YAML:
changes = diff_yaml_files(path_a, path_b)
if args.format == "json":
print(json.dumps(changes, indent=2))
else:
print(format_structural_diff(changes, args.a, args.b))
else:
# Text diff
result = diff_text_files(path_a, path_b)
if result:
print(result)
else:
print(f" Files are identical: {args.a} and {args.b}")
if __name__ == "__main__":
main()